필터 지우기
필터 지우기

For loop with three variables

조회 수: 10 (최근 30일)
Jacqueline Rigatto
Jacqueline Rigatto 2020년 10월 31일
댓글: Jacqueline Rigatto 2020년 11월 1일
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