版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a19990412/article/details/90349029
数值分析题目
求方程组
超松弛迭代其实基于高斯-塞德尔迭代的
可以对比着之前的高斯迭代法对比一下效果
高斯–塞德尔迭代法求方程组的解(Python实现)
代码
import numpy as np
A = np.array([[5.0, 2, 1], [-1, 4, 2], [2, -3, 10]])
B = np.array([-12.0, 20, 3])
x0 = np.array([1.0, 1, 1])
x = np.array([0.0, 0, 0])
w = 1.5
times, MT = 0, 1000
while times < MT:
tempx = x0.copy()
for i in range(3):
temp = 0
for j in range(3):
if i != j:
temp += x0[j] * A[i][j]
x[i] = (B[i] - temp) / A[i][i]
x0[i] = x[i]
x = w * x + (1-w) * tempx
calTemp = max(abs(x - tempx))
times += 1
if calTemp < 1e-4:
break
print(times)
print(x)
- 结果算出来的X为[-4, 3 , 2]