Odefun returns a vector of length 1, but the length of initial conditions vector is 4.

3 ビュー (過去 30 日間)
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);

採用された回答

Torsten
Torsten 2025 年 6 月 1 日
Use
function ydot = noMoonGrav(t, y)
instead of
function [t, ydot] = noMoonGrav(t, y)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEarth and Planetary Science についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by