Eigenvalues and eigenvectors help us understand important properties of a matrix, like how it stretches or rotates space.
Eigenvalues and eigenvectors (eig) in MATLAB
Introduction
Syntax
MATLAB
[V,D] = eig(A)
A is a square matrix.
V is a matrix whose columns are eigenvectors of A.
D is a diagonal matrix with eigenvalues of A on the diagonal.
Examples
MATLAB
A = [2 1; 1 2]; [V,D] = eig(A);
MATLAB
A = [4 0 0; 0 3 0; 0 0 2]; [V,D] = eig(A);
MATLAB
A = [0 -1; 1 0]; [V,D] = eig(A);
Sample Program
This program calculates and shows eigenvalues and eigenvectors of matrix A.
MATLAB
A = [3 1; 0 2]; [V,D] = eig(A); disp('Eigenvalues:'); disp(diag(D)); disp('Eigenvectors:'); disp(V);
Important Notes
Eigenvalues can be real or complex numbers.
Eigenvectors are usually normalized (length 1) in MATLAB output.
Order of eigenvalues in D matches columns of V.
Summary
Use eig to find eigenvalues and eigenvectors of square matrices.
Eigenvalues tell how the matrix scales vectors; eigenvectors show directions.
Useful in many fields like data analysis, physics, and engineering.
Practice
1. What does the MATLAB function
eig return when applied to a square matrix?easy
Solution
Step 1: Understand the purpose of
Theeigeigfunction in MATLAB is designed to compute eigenvalues and eigenvectors of a square matrix.Step 2: Recall the output of
When called with two output arguments,eig[V,D] = eig(A), it returns eigenvectors inVand eigenvalues in diagonal matrixD.Final Answer:
Eigenvalues and eigenvectors of the matrix -> Option AQuick Check:
eigreturns eigenvalues and eigenvectors [OK]
Hint: Remember: eig returns both eigenvalues and eigenvectors [OK]
Common Mistakes:
- Thinking eig returns only eigenvalues
- Confusing eig with inverse function
- Assuming eig returns a scalar
2. Which of the following is the correct syntax to get eigenvalues and eigenvectors of matrix
A in MATLAB?easy
Solution
Step 1: Recall MATLAB function call syntax
In MATLAB, functions return outputs using square brackets for multiple outputs, e.g.,[out1, out2] = function(input);.Step 2: Match correct syntax for
The correct way to get eigenvectorseigVand eigenvaluesDis[V,D] = eig(A);.Final Answer:
[V,D] = eig(A); -> Option AQuick Check:
Use square brackets for multiple outputs [OK]
Hint: Use square brackets for multiple outputs in MATLAB [OK]
Common Mistakes:
- Using commas instead of brackets for outputs
- Assigning outputs incorrectly
- Using arrow notation which is invalid in MATLAB
3. Given the matrix
A = [2 0; 0 3];, what is the output of [V,D] = eig(A); D?medium
Solution
Step 1: Identify eigenvalues of matrix A
MatrixAis diagonal with entries 2 and 3, so eigenvalues are 2 and 3.Step 2: Understand output of
DfromeigDis a diagonal matrix with eigenvalues on the diagonal, soD = [2 0; 0 3].Final Answer:
[[2 0]; [0 3]] -> Option CQuick Check:
Diagonal matrix D has eigenvalues 2 and 3 [OK]
Hint: Eigenvalues of diagonal matrix are diagonal entries [OK]
Common Mistakes:
- Mixing order of eigenvalues
- Confusing eigenvalues with eigenvectors
- Expecting eigenvalues as a vector instead of diagonal matrix
4. What is wrong with this MATLAB code snippet to get eigenvalues of matrix
A?
eigenvalues = eig(A, 'vector');
medium
Solution
Step 1: Check
The MATLABeigfunction argumentseigfunction does not take a second argument like 'vector'. It only takes the matrix as input.Step 2: Identify correct usage to get eigenvalues as vector
To get eigenvalues as a vector, simply calleig(A)without extra arguments.Final Answer:
Theeigfunction does not accept 'vector' as a second argument -> Option DQuick Check:
Check function syntax carefully [OK]
Hint: Check MATLAB function syntax in docs before adding arguments [OK]
Common Mistakes:
- Adding unsupported arguments to eig
- Assuming eig returns vector by default without output assignment
- Confusing eig with other functions
5. You have a matrix
A = [4 1 0; 1 4 1; 0 1 4];. How can you find the eigenvector corresponding to the largest eigenvalue using MATLAB?hard
Solution
Step 1: Compute eigenvalues and eigenvectors
Use[V,D] = eig(A);to get eigenvectorsVand eigenvalues in diagonal matrixD.Step 2: Find index of largest eigenvalue
Extract eigenvalues withdiag(D), then find index of max eigenvalue with[~, idx] = max(diag(D));.Step 3: Select eigenvector corresponding to largest eigenvalue
Use index to get eigenvector:largestVec = V(:, idx);.Final Answer:
[V,D] = eig(A); [~, idx] = max(diag(D)); largestVec = V(:, idx); -> Option BQuick Check:
Index max eigenvalue, select eigenvector column [OK]
Hint: Use max(diag(D)) to find largest eigenvalue index [OK]
Common Mistakes:
- Using eigenvalues matrix D as vector directly
- Selecting wrong column of eigenvectors
- Confusing eigenvalues with eigenvectors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
