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

 채택된 답변

Torsten
Torsten 2023년 3월 25일
편집: Torsten 2023년 3월 25일
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

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
?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2019a

질문:

2023년 3월 25일

편집:

2023년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by