MATLAB Nested For Loop and Plot Results
Show older comments
Hello, I'm new to MATLAB and I'm trying to have multiple overlayed plots on a single plot using nested for loop. However, my current code doesn't generate any results. Maybe I'm doing something wrong. I have Fz value ranging from 4 KN to 8 KN and the alpha value ranging from -10 to +10 degree. Appreciate your time in advance. Thanks.
a0 = 1.43E+00; a1 = -1.68E+01; a2 = -9.81E+02; a3 = -2.48E+03;
a4 = -1.15E+01; a5 = 0.00E+00; a6 = 1.90E-01; a7 = 8.16E-01;
a8 = -1.61E-02; a9 = -1.07E-01; a10 = 0.00E+00; a11 = -1.75E+01;
a12 = -7.20E+01; a13 = 0.00E+00; a14 = 0.00E+00; a15 = 0.00E+00;
a16 = 0.00E+00; a17 = 2.35E-01;
gamma = 1;
t = -10:0.1:10;
y = zeros(500,500);
for Fz = 4:1:8
for alpha = 1:0.1:length(t)
s_hy = (a8*Fz) + a9 + (a10*gamma);
s_vy = (a11*Fz) + a12 + (((a13*Fz)+a14)*(Fz*gamma));
Cy = a0;
Dy = ((a1*Fz^2)+(a2*Fz))*(1-(a15*gamma^2));
Ky = a3*sind(2*atan(Fz/a4))*(1-(a5*abs(gamma)));
By = Ky/(Cy*Dy);
alpha_y = alpha + s_hy;
Dummy = ((a16.*gamma)+a17)*sign(alpha_y);
Ey = ((a6*Fz)+a7)*(1-Dummy);
Dummy1 = Ey.*((By.*alpha_y) - (atand(By.*alpha_y)));
Dummy2 = Cy.*atan((By.*alpha_y) - Dummy1);
y(alpha) = -((Dy.*sin(Dummy2)) + s_vy);
end
hold all;
plot(t,y,'LineWidth', 2);
end
Answers (1)
Walter Roberson
on 20 Mar 2022
t = -10:0.1:10;
201 elements I believe.
for alpha = 1:0.1:length(t)
That seems odd. 1:0.1:201 would be over 2000 entries. Why would you want to iterate alpha according to the number of elements in t, unless you were iterating by 1 so that alpha might index t?
y(alpha) = -((Dy.*sin(Dummy2)) + s_vy);
alpha is not an integer, you cannot index with it.
y = zeros(500,500);
You created y as 2d but your assignment uses a single index. There are cases where that is valid, but I do not think those circumstances match.
When I scan your loop mentally it looks to me as if the only thing you are changing is alpha, so it looks to me as if you are wasting a lot of calculations. I might have overlooked something (middle of the night and all), but it looks to me as if your loop can only produce two different values, one for s_hy being more negative than alpha is positive, and the other for s_hy being less negative than alpha is positive (the case of exact equality seems unlikely in practice.)
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!