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

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

 Réponse acceptée

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

Plus de réponses (0)

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by