-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob2_gauss_seidel.py
32 lines (24 loc) · 1.03 KB
/
prob2_gauss_seidel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''
coded by: Rakeeb
code for: implenting Gauss Seidel method
'''
import numpy as np
x_exact= np.array([7.859713071,0.422926408,-0.073592239,-0.540643016,0.010626163]) #exact solution given
a= np.array([[0.2,0.1,1,1,0],[0.1,4,-1,1,-1],[1,-1,60,0,-2],[1,1,0,8,4],[0,-1,-2,4,700]])
b= np.array([1,2,3,4,5])
# Gauss Seidel method:
x= np.zeros(b.size) # initial value
U= np.triu(a,k=1) #upper triangular part of "a" without main diagonal
D_L= np.tril(a,k=0) #lower triangular part of "a" with the main diagonal
'''
As was done in class, U is the upper triangular part and D_L is the (D+L) matrix, where D is the diagonal part
and L is the lower triangular part of the given matrix. We have to take the inverse of the (D+L) part and
multiply it to the right hand side.
'''
D_L_inv= np.linalg.inv(D_L)
i=0 # for counting iteration number
while (any(np.abs(x_exact[j]-x[j])>0.01 for j in range (b.size))):
s= b-np.matmul(U,x)
x= np.matmul(D_L_inv,s)
i=i+1
print('solution by Gauss-Seidel method: ',x ,'\nNumber of itarations needed: ',i )