0
votes

When I use newtons method to find the roots of a system of equations I get the following error message:

"Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.625479e-33. > In new (line 16)"

Any ideas why I get this error message? I know about singular matrices and the inverse thing but could this really have to do anything with that? If so, how? What difference can I make to the code? It says line 16, which is dx = -J\f;. But I just follow along with my numerical methods textbook. Something must be wrong, but the exercise says "use Newtons Method" so I guess this should work. I hope someone can help me.

x = [0 0 pi/2]';
maxiter = 10;
iter = 0;
dxnorm = 1;
results = cell(maxiter + 1, 2); % Preallocate results array

while dxnorm > 0.5e-4 && iter <= maxiter
    f = [cos(x(1)) + cos(x(2))    + cos(x(3))-2; ...
         sin(x(1)) + sin(x(2))    + sin(x(3)); ...
         tan(x(1)) - 2.*tan(x(2)) + tan(x(3)); ...
         ];
    J = [-sin(x(1)),       -sin(x(2)),          -sin(x(3)); ...
         cos(x(1)),        cos(x(2)),           cos(x(3)); ...
         tan(x(1)).^2 + 1, -2*tan(x(2)).^2 - 2, tan(x(3)).^2 + 1 ...
         ];
    dx = -J\f;
    results{iter + 1, 1} = x;
    x = x + dx;
    dxnorm = norm(dx,inf);
    results{iter + 1, 2} = dxnorm;
    iter = iter + 1;
end
x, iter
1
Its probably because the derivative is close to zero, and Newtons method fails in those cases. en.wikipedia.org/wiki/Newton%27s_method#Derivative_issuesAnder Biguri
What you should do, is find bad cases, and there, just use other method, i.e. Bisection method. So when J~=0 then use other method.Ander Biguri
The way you wrote it, yes. J\f is syntax sugar for inv(J)*f. If J now becomes singular then inv(J) doesn't exist, thus the error. You can try pinv(J-lambda*eye(size(J))) where lambda is some relaxation parameter instead.user2457516
@FirefoxMetzger Not really. J\f solfes Ax=b problem with the rigth choice of algorithm.Ander Biguri
Is the problem ill posed? tan(x(3)) appears in f and J; x(3)=pi/2 to start; tan(pi/2)=InfGeoff

1 Answers

2
votes

Your initial conditions of x(3) = pi/2 leads the 3rd entry of f to become infinite because tan(pi/2) = sin(pi/2)/cos(pi/2) = inf, except it's not quite infinity because of floating point imprecision, imprecision in pi etc... so instead you just get an insanely large number.

Now you have insanely large numbers along with insanely small numbers, and everything basically gets !@#$ed. Your Jacobian matrix is badly scaled etc...

The linear equation which blows up is:

[0,    0,   -1                                       [x1      [0
 1,    1,    0                                    *   x2   =   1
 1,   -2,   266709378811357100000000000000000 ]       x3]      16331239353195370]

These are !@#$ed up conditions for numerically solving a linear system.

what should you do?

Start somewhere sane. Eg. start from initial conditions [0,0,pi/4] and everything might work fine.

Some initial conditions also trigger the multivariate equivalent of the derivative being zero (which will also make Newton's method blow up).