Calculating Eigenvalues and Eigenvectors in Numpy Scipy: Step-by-step Example for Engineers

Eigenvalues and eigenvectors are fundamental concepts in linear algebra, widely used in engineering applications such as stability analysis, vibration analysis, and system dynamics. Python libraries like NumPy and SciPy provide efficient tools to compute these values. This article presents a step-by-step example for engineers to calculate eigenvalues and eigenvectors using these libraries.

Setting Up the Environment

First, ensure that NumPy and SciPy are installed in your Python environment. You can install them using pip:

pip install numpy scipy

Creating the Matrix

Define the matrix for which you want to compute eigenvalues and eigenvectors. Typically, this matrix is square and real or complex.

For example, consider the following 3×3 matrix:

import numpy as np

A = np.array([[4, 1, 2],

[1, 3, 0],

[2, 0, 2]])

Calculating Eigenvalues and Eigenvectors

Use the SciPy function scipy.linalg.eig to compute eigenvalues and eigenvectors:

from scipy.linalg import eig

eigenvalues, eigenvectors = eig(A)

Interpreting the Results

The variable eigenvalues contains the eigenvalues of matrix A. The eigenvectors array contains the corresponding eigenvectors as columns.

To display the results:

print(“Eigenvalues:”, eigenvalues)

print(“Eigenvectors:”, eigenvectors)

Summary

Calculating eigenvalues and eigenvectors in Python using NumPy and SciPy involves defining the matrix, then applying the eig function. The results are essential for analyzing system properties in engineering applications.