import os
os.getcwd()
'D:\\#Python\\jupter'
import numpy as np
def jacobi(a,b,c=0.0001,d=30):
x1=np.zeros(a.shape[1])
x2=np.zeros(a.shape[1])
k=0
while k<d:
k=k+1
print('k=',k)
for i in range(a.shape[1]):
x2[i]=(-a[i].dot(x1)+b[i]+a[i,i]*x1[i])/a[i,i]
if np.max(np.abs(x2-x1))<=c:
print("x%d=" % k,x2)
print(np.max(np.abs(x2-x1)))
break
print("x%d=" % k,x2)
x1=x2.copy()
return x2
a=np.array([[1,0,0,0,0,0,0],
[-0.9,1,0,0,0,0,0],
[0,0.54,-1,0,0.36,0,0],
[0.18,0,0.72,-1,0,0,0],
[0,0,0.36,0.36,-1,0.18,0],
[0,0,0,0.45,0,-1,0.45],
[0,0,0,0,0,0.09,-0.19]]
)
b=np.array([0,10,2,2,-1,2,1])
jacobi(a,b)
k= 1
x1= [ 0. 10. -2. -2. 1. -2.
-5.26315789]
k= 2
x2= [ 0. 10. 3.76 -3.44 -0.8 -5.26842105
-6.21052632]
k= 3
x3= [ 0. 10. 3.112 0.7072 0.16688421 -6.34273684
-7.75872576]
k= 4
x4= [ 0. 10. 3.46007832 0.24064 1.23321937 -5.17318659
-8.26761219]
k= 5
x5= [ 0. 10. 3.84395897 0.49125639 1.40108501 -5.61213748
-7.7136147 ]
k= 6
x6= [ 0. 10. 3.9043906 0.76765046 1.55049278 -5.25006124
-7.92153881]
k= 7
x7= [ 0. 10. 3.9581774 0.81116123 1.73692376 -5.21924976
-7.75002901]
k= 8
x8= [ 0. 10. 4.02529255 0.84988773 1.77749695 -5.1224905
-7.7354341 ]
k= 9
x9= [ 0. 10. 4.0398989 0.89821064 1.83301661 -5.09849586
-7.68960076]
k= 10
x10= [ 0. 10. 4.05988598 0.90872721 1.85999018 -5.05612556
-7.67823488]
k= 11
x11= [ 0. 10. 4.06959646 0.92311791 1.87859815 -5.04627845
-7.65816474]
k= 12
x12= [ 0. 10. 4.07629533 0.93010945 1.88904705 -5.03077107
-7.65350032]
k= 13
x13= [ 0. 10. 4.08005694 0.93493264 1.89676693 -5.02552589
-7.64615472]
k= 14
x14= [ 0. 10. 4.08283609 0.937641 1.90080159 -5.02004994
-7.64367016]
k= 15
x15= [ 0. 10. 4.08428857 0.93964199 1.90376276 -5.01771312
-7.64107629]
k= 16
x16= [ 0. 10. 4.0853546 0.94068777 1.90542664 -5.01564543
-7.63996937]
k= 17
x17= [ 0. 10. 4.08595359 0.94145531 1.90655907 -5.01467672
-7.63898994]
k= 18
x18= [ 0. 10. 4.08636127 0.94188658 1.90722539 -5.01389059
-7.63853108]
k= 19
x19= [ 0. 10. 4.08660114 0.94218011 1.90766892 -5.01349002
-7.6381587 ]
k= 20
x20= [ 0. 10. 4.08676081 0.94235282 1.90793305 -5.01319036
-7.63796896]
k= 21
x21= [ 0. 10. 4.0868559 0.94246778 1.90810664 -5.01302726
-7.63782701]
k= 22
x22= [ 0. 10. 4.08691839 0.94253625 1.90821162 -5.01291165
-7.63774976]
k= 23
x23= [ 0. 10. 4.08695618 0.94258124 1.90827957 -5.01284608
-7.63769499]
6.795344688015348e-05
array([ 0. , 10. , 4.08695618, 0.94258124, 1.90827957,
-5.01284608, -7.63769499])
标签:10,线性方程组,shape,np,import,迭代法,可比,7.63769499
From: https://www.cnblogs.com/HorizonTree/p/18432115