Borrar filtros
Borrar filtros

How to obtained matris vector solution using fminunc function?

2 visualizaciones (últimos 30 días)
Alexi
Alexi el 25 de Mzo. de 2023
Editada: Torsten el 25 de Mzo. de 2023
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

Respuesta aceptada

Torsten
Torsten el 25 de Mzo. de 2023
Editada: Torsten el 25 de Mzo. de 2023
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 comentarios
Alexi
Alexi el 25 de Mzo. de 2023
Thank you for your answer I think I can adapt this approach to my original problem.
Torsten
Torsten el 25 de Mzo. de 2023
Editada: Torsten el 25 de Mzo. de 2023
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
?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by