error in nonlinear system

3 Ansichten (letzte 30 Tage)
king skiler
king skiler am 7 Nov. 2018
Kommentiert: Torsten am 8 Nov. 2018
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 ;

Antworten (1)

Torsten
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
king skiler
king skiler am 7 Nov. 2018
still not working , because adding matrix wrong in the line of : xnew=xold+y;
Torsten
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 ;

Melden Sie sich an, um zu kommentieren.

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!

Translated by