Why does lsqnonlin find wrong coefficients?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
azarang asadi
le 21 Mar 2022
Réponse apportée : Torsten
le 21 Mar 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.
0 commentaires
Réponse acceptée
Torsten
le 21 Mar 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;
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!