Why does lsqnonlin find wrong coefficients?

1 visualización (últimos 30 días)
azarang asadi
azarang asadi el 21 de Mzo. de 2022
Respondida: Torsten el 21 de Mzo. de 2022
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.

Respuesta aceptada

Torsten
Torsten el 21 de Mzo. de 2022
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;

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by