Loop iteration not working

6 views (last 30 days)
Theodore Anderson
Theodore Anderson on 15 Oct 2021
Edited: Stephen23 on 17 Oct 2021
Hi there! I am really confused why this is not working. Please Help
so I want to iterate 1 2 3 4 5 6 7 8 i.e. the length of array waves
when I run this loop it simply assigns q to 1x1 double with the value 8...
when I run the exact same command in the command window it gives me what i am looking for
q = [1 2 3 4 ... etc ]
i must be missing something
waves=[.21 .22 .23 .5 .81 .82 .83 .84];
for q = 1:length(waves)
%Find intitial values
Lo(q) = (g/(2*pi))*(periods(q)^2);
k(q) = (2*pi)/Lo(q);
c(q) =((g/k(q)*tanh(k(q)*Depth(q))))^(1/2);
c_g(q) = c(q)*(.5 + ((k(q)*Depth(q))/sinh(2*k(q)*Depth(q))));
Ew(q) = .125*rho*g*Hrms(q)^2;
end
% oddly the same style of code works in this script:
for y = 2:length(profile_u)
h(y) = profile_u(y)+eta(y-1); % removed eta for now as that was causing the problem
if h(y) > dmin
L(y) = Lo*(tanh((4*(pi^2)*h(y)/(g*period^2).^(3/4)))).^(2/3); % fix equation as above
k(y) = (2*pi)/L(y);
c(y) = ((g/k(y)*tanh(k(y)*h(y))))^(1/2);
c_g(y) = c(y)*(.5 + ((k(y)*h(y))/sinh(2*k(y)*h(y))));
theta(y)= asin((sin(theta(y-1)*c(y)/c(y-1))));
Eb(y) = EbCoefficient* rho*g*B^3*freq_init.*(Hrms(y-1)^5)./((gamma^2)*(h(y).^3)).*(1-(1./(1+(Hrms(y-1)./(gamma*h(y)).^2)).^(5/2)));
Ew(y) = (Ew(y-1)*c_g(y-1)*cos(theta(y-1))-Eb(y)* deltax )/(c_g(y)*cos(theta(y)));
Hrms(y) = sqrt(8*(Ew(y)/(rho*g))); % put rho and g in ()
%%%%%setup
%radiation stress
Sxx(y) = Ew(y)*((cos(theta(y))^2+1)*c_g(y)/c(y) - .5); % take () around c_g/c - .5
%Setup
dSxxdx(y) = (Sxx(y-1)-Sxx(y))/deltax;
deltaEta(y) = dSxxdx(y)/(rho*(g*h(y)));
eta(y) = deltaEta(y)*deltax+eta(y-1); % added deltax
if y == 2
Hrms_b(1)=Hrms(1);
Ew_b(1) = Ew(1);
Eb_b(1) =0;
end
[Hrms_b,Ew_b,Eb_b,Hb]=waves_baldock(Lo,freq_init,Hrms_b,y,Hb,Eb_b,Ew_b,rho,g,c_g,deltax,h);
if y==2
Hrms_f(1)=Hrms(1);
Ew_f(1)=Ew(1);
epsilon_f(1)=0;
gamma_f=.37;
end
[Hrms_f,Ew_f,epsilon_f]=waves_falk(rho,g,B,freq_init,Hrms_f,y,gamma_f,Ew_f,deltax,c_g,h,epsilon_f);
if y==2
Hrms_r(1)=Hrms(1);
Ew_r(1)=Ew(1);
epsilon_r(1)=0;
gamma_r(1)=.76*k(1)*h(1)+.29;
Hbr(1)=.88/k(1)*tanh(gamma_r(1)*k(1)*h(1)/.88);
end
[Hrms_r,Ew_r,epsilon_r,Hbr]=waves_ruessink(k,h,y,rho,g,freq_init,Hrms_r,Ew_r,epsilon_r,c_g,deltax,Hbr);
%setup
else
Hrms(y)=NaN; Hrms_b(y)=NaN; Hrms_f(y)=NaN; Hrms_r(y)=NaN;
eta(y)=NaN; eta_RK(y)=NaN; Sxx(y)=NaN; L(y)=NaN; c(y)=NaN;
k(y)=NaN;
end
end
  7 Comments
Theodore Anderson
Theodore Anderson on 15 Oct 2021
Im not crazy i swear! look at my command window:
Stephen23
Stephen23 on 17 Oct 2021
Edited: Stephen23 on 17 Oct 2021
@Theodore Anderson: the code you showed in your question is different to that shown in your screenshots.
Note that in your actual code (i.e. screenshots) the FOR loop is called with a column vector for its VALUES input:
Careful reading of the FOR documentation tells us that it actually loops over the columns of the VALUES array.
This tells us that your code will iterate exactly once, with the loop iterator equal to that entire column vector that you provided as for its VALUES array. This is easy to confirm:
A = 1:8
A = 1×8
1 2 3 4 5 6 7 8
for q = A(:) % what you did (column vector -> iterates once)
display(q)
end
q = 8×1
1 2 3 4 5 6 7 8
for q = A % probably what you should have done (row vector -> iterates 8 times)
display(q)
end
q = 1
q = 2
q = 3
q = 4
q = 5
q = 6
q = 7
q = 8
"Im not crazy i swear! look at my command window:"
Sure.. but so far everything you have described is consistent with the documentation. Apparently what you expect to occur is something else... but due to your inconsistent code (i.e. difference between FOR loop given in your question vs screenshot) and the fact that your original complaint "when I run this loop it simply assigns q to 1x1 double with the value 8... " describes the expected behavior of a FOR loop after it has completed (and is also inconsistent with your screenshots), I am still trying to figure out what the problem is.
Changing the information details makes it harder to pinpoint problems that are due to details in the code.
Assume that I am stupid and that I cannot see your computer monitor. Explain what you expect. Explain what occurs.
PS: as DGM wrote, the loop is most likely superfluous anyway.

Sign in to comment.

Accepted Answer

DGM
DGM on 15 Oct 2021
Consider:
x = 1:10
x = 1×10
1 2 3 4 5 6 7 8 9 10
This expression by itself results in x being stored as a vector
for y = 1:10
y
end
y = 1
y = 2
y = 3
y = 4
y = 5
y = 6
y = 7
y = 8
y = 9
y = 10
y
y = 10
In a loop statement, the same expression results in y being assigned the same values one at a time in sequence. In this case, y is a scalar and when the loop exits, y retains the last value.
Ultimately, it's kind of moot here anyway. That whole loop isn't needed.
waves=[.21 .22 .23 .5 .81 .82 .83 .84];
periods = rand(size(waves)); % dummy values
Depth = rand(size(waves));
Hrms = rand(size(waves));
Lo = rand(size(waves));
c = rand(size(waves));
g = 1;
rho = 1;
% use the loop
for q = 1:length(waves)
%Find intitial values
Lo1(q) = (g/(2*pi))*(periods(q)^2);
k1(q) = (2*pi)/Lo1(q);
c1(q) =((g/k1(q)*tanh(k1(q)*Depth(q))))^(1/2);
c_g1(q) = c1(q)*(.5 + ((k1(q)*Depth(q))/sinh(2*k1(q)*Depth(q))));
Ew1(q) = .125*rho*g*Hrms(q)^2;
end
% do it without a loop
Lo = (g/(2*pi))*(periods.^2);
k = (2*pi)./Lo;
c = sqrt((g./k.*tanh(k.*Depth)));
c_g = c.*(.5 + ((k.*Depth)./sinh(2*k.*Depth)));
Ew = .125*rho*g*Hrms.^2;
% show that the results are identical
Lo-Lo1
ans = 1×8
0 0 0 0 0 0 0 0
k-k1
ans = 1×8
0 0 0 0 0 0 0 0
c-c1
ans = 1×8
0 0 0 0 0 0 0 0
c_g-c_g1
ans = 1×8
0 0 0 0 0 0 0 0
Ew-Ew1
ans = 1×8
0 0 0 0 0 0 0 0

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!