Filter löschen
Filter löschen

Projectile Motion 2D plot without drag

4 Ansichten (letzte 30 Tage)
MG
MG am 8 Nov. 2015
Kommentiert: MG am 9 Nov. 2015
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 Kommentare
Image Analyst
Image Analyst am 8 Nov. 2015
How did you call it? What did you pass in for v_0, theta, & diagram?
MG
MG am 8 Nov. 2015
Bearbeitet: MG am 8 Nov. 2015
I call
projectile(30, 90, 1)
if I want to plot. The diagram option is supposed to be there.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 8 Nov. 2015
Get rid of your messed up xlim, ylim, xtick, and ytick calls and it looks fine:
  5 Kommentare
Image Analyst
Image Analyst am 9 Nov. 2015
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 am 9 Nov. 2015
That did the trick, thank you!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by