error in nonlinear system
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Torsten
am 7 Nov. 2018
Bearbeitet: Torsten
am 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 Kommentare
Torsten
am 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 ;
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!