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!