error in nonlinear system

5 views (last 30 days)
king skiler
king skiler on 7 Nov 2018
Commented: Torsten on 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 ;

Answers (1)

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

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!