How to obtained matris vector solution using fminunc function?

c_1=[3;5;8];
c_2=[2;6;8];
fun = @(x) (x(1)-c_1).^2 + (x(2)-c_2).^2; % cost function
x0 = [[0.1;0.1;0.1],[0.1;0.1;0.1]]; % İnitial Gues
[x,fval] = fminunc(fun,x0) % Results

 Risposta accettata

You will have to use fminunc three times:
c_1=[3;5;8];
c_2=[2;6;8];
x1_sol = zeros(size(c_1));
x2_sol = zeros(size(c_1));
for i=1:numel(c_1)
fun = @(x) (x(1)-c_1(i)).^2 + (x(2)-c_2(i)).^2; % cost function
x0 = [0.1,0.1]; % İnitial Guess
[x,fval] = fminunc(fun,x0); % Results
x1_sol(i) = x(1);
x2_sol(i) = x(2);
end
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x1_sol
x1_sol = 3×1
3.0000 5.0000 8.0000
x2_sol
x2_sol = 3×1
2.0000 6.0000 8.0000
Or do you try to solve a different problem ?

2 Commenti

Thank you for your answer I think I can adapt this approach to my original problem.
Maybe you mean
c_1=[3;5;8];
c_2=[2;6;8];
fun = @(x)sum((x(1)-c_1).^2 + (x(2)-c_2).^2); % cost function
x0 = [0.1,0.1]; % İnitial Guess
[x,fval] = fminunc(fun,x0) % Results
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 1×2
5.3333 5.3333
fval = 31.3333
?

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2019a

Richiesto:

il 25 Mar 2023

Modificato:

il 25 Mar 2023

Community Treasure Hunt

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

Start Hunting!

Translated by