arrayfun once with 4 variables, or twice with 2 in each, then mult. Why do these two approaches give different solutions?

1 回表示 (過去 30 日間)
I have the following code to achieve W_n
%%CONSTANTS & VARIABLES
clear all; close all;
L_x = 27.4e-3; % membrane width (m)
L_y = 27.4e-3; % membrane height (m)
N_x = 3; % no. of x dim. eigenfreqs
N_y = 3; % no. of y dim. eigenfreqs
N = N_x*N_y; % total no. of eigenfreqs
numX = 101; % no. of x-dir. mapping points
numY = 101; % no. of y-dir. mapping points
xs = linspace(0,L_x,numX); % x-dir. membrane mapping points
ys = linspace(0,L_y,numY); % y-dir. membrane mapping points
%%SET UP MESHES
[X,Y] = meshgrid(ys,xs); % create X and Y array in 2d
XFull = repmat(X, [1 1 N]); % modify X and Y array for 3d
YFull = repmat(Y, [1 1 N]); % modify X and Y array for 3d
R = ones(numX, numY, N_x);% create mode array for X
RFull = []; % create an empty array (0 by 0)
for n = 1:N_x
RFull = cat(3, RFull, n*R);
end
S = ones(numX, numY); % create mode array for Y
SFull = []; % create an empty array (0 by 0)
for n = 1:N_y
SFull = cat(3, SFull, n*S);
end
SFull = repmat(SFull, [1 1 N_x]);
A = ones(numX, numY, N_x);% create mode array for X
AFull = []; % create an empty array (0 by 0)
for n = 1:N_x
AFull = cat(3, AFull, n*A);
end
B = ones(numX, numY); % create mode array for Y
BFull = []; % create an empty array (0 by 0)
for n = 1:N_y
BFull = cat(3, BFull, n*B);
end
BFull = repmat(BFull, [1 1 N_x]);
%%W_n
% 1st method
W_n_i = arrayfun(@(x,r,y,s)sin(x.*r*pi/L_x).*sin(y.*s*pi/L_y)...
,XFull,RFull,YFull,SFull);
% 2nd method
% W_n_x = arrayfun(@(x,r)sin(x.*(r*pi/L_x)), XFull, RFull);
% W_n_y = arrayfun(@(y,s)sin(y.*(s*pi/L_y)), YFull, SFull);
% W_n_ii = W_n_x .* W_n_y; % mode superposition
W_n = W_n_i; % choose 'i' or 'ii'
I can't work out why W_n_i is different to W_n_ii.
Could anyone help me to see why this is?
Many thanks in advance.

採用された回答

Robert Cumming
Robert Cumming 2014 年 7 月 2 日
Your calculations are not the same, in the second method you have:
sin(x.*(r*pi/L_x))
sin(y.*(s*pi/L_y))
in the first you have:
sin(x.*r*pi/L_x)
sin(y.*s*pi/L_y)
Note the extra brackets in the first method.
  3 件のコメント
Robert Cumming
Robert Cumming 2014 年 7 月 2 日
its important due to the order of how operations are performed see the official documentation.
Tom
Tom 2014 年 7 月 3 日
Okay, many thanks for this.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by