Is there any way I can optimize the multi variate function using vector as an input in Matlab?

1 Ansicht (letzte 30 Tage)
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?

Antworten (2)

Walter Roberson
Walter Roberson am 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.

Matt J
Matt J am 8 Nov. 2021
Bearbeitet: Matt J am 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)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 2×3
1.0000 2.0000 3.0000 2.0000 4.0000 6.0000
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

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by