Projectile Motion 2D plot without drag

Hi all. I am plotting a 2D plot of projectile motion, where speed and height are on the y axis and time is on the x axis. My speed is wrong, however. The speed is correct when plotted by itself, but when plotted with height, the graph seems to shift to the left. The point at which speed is supposed to be 0 is at t = 3.0581 s, which is what happens when I plot it on its own. But in the plot below its at 2.7 or 2.8 seconds. How do I fix this? I'm pretty sure there's something wrong with my 2D plot specification but I'm not sure what.
function [v, h] = projectile (v_0, theta, diagram)
% Gravitational acceleration, in m/s^2
g = 9.81;
% Time taken to hit the ground, s
t_hit = 2*(v_0/g)*sind(theta);
% Overall time taken from launch to hitting ground, s
t = 0:0.005:t_hit;
% Height, m
h = v_0*t*sind(theta)-(1/2)*g*(t.^2);
% Speed, m/s
v = sqrt(((v_0)^2)-(2*v_0*g*t*sind(theta))+(g^2)*(t.^2));
% Optional plotting; diagram = 1 is a true condition so v, h and plots
% will be outputted, otherwise diagram = 0 is a false condition and
% only v and h are outputted
if diagram == 1;
[ax,p1,p2] = plotyy(t,h,t,v);
set(p1,'linewidth',1.5);
set(p2,'linewidth',1.5);
% Label x-axis
xlabel(ax(1),'Time (s)')
% Label left y-axis
ylabel(ax(1),'Height (m)')
% Label right y-axis
ylabel(ax(2),'Speed (m/s)')
% Set limits for x-axis
set(ax(1),'XLim',[0 6.1162]);
set(ax(1),'XTick',[0:1:6.1162]);
% Set limits for left y-axis
set(ax(1),'YLim',[0 45.8550]);
set(ax(1),'YTick',[0:20:45.8550]);
% Set limits for right y-axis
set(ax(2),'YLim',[0 30]);
set(ax(2),'YTick',[0:10:30]);
% Vertical grid lines only
set(gca,'XGrid','on')
set(gca,'YGrid','off')
else
diagram = 0;
end
end

댓글 수: 2

Image Analyst
Image Analyst 2015년 11월 8일
How did you call it? What did you pass in for v_0, theta, & diagram?
MG
MG 2015년 11월 8일
편집: MG 2015년 11월 8일
I call
projectile(30, 90, 1)
if I want to plot. The diagram option is supposed to be there.

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2015년 11월 8일

0 개 추천

Get rid of your messed up xlim, ylim, xtick, and ytick calls and it looks fine:

댓글 수: 5

MG
MG 2015년 11월 8일
편집: MG 2015년 11월 8일
That's not how its supposed to look, though. Its supposed to look like this.
Also, I hope you saw the previous edit in my comment. I meant projectile(30, 90, 1) not projectile(20, 90, 1), sorry.
Image Analyst
Image Analyst 2015년 11월 9일
I just assumed your formulas were correct. They're probably not. Recheck them. Compare against the formula here: https://en.wikipedia.org/wiki/Projectile_motion#Velocity
MG
MG 2015년 11월 9일
I checked them and they are correct. I copied them from the assignment file and they also match what it says in wiki. I don't think there's anything wrong with the equations, there's something wrong with the graph. Because if I plot v and h separately, everything is correct.
You altered the x axis for the first curve but not the second. So either put in this line:
set(ax(2),'XLim',[0 6.1162]);
or take out this line:
set(ax(1),'XLim',[0 6.1162]);
MG
MG 2015년 11월 9일
That did the trick, thank you!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

MG
2015년 11월 8일

댓글:

MG
2015년 11월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by