Eigenvalues and Linear Equations
Learn how to calculate eigenvalues and eigenvectors using NumPy, solve systems of linear equations, and understand the role of matrix operations in scientific computing. Explore np.linalg.eig(), np.linalg.solve(), and practical examples with step-by-step explanations.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand eigenvalues and eigenvectors.
- Calculate eigenvalues and eigenvectors with NumPy.
- Interpret the output of np.linalg.eig().
- Represent linear equations using matrices and vectors.
- Solve linear systems using np.linalg.solve().
- Check whether a calculated solution satisfies a system.
- Understand what happens when a system has no unique solution.
- Apply these operations to practical numerical problems.
2. Introduction to Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors describe important properties of a square matrix. For a matrix A, an eigenvector is a non-zero vector whose direction remains unchanged when A multiplies it, although its magnitude may change.
The relationship is represented by the equation:
Eigenvalue Equation
Where:
- A: The square matrix.
- v: An eigenvector.
- lambda: The corresponding eigenvalue.
The eigenvalue describes how the eigenvector is scaled by the matrix transformation. Eigenvalues and eigenvectors are used in scientific computing, data analysis, physics, and machine learning.
An eigenvector is scaled by the matrix transformation without changing its direction.
3. Calculating Eigenvalues and Eigenvectors
NumPy provides np.linalg.eig() to calculate the eigenvalues and corresponding eigenvectors of a square matrix.
Output:
The eigenvalues are 4 and 2. Each column in the eigenvector matrix corresponds to the eigenvalue at the same position in the eigenvalue array.
4. Understanding the Eigenvalue Equation
For an eigenvalue lambda and its corresponding eigenvector v, multiplying A by v should produce the same result as multiplying v by lambda.
Output:
Both sides match for each eigenvalue and eigenvector pair. This verifies the eigenvalue relationship for the example.
5. Eigenvalues of a General Matrix
Eigenvalues are not always integers, and eigenvectors may contain several non-zero components. Consider the following matrix:
Output:
The eigenvalues are 3 and 1. The eigenvector directions correspond to the two columns returned by NumPy.
6. Introduction to Linear Equations
A linear equation contains variables raised only to the first power. A system of linear equations contains two or more equations that must be satisfied simultaneously.
Example System
We can represent this system using a coefficient matrix A, an unknown vector x, and a result vector b.
Representing equations as matrices makes them easier to solve using numerical linear algebra functions.
7. Solving Linear Equations with np.linalg.solve()
NumPy provides np.linalg.solve(A, b) to solve a square linear system Ax = b when the coefficient matrix has a unique solution.
Output:
The solution is x = 1.8 and y = 1.4. These values satisfy both original equations.
8. Verifying a Linear Equation Solution
After calculating a solution, multiply the coefficient matrix by the solution vector. The result should be approximately equal to the original right-hand-side vector.
Output:
np.allclose() checks whether the calculated values are sufficiently close to the expected values, accounting for floating-point precision.
9. Systems with No Unique Solution
A square linear system may fail to have a unique solution when its coefficient matrix is singular. The equations may be inconsistent or may have infinitely many solutions.
Example: Dependent Equations
The second equation is exactly twice the first equation. Both equations describe the same line, so there are infinitely many solutions rather than one unique solution.
Output:
10. Solving a Three-Variable System
NumPy can also solve systems containing three or more unknowns, provided the coefficient matrix and right-hand-side vector have compatible dimensions and the system has a unique solution.
Output:
The solution is x = 2, y = 3, and z = 1. Multiplying the coefficient matrix by the solution reproduces the right-hand-side values.
11. Practical Example: Student Performance Analysis
Activity Description
At CIIT TRANING INSTITUTE, Samadhan, Pradnya, and Samiksha are reviewing a numerical model that combines three subject scores. The team can represent the subject relationships using a matrix and use NumPy to solve for unknown values.
In this example, suppose a simple model is described by the equations:
Sample Solution
Output:
The calculated vector contains the unknown values of the mathematical model. The names are included to demonstrate how a team might review a numerical result.
12. Eigenvalues vs Linear Equation Solving
| Feature | Eigenvalues and Eigenvectors | Linear Equation Solving |
|---|---|---|
| Purpose | Understand matrix transformations and their special directions. | Find unknown values that satisfy a system of equations. |
| NumPy function | np.linalg.eig() |
np.linalg.solve() |
| Input | A square matrix | A coefficient matrix and a right-hand-side vector |
| Output | Eigenvalues and corresponding eigenvectors | A solution vector |
| Common application | Scientific computing, transformations, and data analysis | Solving mathematical and engineering systems |
13. Common Mistakes
Mistake 1: Using a Non-Square Matrix for Eigenvalues
The standard eigenvalue decomposition in np.linalg.eig() requires a square matrix.
Mistake 2: Misreading Eigenvector Columns
NumPy returns eigenvectors as columns. The i-th eigenvector corresponds to the i-th eigenvalue.
Mistake 3: Mixing Up A and b
In Ax = b, A is the coefficient matrix and b is the right-hand-side vector. Their dimensions must be compatible.
Mistake 4: Assuming Every System Has a Unique Solution
A singular coefficient matrix may lead to no solution or infinitely many solutions.
Mistake 5: Comparing Floating-Point Results Exactly
Use np.allclose() when verifying computed numerical results instead of relying on exact floating-point equality.
14. Practice Exercises
Exercise 1: Calculate Eigenvalues
Create the matrix [[3, 0], [0, 5]] and calculate its eigenvalues and eigenvectors using np.linalg.eig().
Exercise 2: Verify an Eigenvector
Select an eigenvalue and its corresponding eigenvector. Check whether A @ v is approximately equal to lambda * v.
Exercise 3: Solve Two Linear Equations
Solve the system 3x + y = 7 and x + 2y = 5 using np.linalg.solve().
Exercise 4: Verify the Solution
Multiply the coefficient matrix by your solution vector. Use np.allclose() to compare the result with b.
Exercise 5: Identify a Singular System
Try solving x + y = 2 and 2x + 2y = 5. Explain why the system has no solution.
Exercise 6: Three Unknowns
Create a 3 × 3 coefficient matrix and a three-element right-hand-side vector. Solve the system and verify the result.
Summary
In this lesson, you learned how to calculate eigenvalues
and eigenvectors using np.linalg.eig().
You explored how eigenvectors relate to matrix
transformations through the equation A @ v = lambda * v.
You also learned to represent systems of linear equations
using matrices and vectors, solve them with
np.linalg.solve(), and verify numerical
solutions using np.allclose().