Bird
Raised Fist0
MATLABdata~10 mins

Eigenvalues and eigenvectors (eig) in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Eigenvalues and eigenvectors (eig)
Input matrix A
↓
Call eig(A)
↓
Compute eigenvalues and eigenvectors
↓
Return eigenvalues (D) and eigenvectors (V)
↓
Use or display results
Start with a matrix A, call eig(A) to find its eigenvalues and eigenvectors, then use or display these results.
Execution Sample
MATLAB
A = [2 1; 1 2];
[V,D] = eig(A);
disp(D);
disp(V);
This code finds eigenvalues and eigenvectors of matrix A and displays them.
Execution Table
StepActionMatrix/VariableResult/Value
1Define matrix AA[2 1; 1 2]
2Call eig(A)eig(A)Compute eigenvalues and eigenvectors
3Calculate eigenvaluesD[1 0; 0 3] (diagonal matrix)
4Calculate eigenvectorsV[ -0.7071 0.7071; 0.7071 0.7071 ]
5Display Ddisp(D)Shows eigenvalues on diagonal
6Display Vdisp(V)Shows eigenvectors as columns
7EndAll eigenvalues and eigenvectors computed
💡 All eigenvalues and eigenvectors computed and displayed
Variable Tracker
VariableStartAfter eig callFinal
A[2 1; 1 2][2 1; 1 2][2 1; 1 2]
Dundefined[1 0; 0 3][1 0; 0 3]
Vundefined[-0.7071 0.7071; 0.7071 0.7071][-0.7071 0.7071; 0.7071 0.7071]
Key Moments - 3 Insights
Why are eigenvalues shown in a diagonal matrix D instead of a simple list?
In the execution_table row 3, eigenvalues are stored in a diagonal matrix D because MATLAB's eig function returns them this way to align with eigenvectors in V.
Why are eigenvectors shown as columns in matrix V?
As shown in row 4, each column of V corresponds to an eigenvector matching the eigenvalue in the same column of D, so columns represent eigenvectors.
What does the eig function actually compute?
From rows 2-4, eig computes values λ and vectors v such that A*v = λ*v, which are the eigenvalues and eigenvectors.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does matrix D represent?
AMatrix of eigenvectors
BThe original matrix A
CA diagonal matrix of eigenvalues
DA matrix of zeros
💡 Hint
Refer to execution_table row 3 where D is described as a diagonal matrix of eigenvalues
At which step are eigenvectors first computed according to the execution_table?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check execution_table row 4 where eigenvectors V are calculated
If matrix A was changed to a 3x3 matrix, how would the size of D and V change?
AD would be 3x1 and V would be 3x3
BD and V would both be 3x3 matrices
CD would be 3x3 and V would be 3x1
DD and V would both be 1x3 matrices
💡 Hint
Refer to variable_tracker showing D and V are square matrices matching size of A
Concept Snapshot
eig(A) returns eigenvalues and eigenvectors of matrix A.
Syntax: [V,D] = eig(A)
D is diagonal matrix of eigenvalues.
Columns of V are eigenvectors.
Used to solve A*v = lambda*v.
Full Transcript
We start with a matrix A. When we call eig(A), MATLAB calculates eigenvalues and eigenvectors. The eigenvalues are returned in a diagonal matrix D, where each diagonal element is an eigenvalue. The eigenvectors are returned as columns in matrix V, each corresponding to an eigenvalue in D. We display D and V to see the results. This process helps us understand how matrix A transforms vectors by scaling them along eigenvectors.

Practice

(1/5)
1. What does the MATLAB function eig return when applied to a square matrix?
easy
A. Eigenvalues and eigenvectors of the matrix
B. Only the eigenvalues of the matrix
C. Only the eigenvectors of the matrix
D. The inverse of the matrix

Solution

  1. Step 1: Understand the purpose of eig

    The eig function in MATLAB is designed to compute eigenvalues and eigenvectors of a square matrix.
  2. Step 2: Recall the output of eig

    When called with two output arguments, [V,D] = eig(A), it returns eigenvectors in V and eigenvalues in diagonal matrix D.
  3. Final Answer:

    Eigenvalues and eigenvectors of the matrix -> Option A
  4. Quick Check:

    eig returns 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
A. [V,D] = eig(A);
B. eig = [V,D](A);
C. V, D = eig(A);
D. eig(A) -> [V,D];

Solution

  1. Step 1: Recall MATLAB function call syntax

    In MATLAB, functions return outputs using square brackets for multiple outputs, e.g., [out1, out2] = function(input);.
  2. Step 2: Match correct syntax for eig

    The correct way to get eigenvectors V and eigenvalues D is [V,D] = eig(A);.
  3. Final Answer:

    [V,D] = eig(A); -> Option A
  4. Quick 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
A. [[5 0]; [0 0]]
B. [[3 0]; [0 2]]
C. [[2 0]; [0 3]]
D. [[0 2]; [3 0]]

Solution

  1. Step 1: Identify eigenvalues of matrix A

    Matrix A is diagonal with entries 2 and 3, so eigenvalues are 2 and 3.
  2. Step 2: Understand output of D from eig

    D is a diagonal matrix with eigenvalues on the diagonal, so D = [2 0; 0 3].
  3. Final Answer:

    [[2 0]; [0 3]] -> Option C
  4. Quick 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
A. The matrix A must be square and symmetric
B. The variable name eigenvalues is invalid
C. The function eig requires three output arguments
D. The eig function does not accept 'vector' as a second argument

Solution

  1. Step 1: Check eig function arguments

    The MATLAB eig function does not take a second argument like 'vector'. It only takes the matrix as input.
  2. Step 2: Identify correct usage to get eigenvalues as vector

    To get eigenvalues as a vector, simply call eig(A) without extra arguments.
  3. Final Answer:

    The eig function does not accept 'vector' as a second argument -> Option D
  4. Quick 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
A. largestVec = eig(A);
B. [V,D] = eig(A); [~, idx] = max(diag(D)); largestVec = V(:, idx);
C. [V,D] = eig(A); largestVec = D(:,1);
D. [V,D] = eig(A); largestVec = max(V);

Solution

  1. Step 1: Compute eigenvalues and eigenvectors

    Use [V,D] = eig(A); to get eigenvectors V and eigenvalues in diagonal matrix D.
  2. Step 2: Find index of largest eigenvalue

    Extract eigenvalues with diag(D), then find index of max eigenvalue with [~, idx] = max(diag(D));.
  3. Step 3: Select eigenvector corresponding to largest eigenvalue

    Use index to get eigenvector: largestVec = V(:, idx);.
  4. Final Answer:

    [V,D] = eig(A); [~, idx] = max(diag(D)); largestVec = V(:, idx); -> Option B
  5. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard