I need help with ode45 function, how to write the code?

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 Kommentare

Dyuman Joshi
Dyuman Joshi am 24 Jan. 2024
Bearbeitet: Dyuman Joshi am 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.
the problem is I can't even start properly or get something, I always get an error, but I will try again
Sam Chak
Sam Chak am 24 Jan. 2024
Bearbeitet: Sam Chak am 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
Ok I found the better equations to solve, and now I have two of them, but I still think this is wrong. Any idea how to improve the code so it works?
Torsten
Torsten am 24 Jan. 2024
Bearbeitet: Torsten am 24 Jan. 2024
Please include code as plain ascii text, not as graphics, so that we are able to execute it.
clc
maks = xlsread("all_units.xlsx");
a=1.024;
b=1.602;
m=1574.02;
v=maks(:,24);
r=maks(:,20);
u=maks(:,23);
t1=maks(:,25);
cr=-25488;
cf=-184780;
I=1790;
del=(maks(:,18)+maks(:,19))/2;
alfaf= atan((v+r.*a)./u)-del;
alfar= atan((v-r.*b)./u);
Fyf=alfaf*cf;
Fyr=alfar*cr;
% Time span for the simulation
tspan = [min(t1),max(t1)];
% Initial condition
v0 = 0; % replace n with the appropriate size
% Solve the differential equation using ode45
[t,v,r] = ode45(@(t, v,r) myODE(t, v, Fyf,Fyr,m,r,u,tspan, v0);
% Plot the results
figure;
subplot(2, 1, 1);
plot(t, v);
xlabel('Time');
ylabel('v');
title('Solution of dv/dt = (Fyf + Fyr - u*r)/m');
subplot(2, 1, 2);
plot(t, r);
xlabel('Time');
ylabel('r');
title('Solution of dr/dt = (a*Fyf + b*Fyr)/I');
% Define the differential equation function
function dvdt = myODE(t, v, Fyf,Fyr,m,r,u)
% Define the differential equation
dvdt = (Fyf + Fyr - u*r)/m;
drdt = (a*Fyf + b*Fyr)/I;
end
this is the whole code

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sam Chak
Sam Chak am 24 Jan. 2024
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

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Tags

Gefragt:

am 24 Jan. 2024

Kommentiert:

am 25 Jan. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by