필터 지우기
필터 지우기

fmincon, nonlcon - too many input arguments

조회 수: 5 (최근 30일)
e_frog
e_frog 2020년 12월 17일
댓글: Walter Roberson 2020년 12월 20일
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.

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 17일
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
e_frog
e_frog 2020년 12월 20일
편집: e_frog 2020년 12월 20일
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 2020년 12월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by