无法使用numpy求解方程组

时间:2012-02-13 17:15:12

标签: python numpy

当我试图使用numpy.linalg.solve(A,b)解决这个18方程式和18个未知数的方程组时,它显示出如下错误:

>>numpy.linalg.solve(A,b)
  File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 328, in solve
    raise LinAlgError, 'Singular matrix'
LinAlgError: Singular matrix`

另外,与matlab相比,这给出了错误的答案,并且在matlab中反向不会像奇异矩阵那样引起任何误差

>>numpy.linalg.lstsq(A,b) 

等式:

A=[[  2.00000000e-05   1.04829508e-14   2.92236138e-24   4.99137993e-34
    5.52810194e-44   4.00873469e-54   1.84523284e-64   4.90835508e-75
    5.76100393e-86   5.99076740e-01   2.99538370e-01   2.24653778e-01
    1.87211481e-01   1.63810046e-01   1.47429042e-01   1.35143288e-01
    1.25490196e-01   1.17647059e-01],
 [  3.73770292e-01   2.09016082e-01   6.78532034e-02   1.39582215e-02
    1.89003753e-03   1.68885303e-04   9.62295375e-06   3.17769127e-07
    4.46104746e-09   2.20133438e-01   3.43958497e-01   3.09132700e-01
    2.41012794e-01   1.94347888e-01   1.66650637e-01   1.48719036e-01
    1.35807777e-01   1.25855315e-01],
 [  3.11663896e-01   3.06638455e-01   2.02332733e-01   8.94385710e-02
    2.66813026e-02   5.32020127e-03   6.81398496e-04   5.08044167e-05
    1.67948485e-06   7.07545654e-02   2.07209799e-01   2.96555487e-01
    2.92571675e-01   2.43843879e-01   1.98570296e-01   1.68671368e-01
    1.49520047e-01   1.36137091e-01],
 [  2.44042188e-01   2.93174990e-01   2.81499972e-01   1.97504125e-01
    9.74752167e-02   3.28394798e-02   7.18929918e-03   9.22549849e-04
    5.27171340e-05   1.90856412e-02   9.06567959e-02   1.99802807e-01
    2.77189118e-01   2.83255891e-01   2.43800895e-01   2.00174117e-01
    1.69378295e-01   1.49649602e-01],
 [  1.97866831e-01   2.44427869e-01   2.83430648e-01   2.71206641e-01
    1.95516338e-01   9.97711554e-02   3.37413845e-02   6.76442793e-03
    6.08038466e-04   4.05196947e-03   2.95793771e-02   9.79563619e-02
    1.96206492e-01   2.69960187e-01   2.80370309e-01   2.43866828e-01
    1.99997042e-01   1.68723418e-01],
 [  1.68723418e-01   1.99997042e-01   2.43866828e-01   2.80370309e-01
    2.69960187e-01   1.96206492e-01   9.79563620e-02   2.95793772e-02
    4.05196947e-03   6.08038465e-04   6.76442792e-03   3.37413845e-02
    9.97711553e-02   1.95516338e-01   2.71206641e-01   2.83430648e-01
    2.44427869e-01   1.97866831e-01],
 [  1.49649602e-01   1.69378295e-01   2.00174117e-01   2.43800894e-01
    2.83255891e-01   2.77189118e-01   1.99802807e-01   9.06567959e-02
    1.90856413e-02   5.27171338e-05   9.22549847e-04   7.18929917e-03
    3.28394797e-02   9.74752166e-02   1.97504125e-01   2.81499972e-01
    2.93174990e-01   2.44042188e-01],
 [  1.36137091e-01   1.49520047e-01   1.68671367e-01   1.98570296e-01
    2.43843879e-01   2.92571675e-01   2.96555487e-01   2.07209799e-01
    7.07545654e-02   1.67948484e-06   5.08044165e-05   6.81398494e-04
    5.32020126e-03   2.66813026e-02   8.94385709e-02   2.02332733e-01
    3.06638455e-01   3.11663896e-01],
 [  1.25855315e-01   1.35807777e-01   1.48719036e-01   1.66650637e-01
    1.94347888e-01   2.41012794e-01   3.09132700e-01   3.43958498e-01
    2.20133438e-01   4.46104727e-09   3.17769134e-07   9.62295367e-06
    1.68885302e-04   1.89003752e-03   1.39582215e-02   6.78532033e-02
    2.09016082e-01   3.73770292e-01],
 [  2.00000000e-05   1.04829508e-14   2.92236138e-24   4.99137993e-34
    5.52810194e-44   4.00873469e-54   1.84523284e-64   4.90835508e-75
    5.76100393e-86   5.99076740e-01   2.99538370e-01   2.24653778e-01
    1.87211481e-01   1.63810046e-01   1.47429042e-01   1.35143288e-01
    1.25490196e-01   1.17647059e-01],
 [  3.73770292e-01   2.09016082e-01   6.78532034e-02   1.39582215e-02
    1.89003753e-03   1.68885303e-04   9.62295375e-06   3.17769127e-07
    4.46104746e-09   2.20133438e-01   3.43958497e-01   3.09132700e-01
    2.41012794e-01   1.94347888e-01   1.66650637e-01   1.48719036e-01
    1.35807777e-01   1.25855315e-01],
 [  3.11663896e-01   3.06638455e-01   2.02332733e-01   8.94385710e-02
    2.66813026e-02   5.32020127e-03   6.81398496e-04   5.08044167e-05
    1.67948485e-06   7.07545654e-02   2.07209799e-01   2.96555487e-01
    2.92571675e-01   2.43843879e-01   1.98570296e-01   1.68671368e-01
    1.49520047e-01   1.36137091e-01],
 [  2.44042188e-01   2.93174990e-01   2.81499972e-01   1.97504125e-01
    9.74752167e-02   3.28394798e-02   7.18929918e-03   9.22549849e-04
    5.27171340e-05   1.90856412e-02   9.06567959e-02   1.99802807e-01
    2.77189118e-01   2.83255891e-01   2.43800895e-01   2.00174117e-01
    1.69378295e-01   1.49649602e-01],
 [  1.97866831e-01   2.44427869e-01   2.83430648e-01   2.71206641e-01
    1.95516338e-01   9.97711554e-02   3.37413845e-02   6.76442793e-03
    6.08038466e-04   4.05196947e-03   2.95793771e-02   9.79563619e-02
    1.96206492e-01   2.69960187e-01   2.80370309e-01   2.43866828e-01
    1.99997042e-01   1.68723418e-01],
 [  1.68723418e-01   1.99997042e-01   2.43866828e-01   2.80370309e-01
    2.69960187e-01   1.96206492e-01   9.79563620e-02   2.95793772e-02
    4.05196947e-03   6.08038465e-04   6.76442792e-03   3.37413845e-02
    9.97711553e-02   1.95516338e-01   2.71206641e-01   2.83430648e-01
    2.44427869e-01   1.97866831e-01],
 [  1.49649602e-01   1.69378295e-01   2.00174117e-01   2.43800894e-01
    2.83255891e-01   2.77189118e-01   1.99802807e-01   9.06567959e-02
    1.90856413e-02   5.27171338e-05   9.22549847e-04   7.18929917e-03
    3.28394797e-02   9.74752166e-02   1.97504125e-01   2.81499972e-01
    2.93174990e-01   2.44042188e-01],
 [  1.36137091e-01   1.49520047e-01   1.68671367e-01   1.98570296e-01
    2.43843879e-01   2.92571675e-01   2.96555487e-01   2.07209799e-01
    7.07545654e-02   1.67948484e-06   5.08044165e-05   6.81398494e-04
    5.32020126e-03   2.66813026e-02   8.94385709e-02   2.02332733e-01
    3.06638455e-01   3.11663896e-01],
 [  1.25855315e-01   1.35807777e-01   1.48719036e-01   1.66650637e-01
    1.94347888e-01   2.41012794e-01   3.09132700e-01   3.43958498e-01
    2.20133438e-01   4.46104727e-09   3.17769134e-07   9.62295367e-06
    1.68885302e-04   1.89003752e-03   1.39582215e-02   6.78532033e-02
    2.09016082e-01   3.73770292e-01]]
b=[ 0.4         0.49479209  0.64342066  0.82512366  1.0269273   1.23579182
  1.43570174  1.6028701   1.68969412  0.66666667  0.77260435  0.87408469
  0.97564512  1.07906283  1.18365629  1.28518028  1.37220606  1.41188186]

1 个答案:

答案 0 :(得分:15)

无论它有什么价值,你都会对基本的线性代数感到困惑。

你有18个方程式,但只有9个独特的方程式。其他9个是其他行的线性组合。

换句话说,你的问题不明确。

如果您快速查看A ....

,这一点很明显

enter image description here

这意味着A是一个奇异矩阵。

lstsq基本上(虽然没有明确地)计算A的Moore-Penrose伪逆。即使A是单数,它也是有效的。

对您的问题绝对没有唯一解决方案。但是,您仍然可以获得无限数量的近似解决方案。

知道选择哪种近似解决方案需要了解您的问题。我们需要 a-priori 信息。这基本上就是逆理论的全部内容:如何将先验信息纳入欠定问题。

在您的情况下,看起来某些列也是相关的。这意味着我们可以减少问题的维度。但是,你不能用“普通”线性代数来解决它。这是不是一个公认的问题。你有18个方程,只有9个解。

但是,为了减少维度,我们需要有关您尝试解决的物理问题的更多信息。你的18个变量代表什么?它们之间是否存在相关性?它们是否应该是空间相关的(即一个值是否应该接近一个值?)?

这是生成上图的代码,顺便说一句......

import numpy as np
import matplotlib.pyplot as plt

A=np.array([[2.00000000e-05, 1.04829508e-14, 2.92236138e-24, 4.99137993e-34,
             5.52810194e-44, 4.00873469e-54, 1.84523284e-64, 4.90835508e-75,
             5.76100393e-86, 5.99076740e-01, 2.99538370e-01, 2.24653778e-01,
             1.87211481e-01, 1.63810046e-01, 1.47429042e-01, 1.35143288e-01,
             1.25490196e-01, 1.17647059e-01],
             [3.73770292e-01, 2.09016082e-01, 6.78532034e-02, 1.39582215e-02,
             1.89003753e-03, 1.68885303e-04, 9.62295375e-06, 3.17769127e-07,
             4.46104746e-09, 2.20133438e-01, 3.43958497e-01, 3.09132700e-01,
             2.41012794e-01, 1.94347888e-01, 1.66650637e-01, 1.48719036e-01,
             1.35807777e-01, 1.25855315e-01],
             [3.11663896e-01, 3.06638455e-01, 2.02332733e-01, 8.94385710e-02,
             2.66813026e-02, 5.32020127e-03, 6.81398496e-04, 5.08044167e-05,
             1.67948485e-06, 7.07545654e-02, 2.07209799e-01, 2.96555487e-01,
             2.92571675e-01, 2.43843879e-01, 1.98570296e-01, 1.68671368e-01,
             1.49520047e-01, 1.36137091e-01],
             [2.44042188e-01, 2.93174990e-01, 2.81499972e-01, 1.97504125e-01,
             9.74752167e-02, 3.28394798e-02, 7.18929918e-03, 9.22549849e-04,
             5.27171340e-05, 1.90856412e-02, 9.06567959e-02, 1.99802807e-01,
             2.77189118e-01, 2.83255891e-01, 2.43800895e-01, 2.00174117e-01,
             1.69378295e-01, 1.49649602e-01],
             [1.97866831e-01, 2.44427869e-01, 2.83430648e-01, 2.71206641e-01,
             1.95516338e-01, 9.97711554e-02, 3.37413845e-02, 6.76442793e-03,
             6.08038466e-04, 4.05196947e-03, 2.95793771e-02, 9.79563619e-02,
             1.96206492e-01, 2.69960187e-01, 2.80370309e-01, 2.43866828e-01,
             1.99997042e-01, 1.68723418e-01],
             [1.68723418e-01, 1.99997042e-01, 2.43866828e-01, 2.80370309e-01,
             2.69960187e-01, 1.96206492e-01, 9.79563620e-02, 2.95793772e-02,
             4.05196947e-03, 6.08038465e-04, 6.76442792e-03, 3.37413845e-02,
             9.97711553e-02, 1.95516338e-01, 2.71206641e-01, 2.83430648e-01,
             2.44427869e-01, 1.97866831e-01],
             [1.49649602e-01, 1.69378295e-01, 2.00174117e-01, 2.43800894e-01,
             2.83255891e-01, 2.77189118e-01, 1.99802807e-01, 9.06567959e-02,
             1.90856413e-02, 5.27171338e-05, 9.22549847e-04, 7.18929917e-03,
             3.28394797e-02, 9.74752166e-02, 1.97504125e-01, 2.81499972e-01,
             2.93174990e-01, 2.44042188e-01],
             [1.36137091e-01, 1.49520047e-01, 1.68671367e-01, 1.98570296e-01,
             2.43843879e-01, 2.92571675e-01, 2.96555487e-01, 2.07209799e-01,
             7.07545654e-02, 1.67948484e-06, 5.08044165e-05, 6.81398494e-04,
             5.32020126e-03, 2.66813026e-02, 8.94385709e-02, 2.02332733e-01,
             3.06638455e-01, 3.11663896e-01],
             [1.25855315e-01, 1.35807777e-01, 1.48719036e-01, 1.66650637e-01,
             1.94347888e-01, 2.41012794e-01, 3.09132700e-01, 3.43958498e-01,
             2.20133438e-01, 4.46104727e-09, 3.17769134e-07, 9.62295367e-06,
             1.68885302e-04, 1.89003752e-03, 1.39582215e-02, 6.78532033e-02,
             2.09016082e-01, 3.73770292e-01],
             [2.00000000e-05, 1.04829508e-14, 2.92236138e-24, 4.99137993e-34,
             5.52810194e-44, 4.00873469e-54, 1.84523284e-64, 4.90835508e-75,
             5.76100393e-86, 5.99076740e-01, 2.99538370e-01, 2.24653778e-01,
             1.87211481e-01, 1.63810046e-01, 1.47429042e-01, 1.35143288e-01,
             1.25490196e-01, 1.17647059e-01],
             [3.73770292e-01, 2.09016082e-01, 6.78532034e-02, 1.39582215e-02,
             1.89003753e-03, 1.68885303e-04, 9.62295375e-06, 3.17769127e-07,
             4.46104746e-09, 2.20133438e-01, 3.43958497e-01, 3.09132700e-01,
             2.41012794e-01, 1.94347888e-01, 1.66650637e-01, 1.48719036e-01,
             1.35807777e-01, 1.25855315e-01],
             [3.11663896e-01, 3.06638455e-01, 2.02332733e-01, 8.94385710e-02,
             2.66813026e-02, 5.32020127e-03, 6.81398496e-04, 5.08044167e-05,
             1.67948485e-06, 7.07545654e-02, 2.07209799e-01, 2.96555487e-01,
             2.92571675e-01, 2.43843879e-01, 1.98570296e-01, 1.68671368e-01,
             1.49520047e-01, 1.36137091e-01],
             [2.44042188e-01, 2.93174990e-01, 2.81499972e-01, 1.97504125e-01,
             9.74752167e-02, 3.28394798e-02, 7.18929918e-03, 9.22549849e-04,
             5.27171340e-05, 1.90856412e-02, 9.06567959e-02, 1.99802807e-01,
             2.77189118e-01, 2.83255891e-01, 2.43800895e-01, 2.00174117e-01,
             1.69378295e-01, 1.49649602e-01],
             [1.97866831e-01, 2.44427869e-01, 2.83430648e-01, 2.71206641e-01,
             1.95516338e-01, 9.97711554e-02, 3.37413845e-02, 6.76442793e-03,
             6.08038466e-04, 4.05196947e-03, 2.95793771e-02, 9.79563619e-02,
             1.96206492e-01, 2.69960187e-01, 2.80370309e-01, 2.43866828e-01,
             1.99997042e-01, 1.68723418e-01],
             [1.68723418e-01, 1.99997042e-01, 2.43866828e-01, 2.80370309e-01,
             2.69960187e-01, 1.96206492e-01, 9.79563620e-02, 2.95793772e-02,
             4.05196947e-03, 6.08038465e-04, 6.76442792e-03, 3.37413845e-02,
             9.97711553e-02, 1.95516338e-01, 2.71206641e-01, 2.83430648e-01,
             2.44427869e-01, 1.97866831e-01],
             [1.49649602e-01, 1.69378295e-01, 2.00174117e-01, 2.43800894e-01,
             2.83255891e-01, 2.77189118e-01, 1.99802807e-01, 9.06567959e-02,
             1.90856413e-02, 5.27171338e-05, 9.22549847e-04, 7.18929917e-03,
             3.28394797e-02, 9.74752166e-02, 1.97504125e-01, 2.81499972e-01,
             2.93174990e-01, 2.44042188e-01],
             [1.36137091e-01, 1.49520047e-01, 1.68671367e-01, 1.98570296e-01,
             2.43843879e-01, 2.92571675e-01, 2.96555487e-01, 2.07209799e-01,
             7.07545654e-02, 1.67948484e-06, 5.08044165e-05, 6.81398494e-04,
             5.32020126e-03, 2.66813026e-02, 8.94385709e-02, 2.02332733e-01,
             3.06638455e-01, 3.11663896e-01],
             [1.25855315e-01, 1.35807777e-01, 1.48719036e-01, 1.66650637e-01,
             1.94347888e-01, 2.41012794e-01, 3.09132700e-01, 3.43958498e-01,
             2.20133438e-01, 4.46104727e-09, 3.17769134e-07, 9.62295367e-06,
             1.68885302e-04, 1.89003752e-03, 1.39582215e-02, 6.78532033e-02,
             2.09016082e-01, 3.73770292e-01]])
b = np.array([0.4, 0.49479209, 0.64342066, 0.82512366, 1.0269273, 1.23579182,
        1.43570174, 1.6028701, 1.68969412, 0.66666667, 0.77260435, 0.87408469,
        0.97564512, 1.07906283, 1.18365629, 1.28518028, 1.37220606, 1.41188186])

plt.matshow(A)
plt.colorbar()
plt.show()