I am trying to optimize the attached problem, but I always receive these errors
Error using objfuntest>@(X)ODEtest(X,g0,Isp,thrust)
Too many input arguments.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in objfuntest (line 12)
[t,x]=ode45(@(X)ODEtest(X,g0,Isp,thrust),tSpan,initial,options);%solve equations for
Optimization
Error in maintest>@(X)objfuntest
Error in fmincon (line 546)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in maintest (line 15)
mfopti=fmincon(myObjective,X0,A,b,Aeq,beq,lb,ub,nonlcon)
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
And I don't get it, because if I don't pass the parameters to my function I get the same error. But that would mean that I have to clear also the input X, so that I don't have any input, which makes no sense to me. Can anyone see my error of reasoning?
Thanks in advance

 Accepted Answer

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)
%constants
ceff=Isp*g0;%[m/s]
propflow=thrust/ceff;%[kg/s]
%differential equations
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;
%Definition of dX
dX=[dX(1);dX(2);dX(3);dX(4);dX(5)];
end
function mp=objfuntest(X)
%constants
thrust=933910;%[N]
g0=9.81;%[m/s^2]
Isp=390;%[s]
ceff=Isp*g0;%[m/s]
propflow=thrust/ceff;%[kg/s]
%solve ODEs
tSpan=[.01 30];
initial=[.01 .01 .01 .01 68e3];%initial gues for x(0),ax(0),y(0),ay(0),m(0)
options=odeset('RelTol',1,'AbsTol',1);
[t,x]=ode45(@(t,X)ODEtest(t,X,g0,Isp,thrust),tSpan,initial,options);%solve equations for Optimization
%Exert information
x1=x(:,1);%first column of the x vector=Position in x direction
x2=x(:,2);%second column of the x vector=Acceleration in x direction
y1=x(:,3);%third column of the x vector=Position in y direction
y2=x(:,4);%fourth column of the x vector=Acceleration in y direction
m=x(:,5);%fifth column of the x vector= Mass
%objective function
alpha=atan(X(3)/X(1));
mp=-propflow*sqrt(2*X(1)*X(5)/(thrust*cos(alpha)));
end
%constraint function
function [c,ceq]=constrainttest(X)
tSpan=[.01 30];
initial=[.01 .01 .01 .01 68e3];%initial gues for x(0),ax(0),y(0),ay(0),m(0)
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);%solve equations for Optimization
%Exert information
x1=x(:,1);%first column of the x vector=Position in x direction
y1=x(:,3);%third column of the x vector=Position in y direction
if y1>=5e5
c=7e5-x1;%sobald die y Koordinate größer gleich 5e5 ist, muss der x Wert größer 7e5 sein
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.

6 Comments

Thank you and I'll try
As always, my pleasure.
What if I only want to optimize m(tfinal) with the control variable alpha? How do I have to change my code?Because I need them as an Input for my Optimization, but I do not want every single of them to be optimized. Although my fmincon needs initial guesses for these values to optimize mfinal with respect to the control variable alpha.
Also how is it possible to just minimize the final value of a variable, because otherwise for the most cases the minimal value will obviously be zero.
Sry for asking again, but there is not a lot information about dynamic Optimization using fmincon without using for example apm Toolboxes and I am completly new to MatLab.
No apologies necessary!
I cannot follow what you are doing with your code. (My objective yesterday was just to get it running.) If you only want to optimise ‘mfinal’, remove the constraints on the other variables.
Also, with respect to ‘alpha’, consider using the atan2 function if its arguments can be negative values:
alpha = atan2(X(3),X(1));
You would have to do that in every function that uses ‘alpha’.
If you have specific questions with respect to optimization, it would be best for you to post them as a new problem, in a new Question. I generally do not code optimization problems often enough to have significant expertise with the optimization functions.
Thanks again, I am going to write a new question.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!