Indexing Error with Ode45

조회 수: 14 (최근 30일)
Matthew Lancaster
Matthew Lancaster 2023년 3월 23일
댓글: Matthew Lancaster 2023년 3월 23일
I am trying to simulate a self driving car before I implement a controller. I am getting an indexing error however.
Here is the code I am working on
start_loc = [0; 0];
end_loc = [10; 10]
obstacles = [5, 5, 1; 7, 8, 0.5];
[t, X] = simulate_self_driving_car(start_loc, end_loc, obstacles);
function [t, y] = simulate_self_driving_car(start_loc, end_loc, obstacles)
% Simulation parameters
tspan = [0, 20]; % Simulation time span
y0 = [start_loc(1); start_loc(2); atan2(end_loc(2)-start_loc(2), end_loc(1)-start_loc(1)); 0; 0]; % Initial conditions
disp(y0)
% Model parameters
L = 2.5; % Wheelbase (m)
m = 1000; % Mass (kg)
Cf = 80000; % Front tire stiffness (N/rad)
Cr = 120000; % Rear tire stiffness (N/rad)
Iz = 2000; % Moment of inertia (kg*m^2)
% ODE function
odefun = @(t, y) self_driving_car_ode(t, y, L, m, Cf, Cr, Iz, obstacles);
% Solve ODE
[t, y] = ode45(odefun, tspan, y0);
end
function dydt = self_driving_car_ode(t, y, L, m, Cf, Cr, Iz, obstacles)
% State variables
x = y(1);
y = y(2);
theta = y(3); % this is where the error begins
v = y(4);
phi = y(5);
%Removed rest of code for simplicity
end
The error being given is:
Index exceeds the number of array elements. Index must not exceed 1.
Error in main>self_driving_car_ode (line 55)
theta = y(3);
Error in main>@(t,y)self_driving_car_ode(t,y,L,m,Cf,Cr,Iz,obstacles) (line 36)
odefun = @(t, y) self_driving_car_ode(t, y, L, m, Cf, Cr, Iz, obstacles);
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in main>simulate_self_driving_car (line 39)
[t, y] = ode45(odefun, tspan, y0);
Error in main (line 18)
[t, X] = simulate_self_driving_car(start_loc, end_loc, obstacles);

채택된 답변

Jack
Jack 2023년 3월 23일
The error is occurring because you are overwriting the variable y in your self_driving_car_ode function. In the function definition, you have y as an input argument and then you are using it as a local variable. Therefore, when you try to access y(3) to get the value of theta, you are actually accessing the local variable y, which only has one element, causing the indexing error.
To fix this, you can change the name of the local variable in the self_driving_car_ode function. For example, you can change the line:
y = y(2);
to:
pos_y = y(2);
And then access the value of theta using y(3).
Alternatively, you can change the name of the input argument y in the function definition to avoid the conflict with the local variable. For example:
function dydt = self_driving_car_ode(t, state, L, m, Cf, Cr, Iz, obstacles)
% State variables
x = state(1);
y = state(2);
theta = state(3);
v = state(4);
phi = state(5);
% rest of the codeend
end
Then you can use state(3) to get the value of theta without any conflicts.
  댓글 수: 1
Matthew Lancaster
Matthew Lancaster 2023년 3월 23일
Thank you so much! I can't believe I overlooked that.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by