Indexing Error with Ode45
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matthew Lancaster
am 23 Mär. 2023
Kommentiert: Matthew Lancaster
am 23 Mär. 2023
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);
0 Kommentare
Akzeptierte Antwort
Jack
am 23 Mär. 2023
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!