MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# repeat a loop for 10 times

Asked by christiana on 30 Jan 2013

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

Matt Kindig on 30 Jan 2013

I'm confused. Won't your last line (L_repeat=...) be overwritten at the beginning of the next iteration?

christiana on 30 Jan 2013

Exactly. This new loop is very close to that I want. But somewhere I am wrong.

for j=1:9

`    j=j+1`
`    L_repeat(:,1)=[-9999; -9999; -9999; -9999; -9999; -9999; -9999; -9999]`
`    esh_repeat=zm./L_repeat(:,j)`
```    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(:,j)=((u_star_repeat.^2).*theta_horizontal_mean)./(0.40.*9.80665.*theta_star_repeat)`

end

christiana on 30 Jan 2013

I think this loop works. This is the answer.

for j=1:10

`    L_repeat(:,j)=((u_star_repeat.^2).*theta_horizontal_mean)./(0.40.*9.80665.*theta_star_repeat)`
`    L_repeat(:,1)=[-9999; -9999; -9999; -9999; -9999; -9999; -9999; -9999]`
`    esh_repeat=zm./L_repeat(:,j)`
```    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```

end

## Products

No products are associated with this question.

Answer by Cedric Wannaz on 30 Jan 2013
Edited by Cedric Wannaz on 30 Jan 2013

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.

christiana on 30 Jan 2013

The folowing loop works better. I have a table L_repeat(i,j). At the beginning I give values for the 1st column L_repeat(:,1). At the end of each loop I expect to add one more column in the "L_repeat" table and use this new column for calculations in this loop. After 10 iterations I expext to have the final results in the column L_repeat(:,10).

Any mistake in this new loop?

for j=1:9

`    j=j+1`
`    L_repeat(:,1)=[-9999; -9999; -9999; -9999; -9999; -9999; -9999; -9999]`
`    esh_repeat=zm./L_repeat(:,j)`
```    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(:,j)=((u_star_repeat.^2).*theta_horizontal_mean)./(0.40.*9.80665.*theta_star_repeat)`

end

Cedric Wannaz on 30 Jan 2013

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```