# Attempted to access y(5); index out of bounds because numel(y)=2.

조회 수: 2 (최근 30일)
Jeppe Nikolajsen 2014년 9월 12일
답변: Rushikesh Tade 2014년 9월 12일
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
% 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]
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

댓글을 달려면 로그인하십시오.

### 답변 (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이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

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이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

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.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Units of Measurement에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by