0% found this document useful (0 votes)
24 views4 pages

NC-Assignment No 1

Uploaded by

Hannan MHR
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)
24 views4 pages

NC-Assignment No 1

Uploaded by

Hannan MHR
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
You are on page 1/ 4

--Assignment No 1--

Numerical Computing
T0

Mr.Umer Hayat

BY

Hannan Khalid

(20014119-026)

Submission Date:24-June-2024
BS Computer Science (A)

Department of Computing and Information Technology

UNIVERSITY OF GUJRAT
1) Write a function to perform Gaussian Elimination on a given system of
linear equations. Your function should return the solution vector.
2) Test your function with the following system of equations:

2x+3y−z=5
4x+y+2z=6
−x+y+z=0

3)Provide a detailed explanation of each step involved in the Gaussian


Elimination process.Also provide the code file (.py).

SOLUTION:

❖ PYTHON-CODE :

import numpy as np
from sympy import Matrix
def gaussian_elimination(A, b):

n = len(A)

for i in range(n):

if abs(A[i][i]) < 1e-9:

j=i+1
while j < n and abs(A[j][i]) < 1e-9:
j += 1
if j == n:
return None

for k in range(n):
A[i][k], A[j][k] = A[j][k], A[i][k]

for j in range(i + 1, n):


factor = A[j][i] / A[i][i]
for k in range(i + 1, n): # Change is here
A[j][k] -= factor * A[i][k]

x = [0] * n
for i in range(n - 1, -1, -1):
# Check for division by zero (may indicate inconsistent system)
if abs(A[i][i]) < 1e-9:
return None # Inconsistent system

sum = 0
for j in range(i + 1, n):
sum += A[i][j] * x[j]
x[i] = (b[i] - sum) / A[i][i]

return x

Test case:

A = [[2, 3, -1], [4, 1, 2], [-1, 1, 1]]


b = [5, 6, 0]

solution = gaussian_elimination(A, b)

if solution is None:
print("Singular or inconsistent system of equations")
else:
print("Solution vector:", solution)

Solution vector: [4.3, -1.2, 0.0]


❖ Explanation of Steps :

1. Forward Elimination:

• Iterate through each row (i) of the augmented matrix (A).


• Check the diagonal element (A[i][i]). If it's close to zero (< 1e-9 for numerical
stability), it might indicate a singular matrix (no unique solution).
• Try to swap rows with a non-zero element in the same column below it.
• If no suitable row is found, the system is singular and the function returns
None.
• Once a pivot element (non-zero diagonal element) is found, eliminate elements
below it in the same column.
• For each row (j) below the current row (i), calculate a factor (A[j][i] / A[i][i]).
• Subtract the product of this factor and the current row (i) from the
corresponding elements in the lower row (j). This effectively sets those
elements to zero.

2. Back Substitution:

• Start from the last row (n - 1) of the upper triangular matrix (obtained after
forward elimination).
• Iterate backwards (-1 step size) through the rows.
• For each row (i), check the diagonal element (A[i][i]). If it's close to zero, the
system might be inconsistent (no solution).
• The function returns None in this case.
• Calculate the value of x[i] using the formula: x[i] = (b[i] - sum(A[i][j] * x[j] for
j in range(i + 1, n))) / A[i][i]
• This involves iterating through the elements in the current row (i) to the right
(excluding the diagonal), multiplying them by the corresponding solved
variables (x[j]), and subtracting the sum from b[i].
• Finally, divide by the diagonal element (`A[i]).

You might also like