Numerical Methods and Probability Assignment
Numerical Methods and Probability Assignment
NUMERICAL METHODS
& PROBABILITY
ASSIGNMENT
03
MINAL FATIMA
21-CP-55
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
CODE:
import numpy as np
import matplotlib
matplotlib.use('TkAgg') # or any other backend that works for you
import matplotlib.pyplot as plt
from scipy.integrate import quad
def plot_error_graph(errors):
methods = list(errors.keys())
values = list(errors.values())
if __name__ == "__main__":
option = int(input("Choose an option (1 for Linear Equations, 2 for Numerical
Integration): "))
if option == 1:
# Linear Equations
A = np.array([[4, -1, 0, 0], [-1, 4, -1, 0], [0, -1, 4, -1], [0, 0, -1, 3]])
b = np.array([15, 10, 10, 10])
print("\nSOR Method:")
print("Solution:", sor_solution)
print("Number of Iterations:", sor_iterations)
print("\nGauss-Seidel Method:")
print("Solution:", gs_solution)
print("Number of Iterations:", gs_iterations)
elif option == 2:
# Numerical Integration
a = 0
b = 2
n = 100 # Number of subintervals
func = lambda x: x ** 2
trapezoidal_result = trapezoidal_rule(func, a, b, n)
simpsons_result = simpsons_rule(func, a, b, n)
exact_integral_value = exact_integral(func, a, b)
print("\nTrapezoidal Method:")
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
print("Result:", trapezoidal_result)
else:
print("Invalid option. Please choose 1 or 2.")
OUTPUT:
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING