Is there any way I can optimize the multi variate function using vector as an input in Matlab?
1 次查看(过去 30 天)
显示 更早的评论
I have one quick question. This is just a sample code I just wrote.
clear;
A = [1;2];
obj = @(Vs,Vx) -Vs.^2 - A.*Vx.^2;
I want to solve the above problem.
I know I can do something like this
clear
a = 1:1:2;
f = @(x) (x-a).^2;
x1 = arrayfun( @(v) fminsearch(@(x) x(1)^2 + v*x(2)^2, [0;0]), a, 'UniformOutput', false);
x1 = [x1{:}]
But I want to use a vector itself as an input.
Is there any way I can solve such problem?
0 个评论
回答(2 个)
Walter Roberson
2021-11-8
No. You have two independent optimization problems, one for when A is 1, and one for when A is 2, and you want to get out a pair of x values for each of those A values. That is a multi-objective optimization, and fminsearch() never supports those.
For multi-objective optimization, you need gamultiobj() or paretosearch(). If you were to do that, you would need to make your input vector x of length 2 times the number of values in A. And both of those routines would waste time trying to optimize the variables against each other, as they would not know that the variable pairs were independent.
0 个评论
Matt J
2021-11-8
编辑:Matt J
2021-11-8
You can embed many small optimization problems in a larger problem by summing the objectives, like in the example below. However, for it to be efficient, this approach demands at minimum that you supply your own gradient calculation code, one that leverages the separability of the objectives.
Also, even with this, it can be disadvantageous, because the algorithm will stop iterating only once it has judged that all sub-problems have converged sufficiently. If you were to solve the sub-problems separately, the solver could choose the stopping point adaptively for each separate problem.
a = 1:3;
n=numel(a);
opts=optimoptions('fminunc','SpecifyObjectiveGradient',true);
x=fminunc( @(x) Objectives(x,a) ,zeros(2,n),opts)
function [fval,grad]=Objectives(x,a)
fval= sum( (x(1,:)-a).^2 + (x(2,:)-2*a).^2 ,'all')/2;
if nargout>1 %Gradient requested
grad=x-[a;2*a];
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!