gauss jordan method python

Solutions on MaxInterview for gauss jordan method python by the best coders in the world

showing results for - "gauss jordan method python"
Darcie
26 Nov 2019
1
2# Importing NumPy Library
3import numpy as np
4import sys
5
6# Reading number of unknowns
7n = int(input('Enter number of unknowns: '))
8
9# Making numpy array of n x n+1 size and initializing 
10# to zero for storing augmented matrix
11a = np.zeros((n,n+1))
12
13# Making numpy array of n size and initializing 
14# to zero for storing solution vector
15x = np.zeros(n)
16
17# Reading augmented matrix coefficients
18print('Enter Augmented Matrix Coefficients:')
19for i in range(n):
20    for j in range(n+1):
21        a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']='))
22
23# Applying Gauss Jordan Elimination
24for i in range(n):
25    if a[i][i] == 0.0:
26        sys.exit('Divide by zero detected!')
27        
28    for j in range(n):
29        if i != j:
30            ratio = a[j][i]/a[i][i]
31
32            for k in range(n+1):
33                a[j][k] = a[j][k] - ratio * a[i][k]
34
35# Obtaining Solution
36
37for i in range(n):
38    x[i] = a[i][n]/a[i][i]
39
40# Displaying solution
41print('\nRequired solution is: ')
42for i in range(n):
43    print('X%d = %0.2f' %(i,x[i]), end = '\t')
44
similar questions
queries leading to this page
gauss jordan method python