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

조회 수: 3 (최근 30일)
Zlatan
Zlatan 2024년 1월 24일
댓글: Zlatan 2024년 1월 25일
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
Torsten
Torsten 2024년 1월 24일
편집: Torsten 2024년 1월 24일
Please include code as plain ascii text, not as graphics, so that we are able to execute it.
Zlatan
Zlatan 2024년 1월 25일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Sam Chak
Sam Chak 2024년 1월 24일
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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by