Borrar filtros
Borrar filtros

For loop with three variables

10 visualizaciones (últimos 30 días)
Jacqueline Rigatto
Jacqueline Rigatto el 31 de Oct. de 2020
Comentada: Jacqueline Rigatto el 1 de Nov. de 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

Respuesta aceptada

Alan Stevens
Alan Stevens el 31 de Oct. de 2020
Editada: Alan Stevens el 31 de Oct. de 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 comentarios
Alan Stevens
Alan Stevens el 31 de Oct. de 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 el 1 de Nov. de 2020
Thank you very much Alan Stevens, it helped a lot

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by