How to plot the values from while loop? I got blank graph

5 次查看(过去 30 天)
theta=input('Enter the launch angle in degrees:');
TimeIncrement=input('Enter the time increment in seconds:');
g=9.81;
Ti=0;
Xi=0;
Yi=100;
Vi=50;
t=0;
x=cosd(theta)*Vi*t;
y=Yi+(g/2)*(t+TimeIncrement)^2;
Vxi=cosd(theta)*Vi;
Vyi=sind(theta)*Vi;
fprintf('Vx = %g m/s\n\n',Vxi)
fprintf('Vy = %g m/s\n\n',Vyi)
y=100+1/2*(-9.81)*t^2;
while y>0
t=t+TimeIncrement;
y=Yi+Vyi*t+(-9.8/2)*(t)^2;
x=t*Vxi;
Vxf=Vxi;
Vyf=Vyi+(-9.81)*t;
end
plot(x,y)

采纳的回答

John BG
John BG 2017-2-23
Hi Mr Lee
now your script plots
theta=input('Enter the launch angle in degrees:');
TimeIncrement=input('Enter the time increment in seconds:');
g=9.81;
Ti=0;
Xi=0;
Yi=100;
Vi=50;
t=0;
x=cosd(theta)*Vi*t;
y=Yi+(g/2)*(t+TimeIncrement)^2;
Vxi=cosd(theta)*Vi;
Vyi=sind(theta)*Vi;
fprintf('Vx = %g m/s\n\n',Vxi)
fprintf('Vy = %g m/s\n\n',Vyi)
y=100+1/2*(-9.81)*t^2;
while y>0
t=t+TimeIncrement;
y=[y Yi+Vyi*t+(-9.8/2)*(t)^2];
x=[x t*Vxi];
Vxf=[Vxf Vxi];
Vyf=[Vyf Vyi+(-9.81)*t];
end
plot(x,y);grid on
.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  2 个评论
Bob Lee
Bob Lee 2017-2-23
hi, thanks for helping. it looks exactly like the graph that my professor assigned to me. but can you briefly explain why did you include those []? it doesnt make sense to me. Thank you!!
John BG
John BG 2017-2-23
编辑:John BG 2017-2-23
your code didn't save the points that were being generated.
the resulting variables in your lines
y=Yi+Vyi*t+(-9.8/2)*(t)^2;
x=t*Vxi;
are scalars, but to plot you need vectors.
There are other ways to do the same
instead of 'appending' at the end of each vector
if using a for loop k as counter you could simply
y(k)=Yi+Vyi*t(k)+(-9.8/2)*(t(k))^2;
x(k)=t(k)*Vxi;
because you are using a while, you have to declare k before the loop starts, and update k++ at each round
k=1;
while y>0
t(k)=t+TimeIncrement;
y(k)=Yi+Vyi*t(k)+(-9.8/2)*(t(k))^2;
x(k)=t(k)*Vxi;
Vxf(k)=Vxi(k);
Vyf(k)=Vyi(k)+(-9.81)*t(k);
k=k+1;
end
yet, in this case I like [], less key strokes.
regards
John BG

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by