0% found this document useful (0 votes)
31 views

Unit - I - MATLAB Coding

Matlab coding begginer

Uploaded by

samunnatgrg6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Unit - I - MATLAB Coding

Matlab coding begginer

Uploaded by

samunnatgrg6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Eigenvalues and Eigenvectors

% Define a square matrix

A = [2, 0, 1;

0, 2, 0;

1, 0, 2];

% Calculate eigenvalues and eigenvectors

[eigenvectors, eigenvalues_matrix] = eig(A);

% Display results

disp('Matrix A:');

disp(A);

disp('Eigenvalues:');

disp(diag(eigenvalues_matrix));

disp('Eigenvectors:');

disp(eigenvectors);

 A: Defines a 3x3 square matrix.

 eig(A): Computes the eigenvalues and eigenvectors of matrix A.

o eigenvalues_matrix contains the eigenvalues on its diagonal.

o eigenvectors contains the corresponding eigenvectors as its columns.

 disp: Displays the results.

Properties of Eigenvalues and Eigenvectors

% 1. The sum of the eigenvalues is equal to the trace of the matrix.

% 2. The product of the eigenvalues is equal to the determinant of the matrix.


% 3. Eigenvectors corresponding to distinct eigenvalues are linearly independent.

clear all;

clc;

A = [6, -2, 2;

-2, 3, -1;

2, -1, 3];

% Verify properties

trace_A = trace(A);

det_A = det(A);

sum_eigenvalues = sum(diag(eigenvalues_matrix));

prod_eigenvalues = prod(diag(eigenvalues_matrix));

disp('Trace of A:');

disp(trace_A);

disp('Sum of eigenvalues:');

disp(sum_eigenvalues);

disp('Determinant of A:');

disp(det_A);

disp('Product of eigenvalues:');

disp(prod_eigenvalues);

 trace(A): Computes the trace (sum of diagonal elements) of matrix A.

 det(A): Computes the determinant of matrix A.

 sum(diag(eigenvalues_matrix)): Computes the sum of the eigenvalues.

 prod(diag(eigenvalues_matrix)): Computes the product of the eigenvalues.


 Verifies that:

o The sum of the eigenvalues equals the trace.

o The product of the eigenvalues equals the determinant.

Cayley-Hamilton Theorem

% The Cayley-Hamilton theorem states that every square matrix satisfies its own
characteristic equation.

% Calculate characteristic polynomial of A

char_poly = poly(A);

disp('Characteristic Polynomial Coefficients:');

disp(char_poly);

% Verify Cayley-Hamilton theorem

char_eq = polyvalm(char_poly, A); % Evaluate the polynomial with matrix A

disp('Verification of Cayley-Hamilton Theorem (should be close to zero matrix):');

disp(char_eq);

 poly(A): Computes the characteristic polynomial coefficients of matrix A.

 polyvalm(char_poly, A): Evaluates the characteristic polynomial using matrix A. This


should result in the zero matrix according to the Cayley-Hamilton theorem.

Diagonalization using Orthogonal Transformation

% For a symmetric matrix A, diagonalization can be achieved using an orthogonal matrix Q

% Define a symmetric matrix

B = [4, 1, 2;

1, 3, 0;

2, 0, 5];
% Calculate eigenvalues and eigenvectors

[Q, D] = eig(B);

% Verify orthogonality

orthogonality_check = Q' * Q; % Should be identity matrix

diagonalization_check = Q * D * Q'; % Should be the original matrix B

disp('Orthogonal Matrix Q:');

disp(Q);

disp('Diagonal Matrix D:');

disp(D);

disp('Check Q'' * Q (should be identity matrix):');

disp(orthogonality_check);

disp('Check Q * D * Q'' (should be original matrix B):');

disp(diagonalization_check);

 B: Defines a symmetric 3x3 matrix.

 eig(B): Computes the eigenvalues and eigenvectors of matrix B. For a symmetric


matrix, the eigenvectors are orthogonal.

o Q: Orthogonal matrix whose columns are the eigenvectors of B.

o D: Diagonal matrix with the eigenvalues of B.

 Q' * Q: Checks the orthogonality of Q (should be the identity matrix).

 Q * D * Q': Reconstructs the original matrix B using the diagonalization.

% Define the matrix A

A = [2 0 1; 0 2 0; 1 0 2];
% Find the eigenvalues and eigenvectors

[V, D] = eig(A);

% V is the matrix of eigenvectors

% D is the diagonal matrix with eigenvalues

% Display eigenvalues and eigenvectors

disp('Eigenvalues (diagonal of D):');

disp(diag(D));

disp('Eigenvectors (columns of V):');

disp(V);

% Check the diagonalization: A = VDV^(-1)

A_reconstructed = V * D / V;

disp('Reconstructed A (should match original A):');

disp(A_reconstructed);

% Compute A^n, for example, n = 3

n = 3;

A_power_n = V * D^n / V;

disp(['A^', num2str(n), ':']);

disp(A_power_n);

You might also like