Filter löschen
Filter löschen

Writing an Optimization Problem

1 Ansicht (letzte 30 Tage)
Nouman Khan
Nouman Khan am 23 Mai 2020
Kommentiert: Nouman Khan am 23 Mai 2020
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.

Antworten (2)

Gautam
Gautam am 23 Mai 2020
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 Kommentar
Nouman Khan
Nouman Khan am 23 Mai 2020
Thank you! This was exactly the correction I needed.

Melden Sie sich an, um zu kommentieren.


Gifari Zulkarnaen
Gifari Zulkarnaen am 23 Mai 2020
Bearbeitet: Gifari Zulkarnaen am 23 Mai 2020
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 Kommentar
Nouman Khan
Nouman Khan am 23 Mai 2020
Thank you! This was exactly the correction I needed.

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by