0% found this document useful (0 votes)
8 views5 pages

Na Lab 05

Uploaded by

mahroosha saeed
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)
8 views5 pages

Na Lab 05

Uploaded by

mahroosha saeed
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/ 5

Numerical Analysis Lab

BSE 7A

FALL 2024

LAB JOURNAL # 05

Submitted by: Umaima Aslam

Enrollment number: 01-131212-036

Submitted to: Ma’am Asuda Adnan

Department of Software Engineering

Bahria University H11/4 Campus


Lab # 05
Gauss Elimination Method

Lab Task # 1:

Code:
import numpy as np

A = np.array([[4, 3, -5],
[-2, -4, 5],
[8, 8, 0]], dtype=float)

b = np.array([2, 5, -3], dtype=float)

solution_solve = np.linalg.solve(A, b)

A_inv = np.linalg.inv(A)
solution_inv = np.dot(A_inv, b)

solution_solve_rounded = np.round(solution_solve, 2)
solution_inv_rounded = np.round(solution_inv, 2)

print("Solution using Gaussian Elimination (solve function):")


print(f"x1 = {solution_solve_rounded[0]}, x2 =
{solution_solve_rounded[1]}, x3 = {solution_solve_rounded[2]}")

print("\nSolution using Matrix Inversion (inv function):")


print(f"x1 = {solution_inv_rounded[0]}, x2 = {solution_inv_rounded[1]},
x3 = {solution_inv_rounded[2]}")
Output:

Task 02:

Code:
import numpy as np

def gaussian_elimination(A, b):

n = len(b)

aug_matrix = np.hstack([A, b.reshape(-1, 1)])

for i in range(n):

aug_matrix[i] = aug_matrix[i] / aug_matrix[i, i]


for j in range(i+1, n):
factor = aug_matrix[j, i] / aug_matrix[i, i]
aug_matrix[j] = aug_matrix[j] - factor * aug_matrix[i]

x = np.zeros(n)
for i in range(n-1, -1, -1):
x[i] = aug_matrix[i, -1] - np.dot(aug_matrix[i, i+1:n], x[i+1:n])

return x

A = np.array([[4, 3, -5],
[-2, -4, 5],
[8, 8, 0]], dtype=float)
b = np.array([2, 5, -3], dtype=float)

solution_gaussian = gaussian_elimination(A.copy(), b.copy())


solution_gaussian_rounded = np.round(solution_gaussian, 2)

print("Solution using manual Gaussian Elimination:")


print(f"x1 = {solution_gaussian_rounded[0]} x2 = {solution_gaussian_rounded[1]} x3
= {solution_gaussian_rounded[2]}")

Output:

Task 03:

Code:
import numpy as np

def gaussian_elimination(a, b):


n = len(b)

augmented_matrix = np.hstack((a, b.reshape(-1, 1)))

for i in range(n):
pivot = augmented_matrix[i, i]
augmented_matrix[i] = augmented_matrix[i] / pivot

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


multiplier = augmented_matrix[j, i]
augmented_matrix[j] = augmented_matrix[j] - multiplier *
augmented_matrix[i]

x = np.zeros(n)
for i in range(n - 1, -1, -1):
x[i] = augmented_matrix[i, -1] - np.sum(augmented_matrix[i, i + 1:n] *
x[i + 1:n])

return x

if __name__ == "__main__":
A = np.array([[1, 2, -1],
[2, -1, 1],
[-1, 1, 2]], dtype=float)

B = np.array([8, 2, 3], dtype=float)

solution = gaussian_elimination(A, B)

for i, val in enumerate(solution):


print(f"x{i + 1} = {val:.2f}")

Output:

Conclusion:
In this lab, we used the Gaussian Elimination method to solve systems of equations by
transforming the matrix and eliminating elements step by step. This process shows how useful
linear algebra is for solving real-life problems and highlights the importance of careful
calculations.

You might also like