# Matlab simulation for projectile motion

101 views (last 30 days)
Bruce Griffin on 20 Sep 2023
Edited: John D'Errico on 20 Sep 2023
Im trying to write/find a code to help my students understand projectile motion. They are having a hard time seeing and understanding the concept that one direction doesnt affect the other. I want to be able to show that the y components are the same by doing just the y position as a stamp on a number line. Can anyone help or show me a place that has this already?
Fifteen12 on 20 Sep 2023
I'm confused by this question. I'm assuming you're talking about the vector independence, but in any projectile path the y-coordinates (vertical displacement) should be changing as the projectile moves (unless your projectile is just rolling along the level ground). Can you clarify? Are you saying you want to compare the vertical displacement between a two projectiles with different horizontal impulses? And what do you mean when you say "stamp on a number line"? Wikipedia has a good simulation of this, if you're just looking for animations...

Cris LaPierre on 20 Sep 2023
Edited: Cris LaPierre on 20 Sep 2023
You may find the example used in Ch1 of Teaching with MATLAB helpful for this purpose. I think you could modify it easily enough to create the plot you are describing.

John D'Errico on 20 Sep 2023
Edited: John D'Errico on 20 Sep 2023
I think I don't understand their not understanding. (Ok, that seems confusing even to me, and I wrote it.)
I would just write it as the motion of an object in a plane. There is no need to think of it as a projectile. You have an object that starts out initially at (0,0) in the plane. with possibly some initial velocity. It accelerates in SOME direction, with an acceleration vector [a_x,a_y]. The acceleration may be a function of time, or not. If the acceleration is zero in both x and y components, then the path will be a simple one. I'd show that, FIRST. We might do it using a simple set of differential equations. I've done this below with constant accelerations.
xy0 = [0;0]; % starts at the origin
syms ax ay
syms vx0 vy0
syms x(t) y(t)
dx = diff(x);
dx2 = diff(dx);
dy = diff(y);
dy2 = diff(dy);
sol = dsolve(dx2 == ax,dy2 == ay,x(0) == xy0(1),dx(0) == vx0,y(0) == xy0(2),dy(0) == vy0)
sol = struct with fields:
y: (ay*t^2)/2 + vy0*t x: (ax*t^2)/2 + vx0*t
If the acceleration is constant, then the object will follow a straight path in the plane, but of course, it will be accelerating all the time. I've not said anything about gravity, YET. But we can trivially build it into the above solution.
T = 0:10;
plot(subs(sol.x,[ax,vx0,t],{0,1,T}),subs(sol.y,[ay,vy0,t],{0,1,T}),'-o') The object, with no acceleration imposed, will go on forever allong that path. We can see the stride between points plotted is exactly constant, as it should be.
And of course, if there is some acceleration, say a rocket launcing from a point in space, with no gravity field, then we might see this, for a rocket starting at an initial velocity of zero, but accelerating in both x and y.
plot(subs(sol.x,[ax,vx0,t],{1,0,T}),subs(sol.y,[ay,vy0,t],{1,0,T}),'-o') Again, we see what should be completely expected. the rocket is undergoing pure quadratic acceleration. If it could continue forever, it will eventually exceed the speed of light, but our equations have no relativistic component in them.
Now, what happens under a gravitational field for a projectile? There will be an acceleration due to gravity, AND there will be an initial velocity, but the projective was fired, as if from a gun. We will ignore air resistance, since that was not built into the equations of motion.
In this example, I have the initial velovity vvector as [100,100], but the acceleration vector will be [-9,8,0].
T = 0:20;
plot(subs(sol.x,[ax,vx0,t],{0,100,T}),subs(sol.y,[ay,vy0,t],{-9.8,100,T}),'-o') And we see a parabolic flight. After roughly 20 seconds, it will hit the ground. (I stopped just short of the big SPLAT.) But does the component of velocity in x stay constant?
diff(double(subs(sol.x,[ax,vx0,t],{0,100,T})))
ans = 1×20
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Again, I'm not sure where the disconnect lies, but I would probably just do a sequence of simulations like this, building their intuition about what happens as I go. Again, no air resistance, no relativistic corrections, etc. And if the projectile is in a gravitational field of the earth, if the projectile was moving quickly enough, we would need to deal with the earth as a sphere, so the acceleration will actually change when viewed in a cartesian coordinate system.