ode45 errorI have

2 次查看(过去 30 天)
Oday Shahadh
Oday Shahadh 2023-11-21
回答: Sam Chak 2023-11-21
I have the following code:
%% 22.initial input data vector
IDATA=[X0 Y0 Z0 Vx0 Vy0 Vz0];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%%%%%%%%%%% END OF INITIAL INPUTS AND CALCULATIONS %%%%%%%%%%%%%%%%%%%
%% ODE
Tol = 1e-12;
Tol0 = 1e-9;
tspan = (1:TS:TT0);
options = odeset('RelTol', Tol, 'AbsTol', Tol0);
% Assuming IDATA contains the initial conditions for your system
IDATA = [X0,Y0,Z0,Vx0,Vy0,Vz0];
[t, y] = ode45(@ODAYRK1, tspan, IDATA, options);
X = y(:,1);
Y = y(:,2);
Z = y(:,3);
Vx = y(:,4);
Vy = y(:,5);
Vz = y(:,6);
and the following ode45 function:
function ODAYRK = ODAYRK1(t, y)
global M e0 mi r0
% Extracting variables from the state vector
X = y(1);
Y = y(2);
Z = y(3);
Vx = y(4);
Vy = y(5);
Vz = y(6);
% Compute some intermediate values
r = sqrt(X.^2+ Y.^2+Z.^2);
% Define the ODEs
dXdt = Vx;
dYdt = Vy;
dZdt = Vz;
dVxdt = mi/r.^3.*X;
dVydt = mi/r^.3.*Y;
dVzdt = mi/r.^3.*Z;
% Assemble the derivative vector
ODAYRK = [dXdt;dYdt;dZdt;dVxdt;dVydt;dVzdt];
ends
and the following error:
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in test1971 (line 97)
[t, y] = ode45(@ODAYRK1, tspan, IDATA, options);
please help

回答(1 个)

Sam Chak
Sam Chak 2023-11-21
Some corrections in the ODE function (ODAYRK1)
Tol = 1e-12;
Tol0 = 1e-9;
tspan = [0 10];
options = odeset('RelTol', Tol, 'AbsTol', Tol0);
% Assuming IDATA contains the initial conditions for your system
IDATA = [1, 0, 0, 0, 0, 0];
[t, y] = ode45(@ODAYRK1, tspan, IDATA, options);
plot(t, y), grid on
function ODAYRK = ODAYRK1(t, y)
% global M e0 mi r0
mi = 1; % <-- define it inside the ode function
% Extracting variables from the state vector
X = y(1);
Y = y(2);
Z = y(3);
Vx = y(4);
Vy = y(5);
Vz = y(6);
% Compute some intermediate values
r = sqrt(X^2 + Y^2 + Z^2);
% Define the ODEs
dXdt = Vx;
dYdt = Vy;
dZdt = Vz;
dVxdt = mi/(r^3)*X;
dVydt = mi/(r^3)*Y; % <-- correction at this line
dVzdt = mi/(r^3)*Z;
% Assemble the derivative vector
ODAYRK = [dXdt; dYdt; dZdt; dVxdt; dVydt; dVzdt];
end

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by