error in nonlinear system
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
please ! can you detect the errors of this code? and how i can get the answer correctly!
fun = @(x) [x(1) ^ 2 + x(2) ^ 2 + x(3) ^ 2 - 1; ...
x(1) ^ 2 + x(3) ^ 2 - 0.25; ...
x(1) ^ 2 + x(2) ^ 2 + 4 * x(3)];
J = @(x) [ 2 * x(1) + 2 * x(2) + 2 * x(3) ; ...
2 * x(1) + 2 * x(3); ...
2 * x(1) + 2 * x(2) - 4 ];
x0 = [1;1;1]'; tol= 0.00001; maxit= 10;
xold = x0; iter=1;
while (iter<= maxit)
y= -feval(J,xold)\ feval(f,xold);
xnew=xold + y';
dif = norm(xnew-xold);
disp(iter); disp(xnew); disp(dif);
if dif <= tol
x=xnew ;
disp (' Newton method has converged '), return;
else
xold= xnew;
end
iter = iter + 1;
disp(' itre xnew dif')
end
disp('Newton method did not converge')
x = xnew ;
0 commentaires
Réponses (1)
Torsten
le 7 Nov 2018
Modifié(e) : Torsten
le 7 Nov 2018
Why is your Jacobian (3x1) and not (3x3) ?
And replace
y = -feval(J,xold)\ feval(f,xold);
by
y = -J(xold)\fun(xold)
Best wishes
Torsten.
2 commentaires
Torsten
le 8 Nov 2018
For me, it works:
fun = @(x) [x(1) ^ 2 + x(2) ^ 2 + x(3) ^ 2 - 1; ...
x(1) ^ 2 + x(3) ^ 2 - 0.25; ...
x(1) ^ 2 + x(2) ^ 2 + 4 * x(3)];
J = @(x) [ 2 * x(1) , 2 * x(2) , 2 * x(3) ; ...
2 * x(1),0, 2 * x(3); ...
2 * x(1) , 2 * x(2), 4 ];
x0 = [1;1;1]'; tol= 0.00001; maxit= 100;
xold = x0; iter=1;
while (iter<= maxit)
y= -J(xold)\ fun(xold);
xnew=xold + y';
dif = norm(xnew-xold);
disp(iter); disp(xnew); disp(dif);
if dif <= tol
x=xnew ;
disp (' Newton method has converged '), return;
else
xold= xnew;
end
iter = iter + 1;
disp(' itre xnew dif')
end
disp('Newton method did not converge')
x = xnew ;
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!