What is wrong with my code?
Show older comments
I am currently doing a school project where i have to plot the trajectory of a projectile launched from the ground with initial speed V0, and angle theta above the horizontal. The projectile has to hit a target distance D=10000m away once it has reached the ground. I have used an initial guess of theta=pi/30 as the angle, so that the projectile does not reach D. The program should then automatically pick a new value of theta. I am doing this by increasing theta from pi/30 in steps dtheta=0.1 until the target is overshot. However, after plotting, I keep getting a wrong and wild graph. Here is my code:
%contants
D=10000;
u=600;
m=50;
g=9.81;
%initial conditions
theta=pi/30;
x=0;
i=0;
t=0;
y=0;
dtheta=0.1;
dt=0.1;
while x(i+1)<D
for i=1:1000
xdot(i)=u*cos(theta);
ydot(i)=u*sin(theta);
v(i)=sqrt(xdot(i).^2+ydot(i).^2);
x(i+1)=(t.*xdot(i));
y(i+1)=(t.*ydot(i))-(0.5*g*(t.^2));
xdbldot(i)=0;
ydbldot(i)=-g;
vel_x(i+1)=xdot(i)+(t.*xdbldot(i));
vel_y(i+1)=ydot(i)+(t.*ydbldot(i));
t=t+dt;
if y(i+1)<0
break
end
end
if x(i+1)<D
theta=theta+dtheta;
if x(i+1)>D
break
end
end
end
plot(x,y),grid
xlabel('Distance/[m]')
ylabel('Height/[m]')
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!