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
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.
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).