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

Norms in Python - Docx 2

The document demonstrates the calculation of L1 and L2 norms for a vector using NumPy, resulting in values of 15.0 and approximately 7.42, respectively. It also includes an implementation of the Gauss-Jacobi iteration method, which checks for diagonal dominance in a matrix and solves a system of linear equations if the matrix is suitable. The results of the solution and error convergence are plotted using Matplotlib.

Uploaded by

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

Norms in Python - Docx 2

The document demonstrates the calculation of L1 and L2 norms for a vector using NumPy, resulting in values of 15.0 and approximately 7.42, respectively. It also includes an implementation of the Gauss-Jacobi iteration method, which checks for diagonal dominance in a matrix and solves a system of linear equations if the matrix is suitable. The results of the solution and error convergence are plotted using Matplotlib.

Uploaded by

Usama Atiqq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

# norms in python

# ussama atiq
# fa19-bee-022

import numpy as np

vector = np.array([1, 2, 3, 4, 5])

l1_norm = np.linalg.norm(vector, ord=1)


# Calculate L2 (Euclidean) norm
l2_norm = np.linalg.norm(vector, ord=2)

print("L1 Norm:", l1_norm)


print("L2 Norm:", l2_norm)

L1 Norm: 15.0
L2 Norm: 7.416198487095663

# jacobi itteration method

import numpy as np

def is_diagonally_dominant(matrix):
n = len(matrix)
for i in range(n):
diagonal_sum = sum(abs(matrix[i][j]) for j in range(n) if j !=
i)
if abs(matrix[i][i]) <= diagonal_sum:
return False
return True

# Define your matrix A


A = np.array([[10, 2, 3],
[1, 10, 3],
[2, 3, 10]])

if is_diagonally_dominant(A):
print("The matrix is diagonally dominant.")
else:
print("No, the matrix is not diagonally dominant.")

The matrix is diagonally dominant.

2.

import numpy as np
import matplotlib.pyplot as plt

def is_diagonally_dominant(A):
n = A.shape[0]
for i in range(n):
row_sum = np.sum(np.abs(A[i, :])) - np.abs(A[i, i])
if np.abs(A[i, i]) <= row_sum:
return False
return True

def gauss_jacobi(A, b, max_iterations=50, tol=1e-6):


n = len(b)
x = np.zeros(n)
x_new = np.copy(x)
error_list = []

if not is_diagonally_dominant(A):
print("The coefficient matrix is not diagonally dominant. The
method may not converge.")
return None, error_list

for _ in range(max_iterations):
for i in range(n):
sigma = np.dot(A[i, :n], x) - A[i, i] * x[i]
x_new[i] = (b[i] - sigma) / A[i, i]

error = np.linalg.norm(x_new - x)
error_list.append(error)
if error < tol:
return x_new, error_list

x = np.copy(x_new)

return x, error_list

A = np.array([[10, 2, 1],
[1, 5, 1],
[2, 3, 10]], dtype=float)
b = np.array([7, -8, 6], dtype=float)

solution, errors = gauss_jacobi(A, b)

if solution is not None:


print("Solution:", solution)
print("Number of iterations:", len(errors))

# Plot the error convergence


plt.figure(figsize=(10, 6))
plt.plot(range(len(errors)), errors, marker='o', linestyle='-')
plt.yscale('log')
plt.xlabel("Iteration")
plt.ylabel("Error")
plt.title("Convergence of Gauss-Jacobi Method")
plt.grid()
plt.show()

You might also like