# ode45 errorI have

5 views (last 30 days)
Oday Shahadh on 21 Nov 2023
Answered: Sam Chak on 21 Nov 2023
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);

Sam Chak on 21 Nov 2023
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