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

Eigenvalue Calculation in Python

The document describes a Python program to find the eigenvalues of a 3x3 matrix. The program takes user input to define the matrix, then calculates the eigenvalues in two ways: by solving the characteristic equation derived from the matrix's determinant, and by using NumPy's linalg.eig function. Both methods produce the same three eigenvalues: -0.854101966249685, 0, 5.85410196624968, validating the program's logic.

Uploaded by

Good Boy
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)
12 views4 pages

Eigenvalue Calculation in Python

The document describes a Python program to find the eigenvalues of a 3x3 matrix. The program takes user input to define the matrix, then calculates the eigenvalues in two ways: by solving the characteristic equation derived from the matrix's determinant, and by using NumPy's linalg.eig function. Both methods produce the same three eigenvalues: -0.854101966249685, 0, 5.85410196624968, validating the program's logic.

Uploaded by

Good Boy
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

1) Suppose that we have the following matrix:

Write a Python program to find the Eigenvalues of the above matrix.

Code
import numpy as np
from sympy import symbols, Matrix
from [Link] import solve
from math import sqrt

n = int(input('Enter matrix size: '))

# Creating augmented matrix of n x n size and initializing to zero


X = [Link]((n,n))

print('Enter the Matrix:')


for i in range(n):
for j in range(n):
X[i][j] = float(input( 'X['+str(i)+']['+ str(j)+']='))

#---------------------------------------------------------------------------------
# Solving Using Logic
# a, b, y = symbols(['a', 'b', 'y']) # Turn a, b, y into symbolic variables

l = symbols('l')
#X = Matrix([[2,1,3],[-1,6,0],[-2,-1,-3]]) # Define the matrix
li = Matrix([[l,0,0],[0,l,0],[0,0,l]]) # Matrix -> Lambda * I
A = X - li

# print([Link](), type([Link]())) # Print its determinant

print([Link]()) # Printing the Determinant

eigenvalues = solve([Link](),l) # Finding roots for |det(A)| = 0


eigenvalues

#---------------------------------------OR----------------------------------------------
# Using [Link] Library

# importing numpy library


import numpy as np

# create numpy 2d-array


m = [Link]([[2, 1, 3],[-1, 6, 0],[-2, -1, -3]])
# m = [Link]([[1, -1, 0], [0, 1, 0], [1, 0, 0]])

print("Printing the Original square array:\n", m)

# finding eigenvalues and eigenvectors


w, v = [Link](m)

# printing eigen values


print("Printing the Eigen values of the given square array:\n", w)

# printing eigen vectors


print("Printing Right eigenvectors of the given square array:\n", v)

OUTPUT:
Enter matrix size: 3
Enter the Matrix:
X[0][0]=2
X[0][1]=1
X[0][2]=3
X[1][0]=-1
X[1][1]=6
X[1][2]=0
X[2][0]=-2
X[2][1]=-1
X[2][2]=-3
Equation: −𝑙3+5.0𝑙2+5.0𝑙 = 0
Eigenvalues = [-0.854101966249685, 0.0, 5.85410196624968]

SCREENSHOT OF INPUT:

SCREENSHOT OF OUTPUT: (using Logic)


Using Library – [Link]

Therefore, the eigenvalues = 0.854101966249685, 0.0, 5.85410196624968


are same from the my logic and pre-defined. Thus, the logic is correct.

You might also like