Fmincon: how to change input variable of function which is not a design variable?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Jeff Miller
2019-10-26
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!