Index exceeds array bounds... need some help please!

1 view (last 30 days)
So my homework is asking me to create a program that uses projectile motion with and without air friction, which is given. I have written the following code, but upon running it, I am always greeted with the error in the title. The graph also seems to be off, as it does not follow my axis limits that I have set. If I could get any help on this, I would seriously appreciate it!
clc
clear all
close all
i= [1:100];
x(i)= 100;
y(i)= 100;
m=5; %Any given mass
V0=20; %Any given initial velocity
Theta0=45; %Any given initial angle
Vx(i)= V0*cos(Theta0);
Vy(i)= V0*sin(Theta0);%Initial velocities
T(i)=0; %Initial time
g=-9.81;
Fx=0; %Force = mass times accel, since there is no accel in x direction, 0.
Fy=m*g;
for i=[1:100]
Tf(i+1)=x(i+1);
DeltaT= Tf(i)-T(i);
x(i+1)= x(i)+Vx(i)*DeltaT;
Vx(i+1)= Vx(i)+Fx*DeltaT;
y(i+1)= y(i)+Vy(i)*DeltaT;
Vy(i+1)= Vy(i)+(Fy/m)*DeltaT;
plot(x,y);
nodrag=plot(x,y);
end
b=.3; %Defined drag co efficent of a baseball
FricX= b*Vx(i);
FricY= b*Vy(i);
for i=[1:100]
Tf(i)=x(i+1);
DeltaT= Tf(i)-T(i);
x(i+1)= x(i)+Vx(i)*DeltaT;
Vx(i+1)= Vx(i)+((Fx-FricX)/m)*DeltaT;
y(i+1)= y(i)+Vy(i)*DeltaT;
Vy(i+1)= Vy(i)+((Fy-FricY)/m)*DeltaT;
plot(x,y);
drag=plot(x,y);
end
xlim([0 100])
ylim([-100 100])
disp drag
disp nodrag

Answers (1)

Walter Roberson
Walter Roberson on 24 Feb 2021
i= [1:100];
x(i)= 100;
You initialize 100 elements
for i=[1:100]
Tf(i+1)=x(i+1);
You try to access 100+1 elements
Also: sin() and cos() expect radians. See sind() and cosd()
  2 Comments
Matt Kindhart
Matt Kindhart on 24 Feb 2021
I understand that now. But how do I fix this? When I try to change i on either one, the graph goes from a smooth parabola to a straight line. Also, appreciate the heads up on the trig functions, I have fixed them.
Walter Roberson
Walter Roberson on 24 Feb 2021
m=5; %Any given mass
V0=20; %Any given initial velocity
Theta0=45; %Any given initial angle
N = 100;
x(1:N) = 100;
y(1:N) = 100;
Vx(1:N) = V0*cos(Theta0);
Vy(1:N) = V0*sin(Theta0);%Initial velocities
T(1:N) = 0; %Initial time
g=-9.81;
Fx=0; %Force = mass times accel, since there is no accel in x direction, 0.
Fy=m*g;
for i=1:N-1
Tf(i+1)=x(i+1);
DeltaT= Tf(i)-T(i);
x(i+1)= x(i)+Vx(i)*DeltaT;
Vx(i+1)= Vx(i)+Fx*DeltaT;
y(i+1)= y(i)+Vy(i)*DeltaT;
Vy(i+1)= Vy(i)+(Fy/m)*DeltaT;
end
subplot(2,1,1)
plot(x, y, 'displayname', 'no drag');
min(x), max(x)
ans = 100
ans = 1.0306e+05
min(y), max(y)
ans = -4.6610e+08
ans = 1.8018e+03
legend show
hold on
b=.3; %Defined drag coefficent of a baseball
x(1:N) = 100;
y(1:N) = 100;
Vx(1:N) = V0*cos(Theta0);
Vy(1:N) = V0*sin(Theta0);%Initial velocities
T(1:N) = 0;
for i = 1 : N-1
Tf(i+1)=x(i+1);
DeltaT= Tf(i)-T(i);
x(i+1)= x(i)+Vx(i)*DeltaT;
FricX= b*Vx(i);
FricY= b*Vy(i);
Vx(i+1)= Vx(i)+((Fx-FricX)/m)*DeltaT;
y(i+1)= y(i)+Vy(i)*DeltaT;
Vy(i+1)= Vy(i)+((Fy-FricY)/m)*DeltaT;
end
subplot(2,1,2)
plot(x, y, 'displayname', 'drag');
legend show
%xlim([0 100])
%ylim([-100 100])
min(x), max(x)
ans = -5.5254e+70
ans = 1.1051e+70
min(y), max(y)
ans = -9.4936e+71
ans = 1.8987e+71
I had to make guesses about what you were trying to do. Your code was sort of like switching from no drag to drag in mid flight, but getting all the coordinates wrong when it did. I had to guess that instead you wanted to show simulations first of flight without drag and then the same flight with drag.
Notice your x with drag is going strongly negative. Your time calculation is wrong for both cases. You should either create a vector of times before the loop, or else you should have a delta t and start at 0 and each iteration add delta t to get the new time.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!