How to obtained matris vector solution using fminunc function?

1 回表示 (過去 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 Center および File ExchangeSolver 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