MATLAB: Trying to solve 4 coupled differential equations using ode45 and keep getting the error not enough input arguments

coupled differential equationsode45

function Q2a
m0 = 120000;
n = 15;
mf = m0/n;
Isp = 320;
g = 9.81;
T = 1.4*m0*g;
me = T/(Isp*g);
t0 = 0;
tb = (m0 - mf)/me;
tspan = [t0 tb];
gamma0 = 1.568;
m = [m0 mf];
ht = 110;
function dvdt = velocity(t,g,T,m,gamma0,ht,littlegam)
while alt <= ht
dvdt = (T - m*g*sin(gamma0))/m ;
end
while alt > ht
dvdt = (T - m*g*sin(littlegam))/m;
end
end
function [t,v] = vel(tspan,g,T,m,gamma0,ht,littlegam,t)
v0 = 0;
[t,v] = ode45(@(t,m) velocity(t,g,T,m,gamma0,ht,littlegam) ,tspan , v0);
output(t,v)
end
function dhdt = altitude(t,vel,littlegam,ht,gamma0)
v = vel;
while ht >= alt
dhdt=v.*sin(gamma0);
end
while alt > ht
dhdt = v.*sin(littlegam);
end
end
function [t,h] = alt(tspan, vel, littlegam, ht, gamma0)
h0 = 0;
[t,h] = ode45(@(t,vel) altitude(t,vel,littlegam,ht,gamma0) , tspan, h0);
end
function dgammadt = pitch_angle(t,g0,littlegam,vel,Re,alt,gamma0,ht)
h = alt;
v = vel;
while ht >= alt
dgammadt=(-(1./v)).*(g0-((v.^2)./(Re+h))).*(cos(gamma0));
end
while alt > ht
dgammadt=(-(1./v)).*(g0-((v.^2)./(Re+h))).*(cos(littlegam));
end
end
function [t,gamma] = littlegam(g0,alt,Re,vel,gamma0,tspan,ht)
[t,gamma] = ode45(@(t,vel) pitch_angle(t,g0,littlegam, vel, Re, alt, gamma0, ht) , tspan, gamma0);
end
function dxdt = downrange_dist(Re,alt,vel,littlegam,ht,gamma0,t)
while ht >= alt
dxdt=(Re./(Re+ h)).*v.*cos(gamma0);
end
while alt > ht
dxdt=(Re./(Re+ alt)).*vel.*cos(littlegam);
end
end
function [t,x] = dis(Re,alt,vel,littlegam,tspan,ht,gamma0)
x0 = 0;
[t,x] = ode45(@(t,alt) downrange_dist(Re,alt,vel,littlegam,ht,gamma0,t), tspan, x0);
end
plot(tspan,vel);
plot(tspan,alt);
plot(tspan,littlegam);
plot(tspan,dis);
end

Best Answer

Please post the complete error message. This would reveal the failing line and is much better than letting the readers guess, where the code fails.
As far as I can see, the code has a general flaw. E.g.:
while alt <= ht
dvdt = (T - m*g*sin(gamma0))/m ;
end
This is an infinite loop, because alt is not changed inside it. Maybe you mean if alt <= ht.
The actual error occurs in this line:
plot(tspan, vel)
Here the function vel() is called without inputs, but actually your require:
vel(tspan,g,T,m,gamma0,ht,littlegam,t)
I do not understand the purpose of the code. But usually coupled ODEs are handled very differently. Please read the documentation again:
doc ode45 % See the examples there
doc while
Note that your function to be integrated seems to have a discontinuity at ht=alt, but Matlab's ODE integrators handle smooth functions only, see http://www.mathworks.com/matlabcentral/answers/59582#answer_72047. Use an event function instead to stop the integration at this ht value and restart it with the modified formula.