Na Lab 05
Na Lab 05
BSE 7A
FALL 2024
LAB JOURNAL # 05
Lab Task # 1:
Code:
import numpy as np
A = np.array([[4, 3, -5],
[-2, -4, 5],
[8, 8, 0]], 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)
Task 02:
Code:
import numpy as np
n = len(b)
for i in range(n):
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)
Output:
Task 03:
Code:
import numpy as np
for i in range(n):
pivot = augmented_matrix[i, i]
augmented_matrix[i] = augmented_matrix[i] / pivot
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)
solution = gaussian_elimination(A, B)
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.