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

2 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Robert Cumming
Robert Cumming le 2 Juil 2014
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 commentaires
Robert Cumming
Robert Cumming le 2 Juil 2014
its important due to the order of how operations are performed see the official documentation.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by