Indexing Error with Ode45
Show older comments
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);
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!