exceeds the number of array

1 次查看(过去 30 天)
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)'})

采纳的回答

Walter Roberson
Walter Roberson 2020-11-16
编辑:Walter Roberson 2020-11-16
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 个评论
Jocelyn
Jocelyn 2020-11-16
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 2020-11-16
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by