필터 지우기
필터 지우기

Are there any other methods to pass value into a predefined function of matlab . (Fmincon problem- passing extra parameters)

조회 수: 1 (최근 30일)
I am trying out an optimization problem which involves usage of fmincon.
I am using the command in following way ------> [optival,fval] = fmincon(@objfun,x0,A,b,Aeq,Beq,Lb,Ub,@confun)
My question is how to pass additional parameter into confun()
As per matlab syntax for confun() we can only pass one parameter inside it , which is z. But for my problem I need values of L, Lb, Ub to be passed into confun() for my problem to work. Is there any other way I can pass the values inside the confun()
This is how my confun() looks but it can only take Z . Z has decesion variables like a,b so it cannot store other values like Lb , Ub & L . Besides Lb & Ub are passed in fmincon and cannot be used as a parameter for Z.
function [c,ceq] = confun(z)
% z = [a,b]
% assign local variable name for readability
a = z(1);
b = z(2);
% we needto enter L
xe = xa + L*30;
ye = ya + L*20;
% constraints c <= 0
% lower and upper bounds on xa an xb {20<xa<50 ; 30<xb<70} [Thus ub and lb also needs to be added in this function]
c(1) = lb(1) - xa;
c(2) = xa - ub(1);
c(3) = lb(2) - xb;
c(4) = xb - ub(2);
% constraints ceq = 0, you don't have any equality constraints, but
ceq = [];
end
Can the values L, Lb, Ub be passed into it by means of some other function inside it?? Is there any way to approach this??

채택된 답변

Jan
Jan 2020년 12월 18일
편집: Jan 2020년 12월 18일
function [c,ceq] = confun(z,L,Lb,Ub)
...
end
Now create an anonymous function to provide the parameters:
L = ...
Lb = ...
Ub = ...
myCofun = @(z) confun(z,L,Lb,Ub);
fmincon(@objfun,x0,A,b,Aeq,Beq,Lb,Ub, myCofun)
Now fmincon calls the cofun with the single argument z and the other arguments are taken from the definition of the anonymous function.
An alternative but less stable approach is storing the parameters in a persisent variable:
function [c,ceq] = confun(z, inL, inLb, inUb)
persistent L Lb inUb
if nargin > 1
L = inL;
Lb = inLb;
Ub = inUb;
return;
end
... your code
end
Then you define the values and call confun with 4 inputs, while the first is a dummy. The values of the paramters are stored persistently in the function and you can run fmincon with the standard call.
This has the drawback, that any clear command removes the values and you have to care to set the variables reliably before running the optimization. I prefer the anonymous function.
  댓글 수: 6
Walter Roberson
Walter Roberson 2020년 12월 27일
However you can define multiple outputs, and after you have the best x, call the objective function again with the best x and record the additional results
Ron Herman
Ron Herman 2020년 12월 28일
Thank you sir....
Can you give a small example if possible to visualise this better.....??? I dint exactly get the above point

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by