MATLAB: I’m trying to create a script file that determines the trajctory of a baseball thrown with an initial velocity of 20 m/s, there’s no air resistence.

preallocationprojectileqaudratictrajectory

I'm trying to create a script file that determines the trajctory of a baseball thrown with an initial velocity of 20 m/s, there's no air resistence. I need to plot the trajectories (YX vs Y) for angles between 5 and 85 degress in steps of 10; all on the same plot. For awhile I was able to get a straight line graphed but its suppposed to be a quadratic, now I get errors about improper preallocation, I've seen basic examples but I can't figure out how to get that to work here.
%%%%%code I'm trying to run starts on the next line%%%%
%initial values
xi=0;
yi=0;
Vi= 20; % meters per second
g=-9.81; %meters per second squared
THy=[5:10:85];
THx=[0:1:90];
t=zeros(1,3600);
for k=1:3600
dt=1;
t(k)=(k-1)*dt;
THy=[5:10:85];
vyi=Vi*sind(THy);%initail vel. in y
vyx=Vi*cosd(THy);%initial vel. in x
Y(:,t)=yi+vyi*t+.5*g*(t^2);%height of the ball
YX(:,t)=xi+vyx*t;%horizonatal distance traveled, no air resitance
end
plot(YX,Y)

Best Answer

There are more efficient ways to do this, however I opted to keep close to your original code. I added a loop for the angles, and vectorised it, but it doesn’t appear to be giving acceptable results. I will let you troubleshoot that.
The (slightly revised) code:
xi=0;
yi=0;
Vi= 20; % meters per second
g=-9.81; %meters per second squared
THy=[5:10:85];
THx=[0:1:90];
t=zeros(1,3600);
for k1 = 1:length(THy)
for k2=1:3600
dt=1;
t(k2)=(k2-1)*dt;
vyi=Vi.*sind(THy(k1));%initail vel. in y
vyx=Vi.*cosd(THy(k1));%initial vel. in x
Y(k1,k2)=yi+vyi.*t(k2)+.5.*g.*(t(k2).^2);%height of the ball
YX(k1,k2)=xi+vyx.*t(k2);%horizonatal distance traveled, no air resitance
end
end
plot(YX,Y)