Numpy linalg:结果不太可能的线性系统
Numpy linalg: linear system with unlikely results
考虑以下矩阵方程:
x=Ab
其中:
In[1]:A
Out[1]:
matrix([[ 0.477, -0.277, -0.2 ],
[-0.277, 0.444, -0.167],
[-0.2 , -0.167, 0.367]])
In[2]: b
Out[2]: [0, 60, 40]
为什么我使用 numpy.linalg()
时会得到以下结果?
import numpy as np
x = np.linalg.solve(A, b)
res=x.tolist()
# res=[1.8014398509481981e+18, 1.801439850948198e+18, 1.8014398509481984e+18]
这些数字很大!这里出了什么问题?我怀疑 A
的形式不对,因为它在我的等式中乘以 b
,而 numpy.linalg()
认为 A
就好像它乘以 x
.
你给出的等式 (x=A b
) 只是一个 matrix multiplication rather than a set of linear equations to solve (A x=b
) for which you would use np.linalg.solve
. What you need to do to get x
in your case is simply use np.dot
(A.dot(b)
).
您的矩阵是奇异的,通过添加总和为零的列可以看出。从数学上讲,这个系统只能解决一小部分 b
向量。
您得到的解决方案很可能只是数字噪音。
考虑以下矩阵方程:
x=Ab
其中:
In[1]:A
Out[1]:
matrix([[ 0.477, -0.277, -0.2 ],
[-0.277, 0.444, -0.167],
[-0.2 , -0.167, 0.367]])
In[2]: b
Out[2]: [0, 60, 40]
为什么我使用 numpy.linalg()
时会得到以下结果?
import numpy as np
x = np.linalg.solve(A, b)
res=x.tolist()
# res=[1.8014398509481981e+18, 1.801439850948198e+18, 1.8014398509481984e+18]
这些数字很大!这里出了什么问题?我怀疑 A
的形式不对,因为它在我的等式中乘以 b
,而 numpy.linalg()
认为 A
就好像它乘以 x
.
你给出的等式 (x=A b
) 只是一个 matrix multiplication rather than a set of linear equations to solve (A x=b
) for which you would use np.linalg.solve
. What you need to do to get x
in your case is simply use np.dot
(A.dot(b)
).
您的矩阵是奇异的,通过添加总和为零的列可以看出。从数学上讲,这个系统只能解决一小部分 b
向量。
您得到的解决方案很可能只是数字噪音。