Borrar filtros
Borrar filtros

fmincon, nonlcon - too many input arguments

5 visualizaciones (últimos 30 días)
e_frog
e_frog el 17 de Dic. de 2020
Comentada: Walter Roberson el 20 de Dic. de 2020
Hi i am trying to minimize some output of a system of 2 ODEs that also are bound by a nonlinear constraint. Unfortunately I cant seem to get my syntax for implementing the nonlcon right.
To clarify what I want to achieve with the nonlnear constraint: The last value of X(2) (a velocity) has to be zero.
I know this is a fairly common question on here, but the other questions and answers couldn't solve my problem. This is my specific code:
x_0 = init_vars();
init_conds_odes = [1 0 5 0];
lb = x_0 - 54;
ub = x_0 + 43;
options = [];
nlcon = @nonlcon;
[H] = fmincon(@objective,x_0,[],[],[],[],lb,ub,@nonlcon,options,init_conds_odes);
These are the functions:
function [x_0,tmax] = init_vars()
m = 2;
n = 3;
o = 4;
tmax = 5;
x_0 = [m;n;o;tmax];
end
function [obj_val,X] = objective(H,init_conds_odes)
tmax = H(4);
tspan_ode = [0 tmax];
[~,X] = ode45(@(t,x) ODEs(t,x,H),tspan_ode,init_conds_odes);
z = max(abs(X(:,2)));
obj_val = z;
end
function [dXdt] = ODEs(~,X,H)
m = H(1);
n = H(2);
o = H(3);
dXdt = [X(2);
X(1)/m+X(3)/n+1;
X(4);
X(3)/o];
end
function [c,ceq] = nonlcon(X)
c = [];
ceq(1) = 0 - X(end,2);
end
This is the error message:
Error using test>nonlcon
Too many input arguments.
Error in fmincon (line 639)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in test (line 13)
[H] = fmincon(@objective,x_0,[],[],[],[],lb,ub,@nonlcon,options,init_conds_odes);
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.

Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Dic. de 2020
nonlcon will be passed t and x. You are not required to make use of t, but it will be passed.
function [c,ceq] = nonlcon(~, X)
  7 comentarios
e_frog
e_frog el 20 de Dic. de 2020
Editada: e_frog el 20 de Dic. de 2020
I cant rewrite it as a boundary value problem, because the initial conditions of the odes [pos(1) vel (1) pos(2) vel(2)] (figuratively) must be met. I tried rewriting my new question from this comment. Perhaps you find the time to help me again. I would really much appreciate it!
Walter Roberson
Walter Roberson el 20 de Dic. de 2020
If those initial conditions must be met, then specify them as part of the boundaries.
Remember that if needed you can add an extra variable to act as an extra layer of differentiation or an extra layer of integration (as needed) in order to be able to put boundary conditions on derivatives or positions.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by