Attempted to access y(5); index out of bounds because numel(y)=2.
1 次查看(过去 30 天)
显示 更早的评论
I get this message when i try to run my code. I am new in the program. I don't know what to do
my code is following
function [dy,ay_NL,beta_NL,rss_NL]=derivNL(t,y,cons)
% Constants
y=zeros(2,1);
a=cons.a; % Longitudinal distance from COG to front wheels [m]
b=cons.b; % Longitudinal distance from COG to rear wheels [m]
C_alpha_f=cons.c_alpha_f; %[N/rad]-Guess/cornering stiffness front wheel
C_alpha_r=cons.c_alpha_r; %[N/rad]-Guess/Cornering stiffness rear wheel
I_zz=cons.I_zz; % Yaw inertia of mass approximation
M=cons.M; % 1688kg car, 80kg person, 79 liters fuel, 20kg oil u=cons.u; % Vihecle logditudinal speed [m/s]
w_r=cons.w_r;
w_f=cons.w_f;
L=cons.L;
g=cons.g;
my=cons.my; N_f=cons.N_f; N_r=cons.N_r;
% Steering angle at 0.2 g'es
delta_f=0.00395*3; % calculated analytic
% Inputs
alpha_f=((y(2)+a*y(1))/u)-delta_f; % Slip angle front
alpha_r=((y(2)-b*y(1))/u); % Slip angel rear
F_yf_NL=NLtire(alpha_f,2*C_alpha_f,my,N_f); % Lateral force front wheel [N]
F_yr_NL=NLtire(alpha_r,2*C_alpha_r,my,N_r); % Lateral force rear wheel [N]
% Yaw rate and lateral velocity
dy(1)=(1/I_zz)*(F_yf_NL*a-F_yr_NL*b); % Yaw rate [deg/sec]
dy(2)=((1/M)*(F_yf_NL+F_yr_NL))-u*y(1); % Lateral speed [m/sec]
dy(3)=(u*cos(y(5)))-(y(2)*sin(y(5))); % Global coordinat car X-direc
dy(4)=(u*sin(y(5)))+(y(2)*cos(y(5))); % Global coordinat car Y-direc
dy(5)=y(1); % Rotational angle psi
0 个评论
回答(3 个)
Julia
2014-9-12
Hi,
Since you declare y to be a zeros-vector, you overwrite your input y.
Your vector y has only 2 entries.
>> y=zeros(2,1)
y =
0
0
But you try to access y(5) when you compute dy.
0 个评论
Mischa Kim
2014-9-12
编辑:Mischa Kim
2014-9-12
Jeppe, if I am not mistaken, derivNL defines a system of differential equations. If that's the case, change
y = zeros(2,1);
to
dy = zeros(5,1);
y represents the initial conditions state vector (which should not be resetted to zero). dy is the derivative of the state vector.
Make sure to call the function with an inital conditions state vector of appropriate size, 5-by-1.
0 个评论
Rushikesh Tade
2014-9-12
Thats because you have declared as:
y=zeros(2,1);
which creates y variable as:
y=[0;0]
which means y variable is only having 2 elements in it and you can access these elements as y(1) and y(2) and not more than that.
now in the following lines
dy(3)=(u*cos(y(5)))-(y(2)*sin(y(5))); % Global coordinat car X-direc
dy(4)=(u*sin(y(5)))+(y(2)*cos(y(5))); % Global coordinat car Y-direc
you have tried to access 5th element in y using y(5) which is not present, resulting in error.
Also I have observed that you are trying to take y as an input from calling function in that case y=zeros(2,1) will overwrite the value of y which you got from calling function.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!