There are several problems with your code. The error you posted was due to requirement that ODE arguments to the differential equation solvers must allow for in independent variable (usually ‘t’) as the first argument. Correcting those, and adding the ‘alpha’ calculation to ‘objfuntest’ got this much to work:
function dX=ODEtest(t,X,g0,Isp,thrust)
ceff=Isp*g0;
propflow=thrust/ceff;
alpha=atan(X(3)/X(1));
dX(1)=X(2);
dX(2)=(thrust*cos(alpha))/X(5);
dX(3)=X(4);
dX(4)=(thrust*sin(alpha))/X(5);
dX(5)=-propflow;
dX=[dX(1);dX(2);dX(3);dX(4);dX(5)];
end
function mp=objfuntest(X)
thrust=933910;
g0=9.81;
Isp=390;
ceff=Isp*g0;
propflow=thrust/ceff;
tSpan=[.01 30];
initial=[.01 .01 .01 .01 68e3];
options=odeset('RelTol',1,'AbsTol',1);
[t,x]=ode45(@(t,X)ODEtest(t,X,g0,Isp,thrust),tSpan,initial,options);
x1=x(:,1);
x2=x(:,2);
y1=x(:,3);
y2=x(:,4);
m=x(:,5);
alpha=atan(X(3)/X(1));
mp=-propflow*sqrt(2*X(1)*X(5)/(thrust*cos(alpha)));
end
function [c,ceq]=constrainttest(X)
tSpan=[.01 30];
initial=[.01 .01 .01 .01 68e3];
g0=9.81;
Isp=390;
thrust=933910;
options=odeset('RelTol',1,'AbsTol',1);
[t,x]=ode45(@(t,X)ODEtest(t,X,g0,Isp,thrust),tSpan,initial,options);
x1=x(:,1);
y1=x(:,3);
if y1>=5e5
c=7e5-x1;
ceq=[x1-8e5;y1-8e5];
else
c=[];
ceq=[x1-8e5;y1-8e5];
end
end
That eliminates the original errors, and your code now runs. Your ‘maintest.m’ file is unchanged, so I did not post it here.
However the result when I ran it is:
Converged to an infeasible point.
fmincon stopped because the size of the current step is less than
the default value of the step size tolerance but constraints are not
satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
mfopti =
999.9979e+003
4.8819e+006
150.6695e+003
4.8819e+006
69.9999e+003
I leave that to you to resolve.
Best Answer