Are there any other methods to pass value into a predefined function of matlab . (Fmincon problem- passing extra parameters)
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ron Herman
el 18 de Dic. de 2020
Comentada: Stephen23
el 28 de Dic. de 2020
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??
1 comentario
Stephen23
el 28 de Dic. de 2020
The method used by Jan is explained here:
That page is also linked from the fmincon documentation near the top of the page in the info block "Passing Extra Parameters". See also:
Respuesta aceptada
Jan
el 18 de Dic. de 2020
Editada: Jan
el 18 de Dic. de 2020
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 comentarios
Walter Roberson
el 27 de Dic. de 2020
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!