MATLAB: Nested function causes MATLAB crash (2011a)

matlab function

The code is shown below. Do I have too many global variables? Am I missing something here?
function slslaunch()
%dy = zeros(5,1);
%units
tonforce = 9806.65; %convert to newtons
tons = 1/0.00110231; %convert to kg
%number of engines
N_srb = 2;
N_rs25 = 4;
Isp_srb = 250;
Isp_rs25 = 400; %this is sort of an average
Isp_j2x = 451.5;
T_srb = 1.428*tonforce*N_srb;
T_rs25 = 830*tonforce; %includes 4 rs25's keyboard
T_j2x = 11.2*tonforce;
taub_srb = 128.4; %seconds
taub_rs25 = 476;
taub_j2x = 377;
tau_orbit = 8000; %that sounds about like an orbit, right?
mass_srb_tank = 100.39*tons*N_srb;
mass_srb_fuel = 650.87*tons*N_srb;
mass_rs25_tank = 89.38*tons;
mass_rs25_fuel = 1002.5*tons;
mass_j2x_tank = 22.063*tons;
mass_j2x_fuel = 207.69*tons;
%mass flow rates
mdot_srb = mass_srb_fuel/ taub_srb;
mdot_rs25 = mass_rs25_fuel/taub_rs25;
mdot_j2x = mass_j2x_fuel/ taub_j2x;
mass_interstage = 0*tons;
mass_payload_fairing = 5.638*tons;
mass_payload = 70*tons;
%initial mass
M0 = mass_srb_tank + mass_srb_fuel + ...
mass_rs25_tank + mass_rs25_fuel + ...
mass_j2x_tank + mass_j2x_fuel + ...
mass_interstage + mass_payload_fairing + mass_payload;
%initial conditions
g0 = 9.81;
h0 = 6378e3;
V0 = 0;
gam0 = (90-.01)/180*pi;
x0 = 0;
theta0 = 0;
[t1,y1] = ode45(@launch,[0,taub_srb],[V0,M0,h0,gam0,theta0]);
plot(t1,y1(:,1),'-',t1,y1(:,2),'-.',t1,y1(:,3),'.')
% %y1 = V velocity
% y2 = M total mass
% y3 = h
% y4 = gamma
% y5 = theta
function dy = launch(t,y)
dy = zeros(5,1);
g = g0*(h0/y(3))^2;
dy(1) = ((T_srb + T_rs25)/y(2))-g*sin(y(4)); %(Fsrb + Frs25(main engine))
dy(2) = mdot_srb+mdot_rs25;
dy(3) = y(1)*sin(y(4));
dy(4) = ((-g*cos(y(4))/y(1))+y(1)*cos(y(4))/y(3))*(y(4)/pi*180>80); %controls gamma
dy(5) = y(1)*cos(y(5))/y(3);
[t1,y1] = ode45(@launch,[0,taub_srb],[V0,M0,h0,gam0,theta0]);
plot(t1,y1(:,1),'-',t1,y1(:,2),'-.',t1,y1(:,3),'.')
%[t2,y2] = ode45(@launch2,[taub_srb,t3],[y(;,end)]);

end
end
%[t,y] = ode45(@launch,[0,taub_srb],[V0,M0,h0,gam0,theta0]);
%[t2,y2] = ode45(@launch2,[taub_srb,t3],[y(;,end)]);
%need to call ode45 three times for each seperation last output of previous
%ode45 will be initial condition of next ode45

Best Answer

Hi,
the problem is a recursion with calling ode45:
You call
[t1,y1] = ode45(@launch,[0,taub_srb],[V0,M0,h0,gam0,theta0]);
in the main function.
Inside function launch you call ode45 again:
[t1,y1] = ode45(@launch,[0,taub_srb],[V0,M0,h0,gam0,theta0]);
This leads to infinite recursion. Remove the ode45 and plot line from the launch function.
Titus
PS: MATLAB should not crash but give an error "Maximum recursion limit reached"...?