[R] Matlab inv() and R solve() differences

Berwin A Turlach berwin at maths.uwa.edu.au
Fri Jan 30 05:03:48 CET 2009


G'day all,

On Thu, 29 Jan 2009 19:24:40 -0700
Kingsford Jones <kingsfordjones at gmail.com> wrote:

> I suppose the solution is unstable because x is ill-conditioned:

While, as you show, x is ill-conditioned, I do not believe that this is
serious enough to explain the differences that Pat sees between MATLAB
and R.  

In fact, on one of our lab machines MATLAB Version 7.5.0.342 (R2007b),
August 15, 2007, yields the following result:

>> x=[0.133 0.254 -0.214 0.116; 0.254 0.623 -0.674 0.139; -0.214 -0.674  0.910 0.011 ; 0.116 0.139 0.011 0.180]

x =
    0.1330    0.2540   -0.2140    0.1160
    0.2540    0.6230   -0.6740    0.1390
   -0.2140   -0.6740    0.9100    0.0110
    0.1160    0.1390    0.0110    0.1800

>> inv(x)  

ans =
  261.9426  116.2219  150.9174 -267.7793
  116.2219  344.3029  286.6735 -358.2959
  150.9174  286.6735  252.9553 -334.0920
 -267.7793 -358.2959 -334.0920  475.2252

which is consistent with the output of R's solve().  

But the matrix x is ill-conditioned enough for small changes in the
precision of the entries leading to big differences in the calculated
inverse matrix.

My guess is that Pat was not completely truthful in the description of
what he did.  Presumably, x was not submitted to MATLAB in the given
form but calculated (to a higher precision) in MATLAB before being fed
to MATLAB's inv() command.  Then x was transferred with a lower
precision (less significant digits) to R and submitted there to R's
solve() function. 

Thus coming back to Pat's original question:

> On Thu, Jan 29, 2009 at 7:00 PM, Joseph P Gray <jpgray at uwm.edu> wrote:
> > [...]
> > Is there a way to obtain the MATLAB result in R?

Presumably yes.  Feed x in the same precision to R's solve() function
as you feed it to MATLAB's inv() function.

HTH.

Cheers,

	Berwin

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba




More information about the R-help mailing list