欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jacobi迭代法、高斯赛德尔迭代法python实现

程序员文章站 2022-06-07 09:38:23
...

jacobi迭代法

代码

#程序目的:实现jacobi迭代法
import numpy as np
import matplotlib.pyplot as plt

def draw_fuction():
	x=np.arange(0,8,0.01)
	y=np.arange(0,8,0.01)
	x,y=np.meshgrid(x,y)
	z=4*x+y-24
	w=2*x+5*y-30
	plt.contour(x,y,z,0)
	plt.contour(x,y,w,0)
def Jacobi(A,b,x_0,n):#记录未知数个数
	x=x_0.copy()
	y=x_0.copy()
	k=0
	key_value=[x]
	while(k<6):
		for i in range(n):
			sum_value=0
			for j in range(n):
				if j!=i:
					sum_value=sum_value+A[i][j]*x[j]
			y[i]=(b[i]-sum_value)/A[i][i]
		x=y.copy()
		k=k+1
		key_value.append(x)
	return x,key_value

A_value=[[4,1],[2,5]]
b=[24,30]
x_0=np.zeros(2)
experiment_value=Jacobi(A_value,b,x_0,2)[0]#输入系数矩阵、向量组右端值
key=Jacobi(A_value,b,x_0,2)[1]
print(key)
x=[]
y=[]
for i in range(7):
	x.append(int(key[i][0]))
	y.append(int(key[i][1]))
print(x)
print(y)

step=np.zeros((13,2))
#路线扩充
for i in range(13):
	if i%2==0:
		step[i]=key[i//2]
	else:
		step[i]=[key[(i+1)//2][0],key[i//2][1]]
print(step)

append_x=[]
append_y=[]
for i in range(13):
	append_x.append(step[i][0])
	append_y.append(step[i][1])
print(append_x)
print(append_y)

for i in range(5):
	plt.scatter(key[i][0],key[i][1], color='b')
	
draw_fuction()
# print(key[])
plt.plot(append_x,append_y,color='r')

plt.show()

结果

jacobi迭代法、高斯赛德尔迭代法python实现

高斯赛德尔迭代法

代码

#程序目的:实现jacobi迭代法
import numpy as np
import matplotlib.pyplot as plt

def draw_fuction():
	x=np.arange(2,7,0.01)
	y=np.arange(2,7,0.01)
	x,y=np.meshgrid(x,y)
	z=4*x+y-24
	w=2*x+5*y-30
	plt.contour(x,y,z,0)
	plt.contour(x,y,w,0)
def Jacobi(A,b,x_0,n):#记录未知数个数
	x=x_0.copy()
	y=x_0.copy()
	k=0
	key_value=[x]
	while(k<6):
		for i in range(n):
			sum_value=0
			for j in range(i):
				if (i!=j):
					sum_value=sum_value+A[i][j]*y[j]
			for j in range(i+1,n):
				sum_value=sum_value+A[i][j]*x[j]
			print(sum_value)
			y[i]=(b[i]-sum_value)/A[i][i]
		x=y.copy()
		k=k+1
		key_value.append(x)
	return x,key_value

A_value=[[4,1],[2,5]]
b=[24,30]
x_0=np.zeros(2)
experiment_value=Jacobi(A_value,b,x_0,2)[0]#输入系数矩阵、向量组右端值
key=Jacobi(A_value,b,x_0,2)[1]
print(key)
x=[]
y=[]
for i in range(7):
	x.append(int(key[i][0]))
	y.append(int(key[i][1]))
print(x)
print(y)

step=np.zeros((13,2))
#路线扩充
for i in range(13):
	if i%2==0:
		step[i]=key[i//2]
	else:
		step[i]=[key[(i+1)//2][0],key[i//2][1]]
print(step)

append_x=[]
append_y=[]
for i in range(13):
	append_x.append(step[i][0])
	append_y.append(step[i][1])
print(append_x)
print(append_y)

for i in range(6):
	plt.scatter(key[i][0],key[i][1], color='b')
	
draw_fuction()
# print(key[])
plt.plot(append_x,append_y,color='r')

plt.show()

结果

相关标签: 数值分析