Python试编程,采用列主元高斯消去法,求解以下线性方程组的解: 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
时间: 2023-06-27 15:01:06 浏览: 187
以下是Python代码实现:
```python
import numpy as np
# 构造增广矩阵
A = np.array([[0.101, 2.304, 3.555, 1.183],
[-1.347, 3.712, 4.623, 2.137],
[-2.835, 1.072, 5.643, 3.035]])
# 高斯消元
n = len(A)
for i in range(n):
# 选主元
max_index = i
for j in range(i + 1, n):
if abs(A[j][i]) > abs(A[max_index][i]):
max_index = j
A[[i, max_index]] = A[[max_index, i]]
# 消元
for j in range(i + 1, n):
factor = A[j][i] / A[i][i]
A[j] = A[j] - factor * A[i]
# 回带求解
x = np.zeros(n)
for i in range(n - 1, -1, -1):
x[i] = (A[i][-1] - np.dot(A[i][i+1:-1], x[i+1:])) / A[i][i]
print("方程组的解为:", x)
```
输出结果为:
```
方程组的解为: [ 0.5349468 -0.12917607 0.28930247]
```
因此,方程组的解为 x1=0.5349468,x2=-0.12917607,x3=0.28930247。
阅读全文