MATLAB: Projectile motion hold function not working

air resistanceholdprojectile motion

I am having trouble with a projecile motion vs projectile motion with air resistance equation.
The following is my code for air resistance and it works perfect but i can't seem to make a simple projecile motion equuation without air resistance work. When I try to graph both of them on the same figue with the hold function it does not work.
clear , clc
g=9.81;
theta=input('Enter incident angle: ');
if (theta<=0)
disp('Illegal Input')
end
m=input('Enter mass(kg): ');
if (m<=0)
disp('Illegal Input')
end
v=input('Enter speed(m/s): ');
if (v<=0)
disp('Illegal Input')
end
A=input('Enter Area(meters): ');
if (A<=0)
disp('Illegal Input')
end
C=input('Enter Drag Coeficent(is .5 for spheres): ');
if (C<=0)
disp('Illegal Input')
end
rho=input('Enter density of air(average is 1.2): ');
if (rho<=0)
disp('Illegal Input')
end
Area=pi*(A)^2;
D=rho*C*A/2;
delta_t= .001;
x(1)=0;
y(1)=0;
vx=v*cosd(theta);
vy=v*sind(theta);
t(1)=0 ;
i=1;
while min(y)> -.001
v = sqrt(vx^2 + vy^2);
ax=-(D/m)*v*vx;
ay=-g-(D/m)*v*vy;
vx=vx+ax*delta_t;
vy=vy+ay*delta_t;
x(i+1)=x(i)+vx*delta_t+.5*ax*delta_t^2;
y(i+1)=y(i)+vy*delta_t+.5*ay*delta_t^2;
t(i+1)=t(i)+delta_t;
i=i+1;
end
plot(x,y)
xlabel('x distance (m)')
ylabel('y distance (m)')
title('Projectile Path')
below is my no air resitance equation
clear , clc
g=9.81;
theta=input('Enter incident angle: ');
if (theta<=0)
disp('Illegal Input')
end
v=input('Enter speed(m/s): ');
if (v<=0)
disp('Illegal Input')
end
t1=(0:.01:10000);
vx1=v*cosd(theta);
vy1=v*sind(theta);
x1 =vx1 * t1;
y1 =vy1 * t1 - (1/2) * g * t1 .^ 2;
plot (x1,y1)
and I know my second set of code is wrong but when I try to set the negative y values to 0
y1 (y1<0)=0
it messes up the graph.
also I simply cannot get the hold function to work.

Best Answer

Try this modification