ode function 45 how to implement it

My code is not working properly, what should i change so i ge the results. I m haveing trouble with ode function and i think with these passing parameters and initial conditions.
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
x0 = [0; 0] %for v0 and r0
x0 = 2×1
0 0
% Solve the differential equation using ode45
[t, x] = ode45(@(t, x) myVehicleODE(t, x, delta_function(t)), tspan, initial_conditions);
Unrecognized function or variable 'initial_conditions'.
figure;
subplot(2, 1, 1);
plot(t, x(:, 1), 'b', 'LineWidth', 2);
title('Longitudinal Velocity (v)');
subplot(2, 1, 2);
plot(t, x(:, 2), 'r', 'LineWidth', 2);
title('Yaw Rate (r)');
xlabel('Time');
% State-space model of your Vehicle
function dxdt = myVehicleODE(t, x, del)
% Elements in matrices (that depend on the parameters)
a11 = (-cf-cr)/(m*u);
a12 = (((-cf*a)+(cr*b))/(m*u))-u;
a21 = ((-cf*a)+(cr*b))/(I*u);
a22 = ((-cf*a*a)-(cr*b*b))/(I*u);
b1 = cf/m;
b2 = (cf*a)/I;
% 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*del;
end

6 个评论

can you attach the "all_units.xlsx" file and post the error?
Thanks
I suggest the MATLAB online introductory course free of costs to learn the basics of the language:
Just as the error states, you did not define the initial_conditions variable. However, you have x0 variable which is actually your initial condition. So change the line:
[t, x] = ode45(@(t, x) myVehicleODE(t, x, delta_function(t)), tspan, initial_conditions);
to
[t, x] = ode45(@(t, x) myVehicleODE(t, x, delta_function(t)), tspan, x0);
Having said that, this is not the only error in your code. I think you are lacking knowledge on how to write code in Matlab. As @Torsten suggested, first look at the basics of Matlab programming.
What is the delta_function you are referring to in your code? You did not define it anywhere.
Your function myVehicleODE does not know what cf cr ... is cause they are not defined within the function workspace.
There are probably some more things you need to fix before your code runs. However give it a try to fix the basic things and then ask again if you get stuck.
In particular see http://www.mathworks.com/help/matlab/math/parameterizing-functions.html for information on how to make cr and cf available to the function.
Some of these problems were subsequently corrected.
It turns out to be a stiff system, and the zeros for initial conditions is another problem. (My approach to a solution as well as Comment by Sam Chak are in help wtih plotting ode function.)
.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2022b

提问:

2024-1-25

Community Treasure Hunt

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

Start Hunting!

Translated by