I'm trying to plot the results of the while loop but the graph is showing gup blank, can someone show me what I'm doing wrong? Thanks!

1 Ansicht (letzte 30 Tage)
% plot thetas for same projectile mass at different velocities
t = 0;
vv = 0;
while t < 179
t = acosd(1-(((m1*vv)^2)/(2*m2^2*g*L)));
vv = vv+0.01;
if t > 179
break
end
end
if system == 1
plot(vv,t),xlabel('Velocity (m/s)'),ylabel('Theta (deg)'),grid on
else
plot(vv,t),xlabel('Velocity (ft/s)'),ylabel('Theta (deg)'), grid on
end
yticks(0:5:180)

Antworten (1)

Walter Roberson
Walter Roberson am 25 Nov. 2022
t = 0;
vv = 0;
Those are scalars
t = acosd(1-(((m1*vv)^2)/(2*m2^2*g*L)));
vv is a scalar, and probably m1 and m2 and g and L are scalars, so t is probably a scalar on output
vv = vv+0.01;
adding a constant to a scalar gives a scalar.
if t > 179
break
end
That is inside a while t < 179 loop. If t > 179 then the while loop would end in the next step anyhow, so there is no point testing that here. The only case that would be different would be if t == 179 exactly, in which case the if would not match but the while would fail a microsecond later.
plot(vv,t),xlabel('Velocity (m/s)'),ylabel('Theta (deg)'),grid on
vv and t are scalars, so you are asking to plot() a single point.
If you want to plot all of the vv and t that you have encounted, then you should use indexed arrays, such as
while t(counter) < 179
counter = counter + 1;
t(counter) = acosd(1-(((m1*vv(counter-1))^2)/(2*m2^2*g*L)));
vv(counter) = vv(counter-1) + 0.01;
end
  2 Kommentare
Jeffrey
Jeffrey am 25 Nov. 2022
Thanks Walter, I had tried Indexing, but wasn't sure if I was doing it correctly because I kept getting a warning at vv(counter) and t(counter) that "the Variable changes size on evry loop iteration, consider preallocating". I also get an error at the start of the loop saying " Arry indices must be positive integers or logical values."
t = 0;
vv = 0;
counter = 0;
while t(counter) < 179
k = k+1;
t(counter) = acosd(1-(((m1*vv(counter-1))^2)/(2*m2^2*g*L)));
vv(counter) = vv(counter-1)+0.01;
Array indices must be positive integers or logical values.
Error in project (line 62)
while t(k) < 179
Walter Roberson
Walter Roberson am 25 Nov. 2022
t = 0;
vv = 0;
counter = 1;
while t(counter) < 179
counter = counter + 1;
t(counter) = acosd(1-(((m1*vv(counter-1))^2)/(2*m2^2*g*L)));
vv(counter) = vv(counter-1)+0.01;
end
Possibly you might want
t = 0;
vv = 0;
counter = 1;
while true
new_t = acosd(1-(((m1*vv(counter-1))^2)/(2*m2^2*g*L)));
if new_t >= 179; break; end
counter = counter + 1;
t(counter) = new_t;
vv(counter) = vv(counter-1)+0.01;
end
The difference is that with the first version, the last entry in t will be >= 179 whereas with the second version, the step that takes it above 179 will not be stored in the array.
The warning has to do with efficiency. You can trick MATLAB into not giving the warning even though the efficiency problem is still there, but ultimately you would still have the problem that you have a potentially infinite loop that could require all of the memory you have available.
Have you considered doing a more direct calculation, such as
syms vv
solvv = solve(acosd(1-(((m1*vv)^2)/(2*m2^2*g*L))) == 180)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by