Projectile Motion Hitting a Terrain
Show older comments
Hi all! I am working on a project in which the user inputs an initial velocity and an initial launch angle for a projectile, which will then land and stop when it hits a predefined terrain. I am having trouble getting the projectile to stop once it hits the terrain. Here is a look at my code:
V = input('initial velocity:')
A = input('launch angle:')
x = [];
y = [];
for n = 0:.5:1000
R = n;
R2 = .5*n;
if n <= 300
x = [x n];
y = [y R];
elseif 300 < n && n <= 700
x = [x n];
y = [y R2];
end
end
plot(x,y)
hold on
g = -9.81;
Vx = V*cos(A*pi/180);
Vy = V*sin(A*pi/180);
T = 2*(-Vy)/g;
Px = [];
Py = [];
for t = 0:.5:T
Z = (Vy*t + .5 * g * power(t,2));
Px = [Px Vx*t];
Py = [Py Z];
end
XXX = Px(Px ~= 0);
XXX = round(XXX);
YYY = Py(Py ~= 0);
YYY = round(YYY);
check = 0;
for w = 1:1000
if YYY(w) < y(XXX(w))
check = 1;
C = w
Py = Py(1:C);
Px = Px(1:C);
break
end
end
check
plot(Px,Py)
I created the matrices XXX and YYY so that I could check using the for loop at the end of the code. Otherwise, the point (0,0) would bring in problems. I think the problem is that the variable C gives me an x coordinate too large.
Finally, I believe that there is a way to do this using comet and movie animations, but that is beyond my scope of Matlab
Let me know what you think the error is, Thanks!
1 Comment
Image Analyst
on 13 Jun 2014
You forgot to tell us typical values to input that will make a nice looking plot.
Answers (0)
Categories
Find more on Data Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!