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

6 views (last 30 days)
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)

Accepted Answer

John BG
John BG on 23 Feb 2017
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 Comments
Bob Lee
Bob Lee on 23 Feb 2017
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 on 23 Feb 2017
Edited: John BG on 23 Feb 2017
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

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!