高斯消元法python编程_Gaussian elimination 高斯消元法 python实现A=[[1,2],[2,3]],b=[1,2]]
时间: 2023-11-21 13:06:36 浏览: 133
python实现高斯(Gauss)迭代法的例子
下面是高斯消元法的Python实现:
```python
def gauss_elim(A, b):
n = len(A)
# 将增广矩阵构造出来
aug = [A[i] + [b[i]] for i in range(n)]
# 消元过程
for i in range(n):
# 将当前列中第i行以下的所有元素消为0
for j in range(i+1, n):
factor = aug[j][i] / aug[i][i]
for k in range(i, n+1):
aug[j][k] -= factor * aug[i][k]
# 回带过程
x = [0] * n
for i in range(n-1, -1, -1):
x[i] = aug[i][n] / aug[i][i]
for j in range(i):
aug[j][n] -= aug[j][i] * x[i]
return x
```
使用示例:
```python
A = [[1, 2], [2, 3]]
b = [1, 2]
x = gauss_elim(A, b)
print(x)
```
输出结果为:`[-3.0, 2.0]`,即方程组的解为x=-3,y=2。
阅读全文