For loop with three variables

17 vues (au cours des 30 derniers jours)
Jacqueline Rigatto
Jacqueline Rigatto le 31 Oct 2020
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

Réponse acceptée

Alan Stevens
Alan Stevens le 31 Oct 2020
Modifié(e) : Alan Stevens le 31 Oct 2020
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 commentaires
Alan Stevens
Alan Stevens le 31 Oct 2020
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 le 1 Nov 2020
Thank you very much Alan Stevens, it helped a lot

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by