A system of linear equations such that the backslash matlab and numpy.linalg.solve create different solutions

I have the following problem using numpy 1.3.0 and MATLAB 7.9.0: python code

import numpy as np    
Lu = [[1.01250000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-0.00250000000000000,1.01250000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,-0.00250000000000000,1.01250000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,-0.00250000000000000,1.01250000000000,0,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0],[-0.00250000000000000,0,0,0,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0],[0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0],[0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0],[0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,0,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0],[0,0,0,0,-0.00250000000000000,0,0,0,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0],[0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0],[0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0],[0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,0,0,0,-0.00250000000000000,0,0,0,0],[0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,0,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0],[0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0],[0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0],[0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,0,0,0,-0.00250000000000000],[0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,0,1.01250000000000,-0.00250000000000000,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01250000000000,-0.00250000000000000,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01250000000000,-0.00250000000000000],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01250000000000]]
rhs = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0,0, 0,  0, 0, 0.0050, 0.0050, 0.0050, 0.0050]

Lu = np.array(Lu)
rhs = np.array(rhs)
ans = np.linalg.solve(Lu,rhs)
print ans

      

outputs output

[  1.87241716e-13   1.89545264e-13   1.89545264e-13   1.87241716e-13
   7.56433496e-11   7.63890449e-11   7.63890449e-11   7.56433496e-11
   3.04833369e-08   3.07089522e-08   3.07089522e-08   3.04833369e-08
   1.22844835e-05   1.23451480e-05   1.23451480e-05   1.22844835e-05
   4.95055571e-03   4.96277946e-03   4.96277946e-03   4.95055571e-03]

      

whereas the backslash in MATLAB produces the output

 0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0050    0.0050    0.0050    0.0050

      

I have not found any other linear algebraic equation system where numpy and matlab produce different solutions. I am currently out of town and therefore cannot check if another variant of numpy (on another computer) will give the correct result. Is np.linalg.solve the wrong function to use this system (Lu system matrix is ​​sparse)? Is this a bug in my version of numpy? Are there any problems in my code?

Thank!

+3


source to share


1 answer


Actually, they are probably the same solution. MATLAB rounds to 0.0000 (when printing at least), while python gives you much more verbose numbers (some problems can come from floating point rounding errors). The only numbers that appear as 0.0050

are those that are e-03

. All other numbers are less than 0.0005, so it can be rounded to the nearest 0.0000.



+3


source







All Articles