Index exceeds the number of array elements. Index must not exceed 281.

1 次查看(过去 30 天)
Hello
I have this error after I running my code
I have tried fixing , but it may doesn't work
and I think that I must need some help
the errors like
Index exceeds the number of array elements. Index must not exceed 2.
出错 main1 (第 55 行)
Cx__( j ) = Cx( t( i ) , vd( i ) / 340 , alpha_( j ) )
t( 1 ) = 0
xd( 1 ) = 0
yd( 1 ) = 20
vd( 1 ) = 20
xm( 1 ) = 26e3
ym( 1 ) = 15e3
vm = 300
theta( 1 ) = pi / 4
alpha( 1 ) = 0
q( 1 ) = atan( ( 15e3 - 20 ) / 26e3 )
gama( 1 ) = -q( 1 ) + theta( 1 )
R( 1 ) = sqrt( ( ym( 1 ) - yd( 1 )) ^ 2 + ( xm( 1 ) - xd( 1 ) ) ^ 2 )
wq = []
wt = []
an = []
dt = 0.01
t( 2 ) = t( 1 ) + dt
for i = 2 : 1 : 2001
xm( i ) = xm( i - 1 ) - vm * dt
ym( i ) = ym( 1 )
xd( i ) = vd( i - 1 ) * cos( theta( i - 1 )) * dt + xd( i - 1 )
yd( i ) = vd( i - 1 ) * sin( theta( i - 1 )) * dt + yd( i - 1 )
R( i ) = sqrt( ( ym( i ) - yd( i )) ^ 2 + ( xm( i ) - xd( i ) ) ^ 2 )
q( i ) = atan( ( ym( i ) - yd( i )) / ( xm( i ) - xd( i )) )
wq( i ) = ( q( i ) - q( i - 1 ) ) / dt
if t <= 2.79
vd( i ) = ( P( t( i - 1 )) - X( t( i - 1 ) , vd( i - 1 ) / 340 , 0 ) ...
-G( t( i - 1 ) * sin( theta( i - 1 )))) / m( t( i - 1 )) * dt + vd( i - 1 )
theta( i ) = ( Y( vd( i - 1 ) / 340 , t( i - 1 ) , alpha( i - 1 )) - G( t( i - 1 )) * cos( theta( i - 1 ))) / ...
( m( t( i - 1 )) * vd( i - 1 )) * dt + theta( i - 1 )
alpha( i ) = 0
gama( i ) = -q( i ) + theta( i )
wt( i ) = ( theta( i ) - theta( i - 1 ) ) / dt
if i >= 3
an( i ) = ( wt( i ) - wt( i - 1 ) ) / dt
end
t( i + 1 ) = t( i ) + dt
else
gama( i ) = gama( i - 1 ) + q( i ) - q( i - 1 )
theta( i ) = theta( i - 1 ) + gama( i ) - gama( i - 1 ) + q( i ) - q( i - 1 )
vd( i ) = ( R( i ) * ( q( i ) - q( i - 1 ))/ dt + vm * sin( q( i ))) / sin( gama( i ))
X_ = P( t ) - G( t ) * sin( theta( i )) - m( t ) * ( vd( i ) - vd( i - 1 )) / dt
Cx_ = 2 * X_ / ( 1.205 * vd( i ) ^ 2 * 1.5 )
Cx__ = []
alpha_( 1 ) = 0
alpha_( 2 ) = 0.08
for j = 1 : 1 : 100
Cx__( j ) = Cx( t( i ) , vd( i ) / 340 , alpha_( j ) )
if j >= 2
if abs( Cx__( j ) - Cx_ ) < abs( Cx__( j - 1 ) - Cx_ )
alpha( i ) = alpha_( j )
alpha_( j + 1 ) = alpha( j ) + 0.08
end
end
end
wt( i ) = ( theta( i ) - theta( i - 1 ) ) / dt
an( i ) = ( wt( i ) - wt( i - 1 ) ) / dt
t( i + 1 ) = dt + t( i )
end
end
an_max = max( an )
figure
plot( xd , yd )
title('弹道曲线')
xlabel('x')
ylabel('y')
figure
plot( t , vd )
title('速度随时间变化曲线')
xlabel('t')
ylabel('V')
figure
plot( t , an )
title('法向加速度随时间变化曲线')
xlabel('t')
ylabel('a_n')
figure
plot( t , alpha )
title('攻角随时间变化曲线')
xlabel('t')
ylabel('α/°')
figure
plot( t , q / pi * 180 )
title('视线角随时间变化曲线')
xlabel('t')
ylabel('q/°')
%升力
function [ Y_ ] = Y( Ma , t , alpha )
Cy = Cya( Ma , t ) * alpha
if t <= 2.79
Y_ = 0.5 * Cy * 1.205 * ( Ma * 340 ) ^ 2 * 1.94
else
Y_ = 0.5 * Cy * 1.205 * ( Ma * 340 ) ^ 2 * 1.5
end
end
%重力
function [ G_ ] = G( t )
G_ = m( t ) * 9.8
end
%阻力
function [ X_ ] = X( t , Ma , alpha )
if t <= 2.79
X_ = 0.5 * Cx( t , Ma , alpha ) * 1.205 * ( Ma * 340 ) ^ 2 * 1.94
else
X_ = 0.5 * Cx( t , Ma , alpha ) * 1.205 * ( Ma * 340 ) ^ 2 * 1.5
end
end
%阻力系数
function [ Cx_ ] = Cx( t , Ma , alpha )
range1 = xlsread( '作业5.xlsx' , 'sheet1' , 'B9:F16' )
range2 = xlsread( '作业5.xlsx' , 'sheet1' , 'B28:H33' )
range_alpah = xlsread( '作业5.xlsx' , 'sheet1' , 'B8:F8' )
range_Ma1 = xlsread( '作业5.xlsx' , 'sheet1' , 'A9:A16' )
range_Ma2 = xlsread( '作业5.xlsx' , 'sheet1' , 'A28:A33' )
if t <= 2.79
Cx_ = interp2( range_alpah , range_Ma1 , range1 , alpha , Ma , 'spline' )
else
Cx_ = interp2( range_alpah , range_Ma2 , range2 , alpha , Ma , 'spline' )
end
end
%升力系数
function [ Cy ] = Cya( Ma , t )
range1 = xlsread( '作业5.xlsx' , 'sheet1' , 'B3:I4' )
range2 = xlsread( '作业5.xlsx' , 'sheet1' , 'B22:H23' )
Cy_range1 = range1( 2 , : )
y1 = spline( range1( 1 , : ) , Cy_range1 )
Cy_range2 = range2( 2 , : )
y2 = spline( range2( 1 , : ) , Cy_range2 )
if t <= 2.79
Cy = ppval( y1 , Ma )
else
Cy = ppval( y2 , Ma )
end
end
%质量
function [ m_ ] = m( t )
if t <= 2.79
m_ = 2022 - t * 219.45
else
m_ = 1009 - ( t - 2.79 ) * 7.4271
end
if t > 20
m_ = 881.1796
end
end
%推力
function [ T ] = P( t )
if t <= 2.79
T = 40159.35 * 9.8
else
T = 5135.94 * 9.8
end
if t > 20
T = 0
end
end
  1 个评论
James Tursa
James Tursa 2023-6-2
Type the following at the command line:
dbstop if error
Then run your code. When the error occurs, the program will pause with all variables intact. Examine them to figure out which variables have dimensions that you were not expecting. Then backtrack in your code to figure out why.

请先登录,再进行评论。

回答(0 个)

类别

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

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by