For loop with three variables

10 次查看(过去 30 天)
u= [1.192 1.194 1.120]; % u_* [ms-1]
z=[0.0021 0.0029 0.0033]; % z_0 [m]
T=2:1:16;
f=1./T;
sigma= 2*pi*f; %[s-1]
g=9.8;
K=sigma.^2./g; % [m-1]
k=0.4;
i_teta=1:36;
teta_i=10.*i_teta;
for i=1:length(sigma)
for ii=1:length(K)
for j=1:length(teta_i)
mi(i,j)=(((g*z(1,1).*(K(ii).^2))./sigma.^2).*exp((k*sigma)./(K(ii).*u(1,1)).*cos(teta_i)))
end
end
end
The loop I want to make is from the equation below, where: u and z are fixed; sigma and k they are in two lines one below the other, both with 15 elements; and theta_i in column with 36 elements.
I am unable to loop for two rows and a column varying. How can I do (my code is above)?
I thank you for your help

采纳的回答

Alan Stevens
Alan Stevens 2020-10-31
编辑:Alan Stevens 2020-10-31
1.You could have m as a function of, i, j and ii. Your loop then might look like
for i=1:length(sigma)
s = sigma(i);
for ii=1:length(k)
K = k(ii);
for j=1:length(teta_i)
theta = teta_i(j);
mi(i,j,ii)=(g*z(1,1).*K.^2./s.^2).*exp(K*s./(K.*u(1,1)).*cos(theta));
end
end
end
However, this doesn't look right compared with your mathematical equation because you have confused Kappa and k:
I suspect they are not the same.
  3 个评论
Alan Stevens
Alan Stevens 2020-10-31
Since sigma and K go hand in hand the following should produce mi in the form you want
u= [1.192 1.194 1.120]; % u_* [ms-1]
z=[0.0021 0.0029 0.0033]; % z_0 [m]
T=2:1:16;
f=1./T;
sigma= 2*pi*f; %[s-1]
g=9.8;
K=sigma.^2./g; % [m-1]
k=0.4;
i_teta=1:36;
teta_i=10.*i_teta;
mi = zeros(numel(teta_i),numel(sigma));
for i=1:length(sigma)
for j=1:length(teta_i)
mi(j,i)=(((g*z(1,1).*(K(i).^2))./sigma(i).^2).*exp((k*sigma(i))./(K(i).*u(1,1)).*cos(teta_i(j))));
end
end
Jacqueline Rigatto
Jacqueline Rigatto 2020-11-1
Thank you very much Alan Stevens, it helped a lot

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by