0% found this document useful (0 votes)
10 views19 pages

MATLAB Matrix Operations and EVD Guide

The document outlines a MATLAB module focused on matrices, covering experiments such as solving systems of linear equations, finding eigenvalues and eigenvectors, and matrix decomposition. It includes various matrix operations, built-in functions, and methods for checking the consistency of systems, along with examples and exercises. The author, R Hari Priya, provides detailed MATLAB code snippets for each operation and exercise.

Uploaded by

haripriyar891
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

MATLAB Matrix Operations and EVD Guide

The document outlines a MATLAB module focused on matrices, covering experiments such as solving systems of linear equations, finding eigenvalues and eigenvectors, and matrix decomposition. It includes various matrix operations, built-in functions, and methods for checking the consistency of systems, along with examples and exercises. The author, R Hari Priya, provides detailed MATLAB code snippets for each operation and exercise.

Uploaded by

haripriyar891
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE 1 : Matrices.

Experiment 1: Solution of system of linear equations,Finding Eigen value ,Eigen vectors and
matrix decomposition using MATLAB.

Name : R Hari priya.

USN: JUUG25BTECH24071.

Date : 17\10\25

Section :CSE AI .

CREATION OF MATRICES:

A=[ 1 4 6; 9 10 12; 14 16 2]

A = 3×3
1 4 6
9 10 12
14 16 2

B=[1 4 6
9 10 12
14 16 2]

B = 3×3
1 4 6
9 10 12
14 16 2

ROW/COLUMN MATRICES :

a=[20 25 30 35]

a = 1×4
20 25 30 35

b=[20; 25; 30; 35]

b = 4×1
20
25
30
35

FIND A ROOT OF POLYNOMIAL :

P=[1, -7 , 40, -34];


X=roots(P)

X = 3×1 complex
3.0000 + 5.0000i
3.0000 - 5.0000i
1.0000 + 0.0000i

1
MATRIX BUILT IN FUNCTIONS :

eye(4,4)

ans = 4×4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

zeros(3,3)

ans = 3×3
0 0 0
0 0 0
0 0 0

ones(3)

ans = 3×3
1 1 1
1 1 1
1 1 1

diag(A)

ans = 3×1
1
10
2

triu(A)

ans = 3×3
1 4 6
0 10 12
0 0 2

tril(A)

ans = 3×3
1 0 0
9 10 0
14 16 2

size(A)

ans = 1×2
3 3

det(A)

ans =
452

inv(A)

ans = 3×3
-0.3805 0.1947 -0.0265
0.3319 -0.1814 0.0929

2
0.0088 0.0885 -0.0575

rank(A)

ans =
3

rref(A)

ans = 3×3
1 0 0
0 1 0
0 0 1

eig(A)

ans = 3×1
24.9490
-1.7819
-10.1671

poly(A)

ans = 1×4
1.0000 -13.0000 -280.0000 -452.0000

rand(3,3)

ans = 3×3
0.7431 0.1712 0.2769
0.3922 0.7060 0.0462
0.6555 0.0318 0.0971

randi(5, 2, 2)

ans = 2×2
5 2
4 5

numel(A)

ans =
9

nrow=3

nrow =
3

ncol=3

ncol =
3

numel(A)

ans =
9

det(A)

3
ans =
452

inv(A)

ans = 3×3
-0.3805 0.1947 -0.0265
0.3319 -0.1814 0.0929
0.0088 0.0885 -0.0575

A*inv(A)

ans = 3×3
1.0000 0 -0.0000
0.0000 1.0000 -0.0000
0.0000 0.0000 1.0000

inv(A)*A

ans = 3×3
1.0000 0 0.0000
0 1.0000 -0.0000
-0.0000 0 1.0000

A'

ans = 3×3
1 9 14
4 10 16
6 12 2

sym(A)

ans =

MATRIX OPERATIONS :

A + B

ans = 3×3
2 8 12
18 20 24
28 32 4

A - B

ans = 3×3
0 0 0
0 0 0
0 0 0

A * B

ans = 3×3

4
121 140 66
267 328 198
186 248 280

A .* B

ans = 3×3
1 16 36
81 100 144
196 256 4

A / B

ans = 3×3
1 0 0
0 1 0
0 0 1

A \ B

ans = 3×3
1 0 0
0 1 0
0 0 1

A ./ B

ans = 3×3
1 1 1
1 1 1
1 1 1

A .\ B

ans = 3×3
1 1 1
1 1 1
1 1 1

A .^ B

ans = 3×3
1019 ×
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
0.0011 1.8447 0.0000

Rank of a matrix:

A = [3 2 4; -1 1 2; 9 5 10];
R=rank(A)

R =
2

R = 2

5
R =
2

A = [1 2 1; 2 3 1; 1 1 2];
R=rank(A)

R =
3

R = 3

R =
3

Checking the consistency of the system and Solving the system of equation:

Problems 1.

Test for consistency and solve by using Gauss – Elimination Method.

clc; clear all; close all;

-------- User Input --------

A = input('Enter square matrix A (e.g. [4 1; 2 3]): ');


[n,m] = size(A);
if n ~= m
error('Matrix must be square.');
end

k = input('Enter the power k to compute: ');

-------- Convert to symbolic to avoid floating-point issues --------

A_sym = sym(A);
disp('Entered matrix is');
disp(A_sym)

-------- Eigenvalue decomposition --------

6
[P,D] = eig(A_sym); % eigenvectors in columns, eigenvalues diagonal

-------- Display eigenvalues & eigenvectors --------

disp('Eigenvalues (Diagonal matrix D):'); disp(diag(D));


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

-------- Check diagonalizability --------

if rank(double(P)) == n
disp('=> Matrix Is diagonalizable.');
clc; clear all; close all;
end

-------- User Input --------

A = input('Enter square matrix A (e.g. [4 1; 2 3]): ');


[n,m] = size(A);
if n ~= m
error('Matrix must be square.');
end

k = input('Enter the power k to compute: ');

-------- Convert to symbolic to avoid floating-point issues --------

A_sym = sym(A);
disp('Entered matrix is');
disp(A_sym)

-------- Eigenvalue decomposition --------

[P,D] = eig(A_sym); % eigenvectors in columns, eigenvalues diagonal

-------- Display eigenvalues & eigenvectors -------

7
disp('Eigenvalues (Diagonal matrix D):'); disp(diag(D));
disp('Eigenvectors (columns of P):'); disp(P);

-------- Check diagonalizability --------

if rank(double(P)) == n

disp('=> Matrix Is diagonalizable.');

Display EVD explicity

fprintf('\nEigenvalue Decomposition (EVD):\n');


fprintf('A = P * D * P^{-1}\n\n');
fprintf('P = \n'); disp(P);
fprintf('D = \n'); disp(D);
fprintf('P^-1 = \n'); disp(inv(P));

Reconstruct A and display

A_recon = P*D*inv(P);
fprintf('EVD of given A=P*D*P^-1:\n');
disp(vpa(A_recon, 6)); % 6-digit precision

Compute A^k using EVD


A_pow = P*(D^k)*inv(P);
fprintf('\nA^%d via EVD:\n', k);
disp(vpa(A_pow, 6));

else
disp('=> Matrix is NOT diagonalizable.');
fprintf('A^%d computed directly:\n', k);
disp(vpa(A_sym^k, 6));
end

Generalized Gauss-Seidel Method for Solving Linear Systems :

clr; clear all;close all;

-------- User Input with Examples -------

8
A = input('Enter diagonally dominant square matrix A{Example:[10 1 1; 1 10
1; 1 1 10]}:');
b = input('Enter right-hand side Matrix b {Example:[12; 12; 12]}: ');
x = input('Enter initial guess x0 {Example:[0; 0; 0]}: ');
max_iter = input('Enter maximum number of iterations {Example: 25}: ');

tol = 1e-6; % fixed tolerance

disp('Enterd augment Matrix is');


disp([A b]);

[n,m] = size(A);

-------- Condition Checks -------

if n ~= m
error('Matrix must be square.');
end

if any(diag(A) == 0)
error('Zero on diagonal! Cannot use Gauss-Seidel.');
end

% -------- Simple diagonal dominance check -------


for i = 1:n
if abs(A(i,i)) <= sum(abs(A(i,:))) - abs(A(i,i))
warning('Matrix A is not strictly diagonally dominant. Answer may
not converge.');
break;
end
end

-------- Gauss-Seidel Iteration -------

for k = 1:max_iter
x_old = x;
for i = 1:n
sum1 = (i>1)*A(i,1:i-1)*x(1:i-1);
sum2 = (i<n)*A(i,i+1:end)*x_old(i+1:end);
x(i) = (b(i)-sum1-sum2)/A(i,i);
end

% Display iteration
fprintf('\n Iteration %d:\n', k);
for i = 1:n

9
fprintf('x%d = %.8f ', i, x(i));
end
fprintf('\n');

% Convergence check
if norm(x - x_old, inf) < tol
fprintf('\nConverged in %d iterations.\n', k);
break;
end
end

if k == max_iter
fprintf('\nReached maximum iterations (%d) without full convergence.\n',
max_iter);
end

-------- Final Solution -------


disp('Final solution x = 1');
disp(x);

EXERCISE PROBLEMS :

1.

Excercise Problems
1. Create the below matrices

and evaluate the following

a.

A +B

A -B

b.

5A-3B+I

[Link]

d. Show that matrix multiplication is not commutative i.e.(AB=!BA)

A=[5 -1 3;2 4 -7;6 1 8]

A = 3×3

10
5 -1 3
2 4 -7
6 1 8

sym(A)

ans =

B=[12 0 7;3 -2 5;-1 9 10]

B = 3×3
12 0 7
3 -2 5
-1 9 10

sym(B)

ans =

C=A+B

C = 3×3
17 -1 10
5 2 -2
5 10 18

C1=A-B

C1 = 3×3
-7 -1 -4
-1 6 -12
7 -8 -2

C2=5*A-3*B+eye(3,3)

C2 = 3×3
-10 -5 -6
1 27 -50
33 -22 11

D=A*B

D = 3×3
54 29 60
43 -71 -36
67 70 127

D1=B*A

D1 = 3×3
102 -5 92

11
41 -6 63
73 47 14

if D==D1
error("some mismatched in matrices");
else
disp("Matrices multiplication is not cummutative i.e.(AB=!BA");
end

Matrices multiplication is not cummutative i.e.(AB=!BA

[Link] the consistency of the system and also solve the system

A = input('Enter the coefficient matrix A:(Example:[1 2;3 4]) ');


b = input('Enter the right-hand side vector (b:[2;3]) ');

disp('System of Linear Equations: A * x = b');

System of Linear Equations: A * x = b

disp('Coefficient Matrix A ='); disp(A);

Coefficient Matrix A =
10 1 1
2 10 1
1 1 5

disp('Right-hand side vector b ='); disp(b);

Right-hand side vector b =


12
13
7

disp('Augmented Matrix [A|b] ='); disp([A b]);

Augmented Matrix [A|b] =


10 1 1 12
2 10 1 13
1 1 5 7

if all(b==0)
disp('=> HOMOGENEOUS system (A*x = 0)');
else
disp('=> NON-HOMOGENEOUS system (A*x = b)');
end

=> NON-HOMOGENEOUS system (A*x = b)

12
rAb = rank([A b]);
rA = rank(A);
n = size(A,2);
fprintf('Rank(A) = %d\n', rA);

Rank(A) = 3

fprintf('Rank([A|b]) = %d\n', rAb);

Rank([A|b]) = 3

fprintf('Number of unknowns (n) = %d\n', n);

Number of unknowns (n) = 3

if rA ~= rAb
disp('=> INCONSISTENT system: No solution exists.');
else
disp('=> CONSISTENT system: Solution(s) exist.');

if rA == n
x = A\b;
disp('=> Unique solution:');
disp(x);
else
disp('=> Infinitely many solutions exist.');
vars = sym('x', [n 1]);
S = solve(A*vars == b, vars, 'ReturnConditions', true);
disp('General solution (with parameters):');
disp(S);
end
end

=> CONSISTENT system: Solution(s) exist.


=> Unique solution:
1.0000
1.0000
1.0000

if all(b==0)
if rA == n
disp('=> Only TRIVIAL solution exists (x=0).');
else
disp('=> NON-TRIVIAL solutions exist (infinitely many).');
end

end

[Link] the eigenvalues and eigenvectors of A

A=[6 -2 2;-2 3 1;2 -1 3]

13
A = 3×3
6 -2 2
-2 3 1
2 -1 3

sym(A)

ans =

[v,D]=eig(A)

v = 3×3
0.8593 -0.4472 0.3879
-0.2775 -0.8944 0.9011
0.4297 0.0000 0.1940
D = 3×3
7.6458 0 0
0 2.0000 0
0 0 2.3542

[Link] the matrix A and find A^4

A=[8 -6 2 ;-6 7 -4;2 -4 3]

A = 3×3
8 -6 2
-6 7 -4
2 -4 3

sym(A)

ans =

[P,D]=eig(A)

P = 3×3
0.3333 0.6667 -0.6667
0.6667 0.3333 0.6667
0.6667 -0.6667 -0.3333
D = 3×3
0.0000 0 0
0 3.0000 0
0 0 15.0000

disp("the diagonaloised mat A is ")

the diagonaloised mat A is

14
A=P*D*P^-1

A = 3×3
8.0000 -6.0000 2.0000
-6.0000 7.0000 -4.0000
2.0000 -4.0000 3.0000

[Link] the eigenvalue decomposition of the matrix and hence find value of A^3

clc;clear all;close all;


A = input('Enter square matrix A (e.g. [4 1; 2 3]): ');
[n,m] = size(A);
if n ~= m
error('Matrix must be square.');
end
k = input('Enter the power k to compute: ');
A_sym = sym(A);
disp('Entered matrix is');

Entered matrix is

disp(A_sym)

[P,D] = eig(A_sym);
disp('Eigenvalues (Diagonal matrix D):'); disp(diag(D));

Eigenvalues (Diagonal matrix D):

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

Eigenvectors (columns of P):

if rank(double(P)) == n
disp('=> Matrix Is diagonalizable.');
fprintf('\nEigenvalue Decomposition (EVD):\n');
fprintf('A = P * D * P^{-1}\n\n');
fprintf('P = \n'); disp(P);
fprintf('D = \n'); disp(D);
fprintf('P^-1 = \n'); disp(inv(P));
A_recon = P*D*inv(P);

15
fprintf('EVD of given A=P*D*P^-1:\n');
disp(vpa(A_recon, 6));
A_pow=P*(D^k)*inv(P);
fprintf('\nA^%d via EVD:\n', k);
disp(vpa(A_pow, 6));

else
disp('=> Matrix is NOT diagonalizable.');
fprintf('A^%d computed directly:\n', k);
disp(vpa(A_sym^k, 6));
end

=> Matrix Is diagonalizable.


Eigenvalue Decomposition (EVD):
A = P * D * P^{-1}
P =

D =

P^-1 =

EVD of given A=P*D*P^-1:

A^3 via EVD:

[Link] the system of equations x+y+54z=110; 27x+6y-z=85; 6x+15y+2z=72, by the Gauss-Seidel

method to obtain the final solution correct to three decimal places by performing 4 iterations

clc;clear all; close all;

16
A = input('Enter diagonally dominant square matrix A{Example:[10 1 1; 1 10
1; 1 1 10]}:');
b = input('Enter right-hand side Matrix b {Example:[12; 12; 12]}: ');
x = input('Enter initial guess x0 {Example:[0; 0; 0]}: ');
max_iter = input('Enter maximum number of iterations {Example: 25}: ');
tol = 1e-6;
disp('Enterd augment Matrix is');

Enterd augment Matrix is

disp([A b]);

1 1 54 110
27 6 -1 85
6 15 2 72

[n,m] = size(A);
if n ~= m
error('Matrix must be square.');
end
if any(diag(A) == 0)
error('Zero on diagonal! Cannot use Gauss-Seidel.');
end
for i = 1:n
if abs(A(i,i)) <= sum(abs(A(i,:))) - abs(A(i,i))
warning('Matrix A is not strictly diagonally dominant. Answer may not
converge.');
break;
end
end

Warning: Matrix A is not strictly diagonally dominant. Answer may not converge.

for k = 1:max_iter
x_old = x;
for i = 1:n
sum1 = (i>1)*A(i,1:i-1)*x(1:i-1);
sum2 = (i<n)*A(i,i+1:end)*x_old(i+1:end);
x(i) = (b(i)-sum1-sum2)/A(i,i);
end
fprintf('\n Iteration %d:\n', k);
for i = 1:n
fprintf('x%d = %.8f ', i, x(i));
end
fprintf('\n');
if norm(x - x_old, inf) < tol
fprintf('\nConverged in %d iterations.\n', k);
break;
end
end

Iteration 1:
x1 = 110.00000000 x2 = -480.83333333 x3 = 3312.25000000

17
Iteration 2:
x1 = -178270.66666667 x2 = 802784.20833333 x3 = -5486033.56250000
Iteration 3:
x1 = 295443138.16666669 x2 = -1330408446.51041675 x3 = 9091733970.32812500
Iteration 4:
x1 = -489623225841.20831299 x2 = 2204819805294.65869141 x3 = -15067278862150.31445312

if k == max_iter
fprintf('\nReached maximum iterations (%d) without full convergence.\n',
max_iter);
end

Reached maximum iterations (4) without full convergence.

disp('Final solution x = ');

Final solution x =

disp(x);

1.0e+13 *

-0.0490
0.2205
-1.5067

7. Solve the system of equations 5x+2y+z=12; x+4y+2z=15; x+2y+5z=20, by the Gauss-Seidel

method, and carry out 4 iterations taking the initial approximation to the solution as (1, 0, 3).

A = input('Enter diagonally dominant square matrix A{Example:[10 1 1; 1 10


1; 1 1 10]}:');
b = input('Enter right-hand side Matrix b {Example:[12; 12; 12]}: ');
x = input('Enter initial guess x0 {Example:[0; 0; 0]}: ');
max_iter = input('Enter maximum number of iterations {Example: 25}: ');
tol = 1e-6;
disp('Enterd augment Matrix is');

Enterd augment Matrix is

disp([A b]);

5 2 1 12
1 4 2 15
1 2 5 20

[n,m] = size(A);
if n ~= m
error('Matrix must be square.');
end
if any(diag(A) == 0)
error('Zero on diagonal! Cannot use Gauss-Seidel.');
end

18
for i = 1:n
if abs(A(i,i)) <= sum(abs(A(i,:))) - abs(A(i,i))
warning('Matrix A is not strictly diagonally dominant. Answer may not
converge.');
break;
end
end
for k = 1:max_iter
x_old = x;
for i = 1:n
sum1 = (i>1)*A(i,1:i-1)*x(1:i-1);
sum2 = (i<n)*A(i,i+1:end)*x_old(i+1:end);
x(i) = (b(i)-sum1-sum2)/A(i,i);
end
fprintf('\n Iteration %d:\n', k);
for i = 1:n
fprintf('x%d = %.8f ', i, x(i));
end
fprintf('\n');
if norm(x - x_old, inf) < tol
fprintf('\nConverged in %d iterations.\n', k);
break;
end
end

Iteration 1:
x1 = 1.80000000 x2 = 1.80000000 x3 = 2.92000000
Iteration 2:
x1 = 1.09600000 x2 = 2.01600000 x3 = 2.97440000
Iteration 3:
x1 = 0.99872000 x2 = 2.01312000 x3 = 2.99500800
Iteration 4:
x1 = 0.99575040 x2 = 2.00355840 x3 = 2.99942656

if k == max_iter
fprintf('\nReached maximum iterations (%d) without full convergence.\n',
max_iter);
end

Reached maximum iterations (4) without full convergence.

disp('Final solution x = ');

Final solution x =

disp(x);

0.9958
2.0036
2.9994

19

You might also like