Projectile motion without drag

11 views (last 30 days)
DiamondsRain
DiamondsRain on 9 Feb 2021
Edited: James Tursa on 9 Feb 2021
I've been trying to figure out how to make this script work for matlabs, but am getting a return that says "Index exceeds the number of array elements (1).. ..x(k+1)=Vx(k)*delt;" if anymore explantion if needed let me know (script below). Thank you!
%Projectile motion with and without drag
%Constants
gc=32.174;
g=-32.174;
v0=100;
ang=30;
ang=ang*pi/180;
mass=0.4;
weight=0.4;
area=11.34;
cd=0.6;
delt=0.1;
x0=0;
y0=0;
%Projectile motion without drag
x(1)=0;
y(1)=0;
Vx=v0*cos(ang);
Vy=v0*sin(ang);
k=1;
while y(k)>=0
Vy(k+1)=Vy(k)+g*delt;
y(k+1)=Vy(k)*delt;
x(k+1)=Vx(k)*delt;
k=k+1;
end
Xmax=max(x)
Ymax=max(y)
%with Drag%

Accepted Answer

James Tursa
James Tursa on 9 Feb 2021
Edited: James Tursa on 9 Feb 2021
You don't define the Vx(k) value before using it, hence the error. Since your Vx values don't change you can fix this error by simply removing the indexing for it. Or you could add a line to define Vx(k) for each k step.
Also, you need to add the velocity*dt to the current position. What you are doing is simply assigning the velocity*dt to the position.
So the code changes would be:
Vx(k+1) = Vx(k);
y(k+1) = y(k) + Vy(k)*delt;
x(k+1) = x(k) + Vx(k)*delt;
Adding the code to assign Vx(k+1) will make the code easier to modify once you add drag, because Vx(k+1) will in fact be changing from step to step in that case.
As an aside, I would advise annotating all of your numbers with unit comments. This makes your code much more readable and easier to spot unit errors. E.g.,
gc=32.174; % (m/s^2)
g=-32.174; % (m/s^2)
v0=100; % (m/s)
ang=30; % (deg)
etc.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!