I need help with ode45 function, how to write the code?
Show older comments
I need to write a code to solve the differential equatian but I don't even know how to start, I need to use the ode solver to find the solution
This is the hint they gave to me:

And this is the problem:

I already have all the values on the right side in the form of a vector nx1, only scalar numbers are Cf,Cr,m,la,I and lb.
6 Comments
Dyuman Joshi
on 24 Jan 2024
Edited: Dyuman Joshi
on 24 Jan 2024
Please show what you have tried yet.
Also, take a look at the documentation of ode45 for resources, examples and other references. This will help you formulate the equation in terms of the expected input to the ode solver.
Zlatan
on 24 Jan 2024
@Zlatan, Follow the example in the documentation, post your code and error message here. We will assess what went wrong.
Update: I understand now. Your problem is described in state-space form, which involves matrix differential equations. Unfortunately, the ode45 documentation does not provide a specific example using state-space representation. As a result, learning solely from examples may not be possible.
However, if you apply critical thinking skills or seek assistance from your course mates, you can work on resolving the matrix differential equation into individual ordinary differential equations. This process can be challenging, but with the right approach, you can overcome the frustration and find a solution.
Update 2: Moreover, the hint provided may be more confusing than helpful in guiding you. The ordinary differential equation (ODE) is defined as a dxdt vector, but the input arguments mentioned are t and y. Additionally, the usage of '...' is unclear, and it is uncertain what additional parameters should be passed to the function.
function dxdt = yourfunction(t, y, ...)
dxdt = A*y + B*d;
end
Zlatan
on 24 Jan 2024
Zlatan
on 25 Jan 2024
Answers (1)
Sam Chak
on 24 Jan 2024
Hi @Zlatan
It's great that you figured it out. Anyway, here is the code snippet for the state-space version. I would suggest placing certain parameters inside the vehicle ODE function as constants since they remain unchanged for the vehicle. If you need to run the simulation multiple times to analyze the vehicle's responses to the steering angle δ, you will need to pass this parameter (delta) in the vehicle ODE function as shown below:
%% State-space model of your Vehicle
function dxdt = myVehicleODE(t, x, delta)
% parameters (properties that do not change in the vehicle)
Cf = 1;
Cr = 1;
la = 1;
lb = 1;
m = 1;
I = 1;
u = 1;
% Elements in matrices (that depend on the parameters)
a11 = 1;
a12 = 1;
a21 = 1;
a22 = 1;
b1 = 1;
b2 = 1;
% matrices
A = [a11, a12; % state matrix
a21, a22];
B = [b1; % input matrix
b2];
% matrix differential equation (x is the state vector for [v; r])
dxdt = A*x + B*delta;
end
Categories
Find more on Programming 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!