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%

 채택된 답변

James Tursa
James Tursa 2021년 2월 9일
편집: James Tursa 2021년 2월 9일

1 개 추천

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2021년 2월 9일

편집:

2021년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by