Indexing Error with Ode45
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!