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

조회 수: 1 (최근 30일)
Tom 2014년 7월 2일
댓글: Tom 2014년 7월 3일
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?

댓글을 달려면 로그인하십시오.

### 채택된 답변

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이전 댓글 1개 표시이전 댓글 1개 숨기기
Robert Cumming 2014년 7월 2일
its important due to the order of how operations are performed see the official documentation.
Tom 2014년 7월 3일
Okay, many thanks for this.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by