题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/81c98dee720feab6ba59cf6b4a0a1731.png)
代码
def Jacobi(x1,x2,x3):
_x1 = -0.1*x2 - (3/20)*x3 + 1.2
_x2 = -(1/8)*(x1+x3) + (3/2)
_x3 = -(2/15)*x1 + 0.2*x2 + 2
return _x1, _x2, _x3
print("简单雅各比迭代法")
x1 = x2 = x3 = 0.0
print(f"x1 = {x1}, x2 = {x2}, x3 = {x3}")
for i in range(1,10):
x1,x2,x3 = Jacobi(x1,x2,x3)
print(f"x1 = {x1}, x2 = {x2}, x3 = {x3}")
print("------------------------------------------------------------")
print("Gauss-Seidel赛德尔迭代法")
x1 = x2 = x3 = 0.0
print(f"x1 = {x1}, x2 = {x2}, x3 = {x3}")
for i in range(1,10):
x1 = -0.1*x2 - (3/20)*x3 + 1.2
x2 = -(1/8)*(x1+x3) + (3/2)
x3 = -(2/15)*x1 + 0.2*x2 + 2
print(f"x1 = {x1}, x2 = {x2}, x3 = {x3}")
演示效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8031021a088f120036d685ac878d16c8.png)