Thomas Algorithm For Tridiagonal Matrix Using Python
Thomas Algorithm For Tridiagonal Matrix Using Python
# Assignment No. 2
print("Enter the values of the given indexes as per the format \"a[row][column]\"")
#Create an augumented matrix
for i in range(n): #Starting value is 0 and last value is n
for j in range(n+1): #Starting value is 0 and last value is n+1
a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']=')) #Enter the values
of augumented matrix
#To convert augumented matrix to upper triangular matrix
for i in range(n):
if a[i][i] == 0.0: #This line will check all diagonal elements
sys.exit('Divide by zero detected!. Program will exit.')
#If diagonal elements becomes zero, then Above function is used to exit from the
program.
for j in range(i+1, n): #To calculate multiplication factor to each
pivoting point
ratio = a[j][i]/a[i][i]
for k in range(n+1): #To make each element below main diagonal as zero
a[j][k] = a[j][k] - ratio * a[i][k]
OUTPUT:
Enter number of unknowns: 3
Enter the values of the given indexes as per the format "a[row][column]"
a[0][0]=5
a[0][1]=-2
a[0][2]=3
a[0][3]=18
a[1][0]=1
a[1][1]=7
a[1][2]=-3
a[1][3]=-22
a[2][0]=2
a[2][1]=-1
a[2][2]=6
a[2][3]=22
The value of x is 1.00
The value of y is -2.00
The value of z is 3.00