Projectile motion without drag

조회 수: 6 (최근 30일)
DiamondsRain
DiamondsRain 2021년 2월 9일
편집: James Tursa 2021년 2월 9일
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일
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.
  댓글 수: 1
DiamondsRain
DiamondsRain 2021년 2월 9일
Thank you much this helped a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by