0% found this document useful (0 votes)
14 views3 pages

Gauss Jordan and Elimination Methods Code

The document outlines algorithms and programs for solving systems of linear equations using the Gauss-Jordan and Gauss elimination methods. It includes step-by-step procedures for transforming augmented matrices and obtaining solutions, as well as Python code implementations for both methods. The programs read the number of unknowns and the augmented matrix, perform necessary calculations, and display the results.

Uploaded by

resot240
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Gauss Jordan and Elimination Methods Code

The document outlines algorithms and programs for solving systems of linear equations using the Gauss-Jordan and Gauss elimination methods. It includes step-by-step procedures for transforming augmented matrices and obtaining solutions, as well as Python code implementations for both methods. The programs read the number of unknowns and the augmented matrix, perform necessary calculations, and display the results.

Uploaded by

resot240
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Matrix Program

Algorithm For Gauss Jordan Method


Start
2. Read Number of Unknowns: n
3. Read Augmented Matrix (A) of n by n+1 Size
4. Transform Augmented Matrix (A)
to Diagonal Matrix by Row Operations.
5. Obtain Solution by Making All Diagonal Elements to 1.
6. Display Result.
7. Stop
Program
import numpy as np
import sys
n = int(input('Enter number of unknowns: '))
a = [Link]((n,n+1))
x = [Link](n)
print('Enter Augmented Matrix Coefficients:')
for i in range(n):
for j in range(n+1):
a[i][j] = float(input( 'a['+str(i)+']['+
str(j)+']='))
for i in range(n):
if a[i][i] == 0.0:
[Link]('Divide by zero detected!')
for j in range(n):
if i != j:
ratio = a[j][i]/a[i][i]
for k in range(n+1):
a[j][k] = a[j][k] - ratio * a[i][k]
for i in range(n):
x[i] = a[i][n]/a[i][i]
print('\nRequired solution is: ')
for i in range(n):
print('X%d = %0.2f' %(i,x[i]), end = '\t')

Algorithm For Gauss Elimination Method


Start
2. Read Number of Unknowns: n
3. Read Augmented Matrix (A) of n by n+1 Size
4. Transform Augmented Matrix (A)
to Upper Trainagular Matrix by Row Operations.
5. Obtain Solution by Back Substitution.
6. Display Result.
7. Stop

Program
import numpy as np
import sys
n = int(input('Enter number of unknowns: '))
a = [Link]((n,n+1))
x = [Link](n)
print('Enter Augmented Matrix Coefficients:')
for i in range(n):
for j in range(n+1):
a[i][j] = float(input( 'a['+str(i)+']['+
str(j)+']='))
for i in range(n):
if a[i][i] == 0.0:
[Link]('Divide by zero detected!')
for j in range(i+1, n):
ratio = a[j][i]/a[i][i]
for k in range(n+1):
a[j][k] = a[j][k] - ratio * a[i][k]
x[n-1] = a[n-1][n]/a[n-1][n-1]
for i in range(n-2,-1,-1):
x[i] = a[i][n]
for j in range(i+1,n):
x[i] = x[i] - a[i][j]*x[j]
x[i] = x[i]/a[i][i]
print('\nRequired solution is: ')
for i in range(n):

print('X%d = %0.2f' %(i,x[i]), end = '\t')

You might also like