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

조회 수: 6 (최근 30일)
Bob Lee
Bob Lee 2017년 2월 23일
편집: John BG 2017년 2월 23일
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