Fmincon: how to change input variable of function which is not a design variable?

1 visualización (últimos 30 días)
Hi all
My question regards the use of fmincon and how to change input variables of i.e. the objective function that are NOT design variables for fmincon. I was wondering if it is possible to have for example a function:
function [L,dLdx] = obj(x,a,b,c,d,changeVariable)
L=a*b*changeVariable*x;
dLdx = a*b*changeVariable;
% Update variable
if changeVariable < c*d
c = c-1;
changeVariable = changeVariable*d
end
end
where as you can see c and changeVariable are being updated every call of obj(), but not passed through. The idea is that this has an effect, so that every call c and changeVariable are being updated and used as input again. I know this is possible in a loop and also using c and changeVariable as output of the function. But this is the problem I'm encountering. I use this function as an objective function for fmincon, which calls this function internally and updates only x and has all 'no design variables' set in stone. It also does not work with more than 2 output arguments.
My question: is there a way to update non design variables that are used as input variables while using fmincon?
Kind regards
Daan

Respuesta aceptada

Jeff Miller
Jeff Miller el 26 de Oct. de 2019
If I follow what you are trying to do, one way is by nesting your objective function within some outer master function, something like this:
function outerMaster()
% initialize these variables however you want.
% the innerObjective function can change them.
changeVariable = 0;
a = 1;
b = 2;
c = 3;
d = 4;
optimalValues = fmincon(innerObjective,startingX,yourConstraints)
function [L,dLdx] = innerObjective(x) % This nested function has access to outerMaster's variables.
L=a*b*changeVariable*x;
dLdx = a*b*changeVariable;
% Update variable
if changeVariable < c*d
c = c-1;
changeVariable = changeVariable*d;
end
end % innerObjective
end % outerMaster

Más respuestas (0)

Categorías

Más información sobre Surrogate Optimization en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by