Why does lsqnonlin find wrong coefficients?

3 ビュー (過去 30 日間)
azarang asadi
azarang asadi 2022 年 3 月 21 日
回答済み: Torsten 2022 年 3 月 21 日
I'm trying to learn lsqnonlin and I just tried an example and it failed to find the right answer.
Here's my example:
X = rand(16,4);
a = 1; b = 2; c = 3; d = 4;
y = a * X(:,1) + b * X(:,2) + c * X(:,3) + d * X(:,4);
guess= [1 1 1 1];
co = lsqnonlin(@(x0)myfun(x0,X,y), guess);
function diff = myfun(x,X,y)
diff = (x(1) *X(1) + x(2) * X(2) + x(3) * X(3) + x(4) * X(4)) -y;
end
although the right a b c and d are 1 2 3 4, what I got was 6.13724694019609 -2.84149544942237 8.32804249499245 3.26966946738081. Is this because of local minimum or is that because my code is doing something wrong?
P.S. I don't wanna use \ or mldivide.

採用された回答

Torsten
Torsten 2022 年 3 月 21 日
diff = (x(1) *X(:,1) + x(2) * X(:,2) + x(3) * X(:,3) + x(4) * X(:,4)) - y;
instead of
diff = (x(1) *X(1) + x(2) * X(2) + x(3) * X(3) + x(4) * X(4)) -y;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by