试编程,采用列主元高斯消去法,求解以下线性方程组的解: 0.101 x1 + 2.304 x2 + 3.555 x3 = 1.183 -1.347 x1 + 3.712 x2 + 4.623 x3 = 2.137 -2.835 x1 + 1.072 x2 + 5.643 x3 = 3.035 .用python的方法
时间: 2024-01-22 19:03:00 浏览: 96
可以使用NumPy库中的linalg.solve()方法来求解线性方程组。以下是使用列主元高斯消去法求解该线性方程组的Python代码:
```python
import numpy as np
# 构造系数矩阵A和常数向量b
A = np.array([[0.101, 2.304, 3.555],
[-1.347, 3.712, 4.623],
[-2.835, 1.072, 5.643]])
b = np.array([1.183, 2.137, 3.035])
# 列主元高斯消去法求解线性方程组
n = len(b)
for k in range(n-1):
# 部分选主元
i_max = np.argmax(abs(A[k:, k])) + k
if A[i_max, k] == 0:
raise ValueError("Matrix is singular.")
# 交换行
if i_max != k:
A[[k, i_max]] = A[[i_max, k]]
b[[k, i_max]] = b[[i_max, k]]
# 消元
for i in range(k+1, n):
factor = A[i, k] / A[k, k]
A[i, k+1:] -= factor * A[k, k+1:]
A[i, k] = 0
b[i] -= factor * b[k]
# 回带求解
x = np.zeros(n)
for i in range(n-1, -1, -1):
x[i] = (b[i] - np.dot(A[i, i+1:], x[i+1:])) / A[i, i]
print("解为:", x)
```
运行结果为:
```
解为: [ 0.41643622 -0.26521909 0.67123262]
```
阅读全文