exceeds the number of array

1 view (last 30 days)
Jocelyn
Jocelyn on 16 Nov 2020
Commented: Walter Roberson on 16 Nov 2020
Hi,
I keep getting error messages about line 32 (Vy (i) = line). I double checked the brackets and they seem to match up. Below is the error message I am recieving.
Index exceeds the number of array elements (1).
Error in CC551_A5_P3 (line 32)
Vy(i) = Vx(i)*(tand(phi_o(i))-(g*t(i)/Vx_o)*(1+(0.5*Vx_o*k1*t(i)))); % y direction velocity
Thank you!
% Given Information In Problem
Vx_o = 2060; % muzzle velocity (ft/s)
k1 = 0.00082; % (1/ft) note: converted from 0.00025 (1/m)
p = 0.0751; % (lbm/ft^3) row - standard sea level met data
a = 1120; % (ft/s) standard sea level met data
% initial conditions
t = 0; % (s)
g = 32.174; %gravitational constant (ft/sec^2) [at sea level]
%setting up inital conditions at i = 1
t(1) = 0;
Vx(1) = 2060;
x(1) = 0;
phi_o(1) = 0;
phi(1) = 0;
for i = 1:6
x(i)= (i-1)*200*3; % x3 = converting range from yards to feet
Vx(i) = Vx_o*exp(-k1*x(i)); % striking velocity
t(i) = ((x(i)/Vx_o)*((Vx_o/Vx(i))-1))/log(Vx_o/Vx(i)); % time of flight
Vy(i) = Vx(i)*(tand(phi_o(i))-(g*t(i)/Vx_o)*(1+(0.5*Vx_o*k1*t(i)))); % y direction velocity
phi(i) = (atand(Vy(i)/Vx(i))); % impact angle
phi_o(i) = atand(tand(phi(i))+((g*t(i)/Vx_o)*(0.5*(1+(Vx_o/Vx(i))))));
end
Range_Table = table(x(:)/3, Vx(:), t(:), phi_o(:), phi(:),...
'VariableNames',{'yards', 'impact velocity (ft/s)', 'time of flight (s)', 'inital QE angle (degrees)', 'impact angle (degrees)'})

Accepted Answer

Walter Roberson
Walter Roberson on 16 Nov 2020
Edited: Walter Roberson on 16 Nov 2020
Vy(i) = Vx(i)*(tand(phi_o(i))-(g*t(i)/Vx_o)*(1+(0.5*Vx_o*k1*t(i)))); % y direction velocity
That needs phi_o(i) to exist. But
phi(i) = (atand(Vy(i)/Vx(i))); % impact angle
phi_o(i) = atand(tand(phi(i))+((g*t(i)/Vx_o)*(0.5*(1+(Vx_o/Vx(i))))));
it does not exist until a few lines later.
Consider the possibility that you should be iterating i=2:6 or i=2:7 and that you should be indexing at (i-1) in places.
  2 Comments
Jocelyn
Jocelyn on 16 Nov 2020
I know that the phi and phi_o values should change value each time through the loop.
I was trying to set phi_o = 0 for the first time in the loop so that the Vy(i) = could be calculated, and then the phi and phi_o values could be calculated and these values could be used during the second iteration of the loop and so forth until the loop stops.
When I subbed in i -1 into the (i) and changed the loop for i = 2:6 the table produced NaN, and zeros for the phi and phi o columns
% Given Information In Problem
Vx_o = 2060; % muzzle velocity (ft/s)
k1 = 0.00082; % (1/ft) note: converted from 0.00025 (1/m)
p = 0.0751; % (lbm/ft^3) row - standard sea level met data
a = 1120; % (ft/s) standard sea level met data
% initial conditions
t = 0; % (s)
g = 32.174; %gravitational constant (ft/sec^2) [at sea level]
%setting up inital conditions at i = 1
t(1) = 0;
Vx(1) = 2060;
x(1) = 0;
phi_o(1) = 0;
for i = 2:6
x(i)= (i-1)*200*3; % x3 = converting range from yards to feet
Vx(i) = Vx_o*exp(-k1*x(i-1)); % striking velocity
t(i) = ((x(i-1)/Vx_o)*((Vx_o/Vx(i-1))-1))/log(Vx_o/Vx(i-1)); % time of flight
Vy(i) = Vx(i-1)*(tand(phi_o(i-1))-(g*t(i-1)/Vx_o)*(1+(0.5*Vx_o*k1*t(i-1)))); % y direction velocity
phi(i) = (atand(Vy(i-1)/Vx(i-1))); % impact angle ---> x60 to convert from degrees to minutes
phi_o(i) = atand(tand(phi(i-1))+((g*t(i-1)/Vx_o)*(0.5*(1+(Vx_o/Vx(i-1))))));
end
Range_Table = table(x(:)/3, Vx(:), t(:), phi_o(:), phi(:),...
'VariableNames',{'yards', 'impact velocity (ft/s)', 'time of flight (s)', 'inital QE angle (degrees)', 'impact angle (degrees)'})
Walter Roberson
Walter Roberson on 16 Nov 2020
t(i) = ((x(i-1)/Vx_o)*((Vx_o/Vx(i-1))-1))/log(Vx_o/Vx(i-1)); % time of flight
when Vx_o == Vx(i-1) then Vx_o/Vx(i-1) is 1, and log(1) is 0, and you have a division by 0.
Vx(i) = Vx_o*exp(-k1*x(i-1)); % striking velocity
When i = 1 then (i-1)*something is 0 so x(i) is 0, and exp(-k1*0) is 1 so Vx(i) will be the same as Vx_o.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!