필터 지우기
필터 지우기

Writing an Optimization Problem

조회 수: 1 (최근 30일)
Nouman Khan
Nouman Khan 2020년 5월 23일
댓글: Nouman Khan 2020년 5월 23일
I am trying to solve a non-linear optimization problem, i.e. to maximize
subject to .
I have made a separate function file nlconstraints with the following content.
function [c,ceq] = nlconstraints(x,y)
c(1)=1-x;
c(2)=1-y;
c(3)=y-x;
ceq=[];
end
I have the following code in my original file (which is in the same folder as the function file)
clear all
clc
fun = @(x,y)-x*(1+2*y)*exp(-y*x^(0.9));
nonlcon = @nlconstraints;
x0 = [1.12 1];
A = []; % No other constraints
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
I am getting the following errors.
Not enough input arguments.
Error in max_drift_non_rare (line 50)
fun = @(x,y)-x*(1+2*y)*exp(-y*x^(0.9));
Error in fmincon (line 562)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in max_drift_non_rare (line 59)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Would someone kindly help. Thank you.

답변 (2개)

Gautam
Gautam 2020년 5월 23일
I know you want to find optimal values of x&y, but while using fmincon or any of the optimization routines, you should pass only one input argument to an objective function.
Replace:
fun = @(x,y)-x*(1+2*y)*exp(-y*x^(0.9)); % Your original code
with
fun = @(x) -x(1)*(1+2*(x(2)))*exp(-x(2)*(x(1))^0.9); % Where MATLAB will assume x(1) is x and x(2) is y;
Similarly change the constraint function 'nlconstraints' to take in one input argument 'x' and in your function body rewrite y as x(2), and x as x(1). For ex: Replace c(1)=1-x with c(1)=1-x(1);
  댓글 수: 1
Nouman Khan
Nouman Khan 2020년 5월 23일
Thank you! This was exactly the correction I needed.

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


Gifari Zulkarnaen
Gifari Zulkarnaen 2020년 5월 23일
편집: Gifari Zulkarnaen 2020년 5월 23일
I am not sure why yours doesnt work, but it works if the functions are written in array form like this:
clear
fun = @(x) (x(1)*(1+2*x(2))*exp(-x(2)*x(1)^(0.9));
x0 = [1.12 1];
x = fmincon(fun,x0,[],[],[],[],[],[],@nlconstraints);
function [c,ceq] = nlconstraints(x)
c(1)=1-x(1);
c(2)=1-x(2);
c(3)=x(2)-x(1);
ceq=[];
end
  댓글 수: 1
Nouman Khan
Nouman Khan 2020년 5월 23일
Thank you! This was exactly the correction I needed.

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

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by