雅克比迭代法与赛德尔迭代法的简单例子 BIT数值分析

题目

雅克比迭代法与赛德尔迭代法的简单例子 BIT数值分析_第1张图片

代码

# 简单迭代法和赛德尔迭代法
# import math

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}")

演示效果

雅克比迭代法与赛德尔迭代法的简单例子 BIT数值分析_第2张图片

你可能感兴趣的:(雅克比迭代法与赛德尔迭代法的简单例子 BIT数值分析)