Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Warning: Matrix is singular to working precision.

1,389 views
Skip to first unread message

Diego

unread,
Jan 3, 2009, 3:20:02 PM1/3/09
to
hello folks, first , my english is very bad, so, i'm sory about that.

i creat a program that find a intersection in two nonlinear equations...
but, i just find one of them, the other have a problem:
"Warning: Matrix is singular to working precision."
the code :

main.m
function main (a,b)
% newton method for systems
% main part
echo off all;
format long;
x = [[a]; [b]];


err = 10^(-6);
nmax = 1000;
xold = x;
for k=1:nmax
x = x - inv(df(x))*f(x);

if (norm(x-xold,inf)< err )
fprintf('precis?o alcan?ada em %3d itera??es\n', k);
fprintf('x1=%10.6f, x2=%10.6f\n', x(1), x(2));
break
end
xold = x;
end
if (k==nmax)
fprintf('precis?o n?o alcan?ada em %3d itera??es\n', nmax);
end
%gr?fico

figure(1);
clf;
hold on;
whitebg('w');
xlabel ('x');
ylabel('y');
grid;
axis([ -5 5 -5 5 ]);
xx1 = double(x(1));
xx2 = double (x(2));

t= 0:0.025:5;
u= 0:0.025:2;
y1 = eval('log(exp(t)-1)');
y2 = (1024-u.^10).^(1/10);
y3= (1024-u.^10).^(1/10)*-1;

plot (t,y1,'r',xx1,xx2,'ob',u,y2,'b',u,y3,'b');

plot([-5 5],[0 0],'k',[0 0],[-5 5],'k' );
hold off;

% end of main part

f.m
%onde x1 = x e x2 = y
function y = f(x)
y(1,1) = exp(x(1)) - exp(x(2)) - 1;
y(2,1) = x(1)^10 + x(2)^10 - 1024;
% end of f(x)

df.m
function df = df(x)
df(1,1)= exp(x(1)); df (2,1) = exp(x(2));
df(2,1)= 10*x(1)^9; df(2,2) = 10*x(2)^9;

Rune Allnor

unread,
Jan 3, 2009, 3:26:50 PM1/3/09
to
On 3 Jan, 21:20, "Diego " <diego...@hotmail.com> wrote:
> hello folks, first , my english is very bad, so, i'm sory about that.
>
> i creat a program that find a intersection in two nonlinear equations...
> but,  i just find one of them, the other have a problem:
> "Warning: Matrix is singular to working precision."

This means that there is a problem with the condition number
of some matrix in your code. The matrix is effectively non-
invertible, so whatever expression it appears in can not be solved.

This is a severe error - it *might* in fact mean that what you
intend to do is impossible, although in some cases work-arounds
are possible.

The way to handle this is to find out exactly what variable and
expression causes the rror, and analyze the situation to find out
if a work-around is possible.

Rune

Diego

unread,
Jan 3, 2009, 3:30:04 PM1/3/09
to
i test two intervals:

main (0,-2); <- i find the correct value
main (0,2); <-problem

i'm using the newtons metlhod to solve this problem


"Diego " <dieg...@hotmail.com> wrote in message <gjoh9i$l9u$1...@fred.mathworks.com>...

Diego

unread,
Jan 3, 2009, 3:35:03 PM1/3/09
to
when i put in matlab:
main(0,-2)
x1= 0.126928, x2= -2.00000 (correct)

main (0,2), i have a problem(wrong)

i have a doubt, if the inv funcition have cause this problem....

Diego

unread,
Jan 3, 2009, 10:00:04 PM1/3/09
to
i found a easy solution to my problem,

http://www.mathworks.com/support/solutions/data/1-15NRJ.html?product=OP&solution=1-15NRJ

Optimization Toolbox
the code:

test2.m

function test2 (a,b)
F = inline ('[exp(V(1))-exp(V(2))-1 ; V(1)^10 + V(2)^10 - 1024 ]','V');
InitialGuess = [a;b];
Options = optimset ('Display', 'iter');
XY = fsolve (F, InitialGuess, Options);
ShouldBeZero = F(XY);


figure(1);
clf;
hold on;
whitebg('w');
xlabel ('x');
ylabel('y');
grid;

ezplot('exp(x)-exp(y) = 1');
ezplot('x^10 + y^10 = 1024');
plot(XY(1), XY(2), 'ro');
hold off;

i have read in manual, that the Optimization Toolbox implements newton method, but when i use the raw algorithm, i had problems to find the solution in interval (0,2) , the famous problem called in inv function:
"Warning: Matrix is singular to working precision.", it's very strange, does anybody have this code implemented ?, i'd like to compare with my code.

Tom mcmurray

unread,
Mar 20, 2009, 7:42:01 PM3/20/09
to
"Diego " <dieg...@hotmail.com> wrote in message <gjp8nk$732$1...@fred.mathworks.com>...

hi diego:

you likely have figured out that your jacobian, df, is nearly singular around the origin. any steps that result in an root iteration in the rectangular region, -0.02<x,y<0.02, approximately, produce a jacobian that makes matlab unhappy upon attempted inversion. i tried (x,y)=(0.02,0.02) and got:

jacobian = [1.02 -1.02;5e-15 5e-15]

with an inverse:

inv(jacobian) = [0.49 9.77e13;-0.49 9.77e13]

so, gradient optimizations are typically sensitive to initial root selection and fail for singular jacobians, as you have demonstrated. thanx for the interesting objective function.

tom

murtyd...@gmail.com

unread,
Mar 27, 2013, 10:13:06 AM3/27/13
to Diego
Please suggest some solution for good precision....It's better to round of the decimals to required one...
0 new messages