# Can anyone help me with my code?

25 views (last 30 days)
Kendal Kobayashi on 22 Feb 2021
Answered: Jan on 22 Feb 2021
clear all
close all
clc
x = 1.2;
x2 = 0;
y = 0;
y2 = -1.04935751;
T = 6.19216933;
u = 1/83.45;
us = 1-u;
tspan = [0 2*T];
IC = [1.2 0 0 -1.04935751];
orbit(x,x2,y,y2);
options = odeset('RelTol',1e-6);
[TIME, Z] = ode45(@(x,x2,y,y2) orbit(x,x2,y,y2),tspan,IC,options);
axis([-1.5 1.5 -1.5 1.5]); axis equal, grid on
function [P] = orbit(x,x2,y,y2)
u = 1/83.45;
us = 1-u;
A = (x+u)^2+y^2;
B = (x-us)^2+y^2;
r1 = sqrt(A);
r2 = sqrt(B);
P = zeros(1,2);
P(1) = 2*y2+x-((us*(x+u))/r1^3)-((u*(x-us))/r2^3);
P(2) = -2*x2+y-((us*y)/r1^3)-((u*y)/r2^3);
end
I keep getting these errors and I don't know why.
Not enough input arguments.
Error in HwOnePFour>@(x,x2,y,y2)orbit(x,x2,y,y2) (line 19)
[TIME, Z] = ode45(@(x,x2,y,y2) orbit(x,x2,y,y2),tspan,IC,options);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in HwOnePFour (line 19)
[TIME, Z] = ode45(@(x,x2,y,y2) orbit(x,x2,y,y2),tspan,IC,options);
Rik on 22 Feb 2021
The documentation clearly states ode45 expects a function with 2 inputs. You will have to modify your anonymous function.

Jan on 22 Feb 2021
ODE45 calls the function to be integrated with 2 inputs: the scalar time t and the vector x.
Definming this function with 4 inputs "x,x2,y,y2" cannot work, because ODE45 cannot guess, what the inputs should be. I assume, instead of these 4 variables, you want to use one vector:
[TIME, Z] = ode45(@orbit,tspan,IC,options);
function [P] = orbit(t, z)
x = z(1);
x2 = z(2);
y = z(3);
y2 = z(4);
... your code of orbit() comes here
end