Odefun returns a vector of length 1, but the length of initial conditions vector is 4.
3 views (last 30 days)
Show older comments
Here is the code. For context, I am attempting to simulate a rocket performing a hohmann transfer. Not sure why I am getting the error that the noMoonGrav function returns a vector of length 1 when it clearly returns a column vector of length 4.
%simulate.m
%constants (si base units)
G = 6.674e-11; %universal grav constant
Me = 5.972e24; %mass of earth
Re = 6378e3; %radius of earth
Mm = 7.348e22; %mass of moon
Rm = 1737e3; %radius of moon
Dem = 385000e3; %distance from earth to moon
initialAltitude = 1000e3;
vOrb = sqrt(G*Me / (Re + initialAltitude)); %velocity of circular orbit @1000km altitude
deltaV = 2946; %delta V required for hohmann transfer to moon
%initial state takes the form [xPos, yPos, xVelocity, yVelocity]
intialState = [Re+initialAltitude, 0, 0, vOrb + deltaV];
tspan = [0, 10.01*24*60*60]; %simulate for a time span of 10.01 days
[t1, r1] = ode45(@noMoonGrav, tspan, intialState);
plot(r1(:,1), r1(:,2));
axis equal
function [t, ydot] = noMoonGrav(t, y)
G = 6.674e-11;
Me = 5.972e24;
rad = y(1:2);
rhat = rad/norm(rad);
accel = -((G*Me)/(norm(rad)^2))*rhat;
ydot = [y(3); y(4); accel(1); accel(2)];
end
Here is the error I am getting:
Error using odearguments
NOMOONGRAV returns a vector of length 1, but the length of initial conditions vector is 4. The vector returned by NOMOONGRAV and the initial conditions vector must have the same number of elements.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in simulate (line 15)
[t1, r1] = ode45(@noMoonGrav, tspan, intialState);
0 Comments
Accepted Answer
Torsten
on 1 Jun 2025
Use
function ydot = noMoonGrav(t, y)
instead of
function [t, ydot] = noMoonGrav(t, y)
More Answers (0)
See Also
Categories
Find more on Earth and Planetary Science in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!