Why does lsqnonlin find wrong coefficients?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!