repeat a loop for 10 times
显示 更早的评论
I try to create a loop for which I want to be repeated 10 times. 10 times are enough iterations in order to have at last a constant number.
The loop I create is the following.
I suppose for L_repeat a number (-9999) and after 10 iterations I expect to have the solution in the equation of L_repeat. At the end of each iteration the solution of the equation of L_repeat will be the numbers used in the beginning of the next iteration.
for iterations=10
L_repeat=[-9999; -9999; -9999; -9999; -9999; -9999; -9999; -9999]
esh_repeat=zm./L_repeat
if Ri_zm(:,1)<0
phi_m_repeat=(1-19.3.*esh_repeat).^(-1/4)
phi_H_repeat=0.95.*((1-11.6.*esh_repeat).^(-1/2))
else if Ri_zm(:,1)>0 & Ri_zm(:,1)<=0.2
phi_m_repeat=1+6.*esh_repeat
phi_H_repeat=0.95+(7.8.*esh_repeat)
end
end
% neutral
% φm(ς)=φΗ(ς)= φΕ(ς)=1
if Ri_zm(:,1)<=0.01 & Ri_zm(:,1)>=-0.01
u_star_repeat=0.40./p_u(:,1)
theta_star_repeat=0.40./p_theta(:,1)
% unstable και stable (Ri<=0.2)
else if Ri_zm(:,1)<-0.01 | Ri_zm(:,1)>0.01 & Ri_zm(:,1)<=0.2
u_star_repeat=0.40./(p_u(:,1).*phi_m_repeat(:,1))
theta_star_repeat=0.40./(p_theta(:,1).*phi_H_repeat(:,1))
end
end
L_repeat=((u_star_repeat.^2).*theta_horizontal_mean)./(0.40.*9.80665.*theta_star_repeat)
end
3 个评论
Matt Kindig
2013-1-30
I'm confused. Won't your last line (L_repeat=...) be overwritten at the beginning of the next iteration?
christiana
2013-1-30
christiana
2013-1-30
回答(1 个)
Your for loop statement is not built correctly:
for iterations=10
will just give iterations the value 10 and will execute what follows only once. You have to have a vector of values on the right hand side, that defines all the values that the loop variable has to take iteratively. For example:
for iterations = 1 : 10
...
end
I have a doubt about the rest of the code as well. My advice would be to build it progressively, displaying what happens. For example, make a first step:
for ii = 1 : 10
ii
end
and observe what happens. Then add a first, small computation in the loop, and check again that it is working, until you have implemented the full code in a well controlled way.
2 个评论
christiana
2013-1-30
编辑:christiana
2013-1-30
If you just test
for ii = 1 : 10
ii
end
You will see that the for loop iterates ii already. No need to do it yourself (with j=j+1). For the rest, I stick to my comment, that you should start with just a simple loop, and then progressively implement your computations, checking that each step produces the correct output.
Just a few additional remarks: for a purpose of efficiency, you could preallocate memory for L_repeat, before starting the loop. Also, you don't need to set L_repeat(:,1) at each step of your iteration.
L_repeat = zeros(8,10) ;
L_repeat(:,1) = -9999 ;
for jj = 2 : 10
L_repeat(:,jj) = ... something computed from L_repeat(:,jj-1) ?
end
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!