# Matlab simulation for projectile motion

49 views (last 30 days)

Show older comments

##### 1 Comment

Fifteen12
on 20 Sep 2023

### Answers (2)

Cris LaPierre
on 20 Sep 2023

Edited: Cris LaPierre
on 20 Sep 2023

##### 0 Comments

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)

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})))

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.

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!