필터 지우기
필터 지우기

How to obtained matris vector solution using fminunc function?

조회 수: 2 (최근 30일)
Alexi
Alexi 2023년 3월 25일
편집: Torsten 2023년 3월 25일
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
Alexi
Alexi 2023년 3월 25일
Thank you for your answer I think I can adapt this approach to my original problem.
Torsten
Torsten 2023년 3월 25일
편집: Torsten 2023년 3월 25일
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개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by