Is there any way I can optimize the multi variate function using vector as an input in Matlab?
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Walter Roberson
on 8 Nov 2021
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 Comments
Matt J
on 8 Nov 2021
Edited: Matt J
on 8 Nov 2021
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 Comments
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!