jupyter写出高斯消除算法Gauss_elimination(A)
时间: 2023-05-28 17:07:00 浏览: 151
很高兴为您提供高斯消除算法的Jupyter代码:
```python
import numpy as np
def Gauss_elimination(A):
'''
高斯消元法解线性方程组
A: 系数矩阵
return: 解向量x
'''
n = len(A)
# 高斯消元
for i in range(n-1):
for j in range(i+1, n):
factor = A[j][i] / A[i][i]
for k in range(i+1, n):
A[j][k] -= factor * A[i][k]
A[j][i] = 0
A[j][n] -= factor * A[i][n]
# 回代求解
x = np.zeros(n)
x[n-1] = A[n-1][n] / A[n-1][n-1]
for i in range(n-2, -1, -1):
sum = A[i][n]
for j in range(i+1, n):
sum -= A[i][j] * x[j]
x[i] = sum / A[i][i]
return x
```
该函数接收一个系数矩阵A作为参数,返回线性方程组的解向量x。在函数中,首先进行高斯消元,将系数矩阵A化为上三角矩阵。然后进行回代,求解出线性方程组的解向量x。
阅读全文