Matrix

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

MATLAB Course No-MAT150 (Lab)

Matrix Algebra

Question 1: Write a matlab program to determine the determinant of a matrix whose input
data are given randomly by keyboard

Answer:
clc
clear all

disp('Enter the first row')


a11=input('a11=');
a12=input('a12=');
a13=input('a13=');
%--------------------------------
disp('Enter the second row')
a21=input('a21=');
a22=input('a22=');
a23=input('a23=');
%--------------------------------
disp('Enter the third row')
a31=input('a31=');
a32=input('a32=');
a33=input('a33=');
%--------------------------------
A=[a11 a12 a13 ; a21 a22 a23 ; a31 a32 a33 ]
detA=det(A);
fprintf('The determinant of the given matrix is %f', detA);

Question 2: Write a matlab program to find the inverse of a 3  3 matrix whose input data
are given randomly by keyboard

Answer:
clc
clear all

disp('Enter the first row')


a11=input('a11=');
a12=input('a12=');
a13=input('a13=');
%--------------------------------
disp('Enter the second row')
a21=input('a21=');
a22=input('a22=');
a23=input('a23=');
%--------------------------------
disp('Enter the third row')
a31=input('a31=');
a32=input('a32=');
a33=input('a33=');
%--------------------------------
% format short
% format long
A=[a11 a12 a13 ; a21 a22 a23 ; a31 a32 a33 ]
Det_A=det(A);
fprintf('The determinant of the given matrix is %f', Det_A);
%--------------------------------
if Det_A==0
disp('Matrix A is singular, it has no inverse')
else
A11=(-1)^(1+1)*det([a22 a23 ; a32 a33]);
A12=(-1)^(1+2)*det([a21 a23 ; a31 a33]);
A13=(-1)^(1+3)*det([a21 a22 ; a31 a32]);
A21=(-1)^(2+1)*det([a12 a13 ; a32 a33]);
A22=(-1)^(2+2)*det([a11 a13 ; a31 a33]);
A23=(-1)^(2+3)*det([a11 a12 ; a31 a32]);
A31=(-1)^(3+1)*det([a12 a13 ; a22 a23]);
A32=(-1)^(3+2)*det([a11 a13 ; a21 a23]);
A33=(-1)^(3+3)*det([a11 a12 ; a21 a22]);

Adj_A=[A11 A12 A13; A21 A22 A23 ; A31 A32 A33]';


end
Inverse_A=Adj_A/Det_A

Question 3: Write a matlab code of 3  3 matrix whose elements are random. Also write it’s
all cofactor matrices. Use cofactor of A find its determinant also verify it.

Answer:
clc
clear all
A=rand(3)
A11=(-1)^(1+1)*[A(2:3,2) A(2:3,3)];
a11=det(A11);
A12=(-1)^(1+2)*[A(2:3,1) A(2:3,3)];
a12=det(A12);
A13=(-1)^(1+3)*[A(2:3,1) A(2:3,2)];
a13=det(A13);

A21=(-1)^(2+1)*[A(1,2:3) ; A(3,2:3)];
a21=det(A21);
A22=(-1)^(2+2)*[A(1,1) A(1,3); A(3,1) A(3,3)];
a22=det(A22);
A23=(-1)^(2+3)*[A(1,1:2) ; A(3,1:2)];
a23=det(A23);

A31=(-1)^(3+1)*[A(1,2:3) ; A(2,2:3)];
a31=det(A31);
A32=(-1)^(3+2)*[A(1:2,1) A(1:2,3)];
a32=det(A32);
A33=(-1)^(3+3)*[A(1:2,1) A(1:2,2)];
a33=det(A33);

Cofactor_of_A=[a11 a12 a13; a21 a22 a23; a31 a32 a33]

display(' Determinant of A ')


D=A(1,1)*a11-A(1,2)*a12+A(1,3)*a13
D1=det(A)
D==D1
1 2 5
 
Question 4: Write a matlab code of a matrix  3 2 5  . Also write it’s all cofactor
1 0 2
 
matrices. Use cofactor of A find its determinant also verify it.

Answer:
clc
clear all
A=[1 2 5 ; 3 2 5; 1 0 2]
A11=(-1)^(1+1)*[A(2:3,2) A(2:3,3)];
a11=det(A11);
A12=(-1)^(1+2)*[A(2:3,1) A(2:3,3)];
a12=det(A12);
A13=(-1)^(1+3)*[A(2:3,1) A(2:3,2)];
a13=det(A13);

A21=(-1)^(2+1)*[A(1,2:3) ; A(3,2:3)];
a21=det(A21);
A22=(-1)^(2+2)*[A(1,1) A(1,3); A(3,1) A(3,3)];
a22=det(A22);
A23=(-1)^(2+3)*[A(1,1:2) ; A(3,1:2)];
a23=det(A23);

A31=(-1)^(3+1)*[A(1,2:3) ; A(2,2:3)];
a31=det(A31);
A32=(-1)^(3+2)*[A(1:2,1) A(1:2,3)];
a32=det(A32);
A33=(-1)^(3+3)*[A(1:2,1) A(1:2,2)];
a33=det(A33);

Cofactor_of_A=[a11 a12 a13; a21 a22 a23; a31 a32 a33]

display(' Determinant of A ')


D=A(1,1)*a11-A(1,2)*a12+A(1,3)*a13
D1=det(A)
D==D1

Question 5: Write a matlab code of 4  4 matrix whose elements are random. Also Find the
values of it’s cofactor matrices of the elements of first row. Use these cofactors of A find its
Adjoint of A and inverse of A

Answer:
clc
clear all

A=rand(4)

A11=(-1)^(1+1)*[A(2:4,2) A(2:4,3) A(2:4,4)];


a11=det(A11);
A12=(-1)^(1+2)*[A(2:4,1) A(2:4,3) A(2:4,4)];
a12=det(A12);
A13=(-1)^(1+3)*[A(2:4,1) A(2:4,2) A(2:4,4)];
a13=det(A13);
A14=(-1)^(1+4)*[A(2:4,2) A(2:4,3) A(2:4,4)];
a14=det(A14);

A21=(-1)^(2+1)*[A(1,2:4) ; A(3,2:4); A(4,2:4)];


a21=det(A21);
A22=(-1)^(2+2)*[A(1,1) A(1,3:4); A(3,1) A(3,3:4); A(4,1) A(4,3:4)];
a22=det(A22);
A23=(-1)^(2+3)*[A(1,1:2) A(1,4); A(3,1:2) A(3,4); A(4,1:2) A(4,4)];
a23=det(A23);
A24=(-1)^(2+4)*[A(1,1:3) ; A(3,1:3); A(4,1:3)];
a24=det(A24);

A31=(-1)^(3+1)*[A(1,2:4) ; A(2,2:4); A(4,2:4)];


a31=det(A31);
A32=(-1)^(3+2)*[A(1,1) A(1,3:4); A(2,1) A(2,3:4); A(4,1) A(4,3:4)];
a32=det(A32);
A33=(-1)^(3+3)*[A(1,1:2) A(1,4); A(2,1:2) A(2,4); A(4,1:2) A(4,4)];
a33=det(A33);
A34=(-1)^(3+4)*[A(1,1:3) ; A(2,1:3); A(4,1:3)];
a34=det(A34);

A41=(-1)^(4+1)*[A(2:4,2) A(2:4,3) A(2:4,4)];


a41=det(A41);
A42=(-1)^(4+2)*[A(2:4,1) A(2:4,3) A(2:4,4)];
a42=det(A42);
A43=(-1)^(4+3)*[A(2:4,1) A(2:4,2) A(2:4,4)];
a43=det(A43);
A44=(-1)^(4+4)*[A(1:3,1) A(1:3,2) A(1:3,3)];
a44=det(A44);
display(' Cofactor of A ')

Cof_A= [a11 a12 a13 a14; a21 a22 a23 a24 ; a31 a32 a33 a34 ; a41 a42 a43 a44]

Adj_A=Cof_A'

inv_A=Adj_A/det(A)

Question 6: Make a square matrix by adding two matrices whose one of them an upper
triangular and other are lower triangular matrices by using diagonal command

Answer:
clc
clear all

D1=diag([1 1 1 1]);
D2=diag([2 2 2], -1);
D3=diag([3 3], -2);
D4=diag([4], -3);
LowerTri=D1+D2+D3+D4

D5=diag([5 5 5], 1);


D6=diag([6 6], 2);
D7=diag([7], 3);
UperTri=D5+D6+D7

Square_A=UperTri+LowerTri

Question-7: Make a tri diagonal 9×9 matrix by using diagonal matlab command
Answer:
clc
clear all
n=3;
I_n=eye(n)

% a matrix with zeros on diagonal and 1 on the subdiagonal


I_n1=diag(ones(n-1,1),-1)

% a matrix with zeros on diagonal and 1 on the 'upper' diagonal


I_n2=diag(ones(n-1,1),1)
A1=I_n1-2*I_n+I_n2
A=kron(I_n , A1)

Question-8: Write a matlab program to find solution of a linear system of equation by using
row reduce echelon form.
Answer:
clc
clear all
A=[2 3 4; 5 2 4; 6 5 1]
A1=0.5*(A+A')
A2=0.5*(A-A')
A==A1+A2

Question 9: Write a matlab program to find solution of a linear system of equations


2 x1 + 3 x 2 − 6 x3 = 5
2 x1 − 2 x 2 − 4 x3 = 9
x1 + 2 x 2 + x3 = 8
whose input data are given randomly by keyboard. And verify it.
Answer:
clc
clear all

disp('Enter the coefficients of the first equation of x1, x2, x3 and constant
term')
a11=input('a11=');
a12=input('a12=');
a13=input('a13=');
b1=input('b1=');
%--------------------------------
disp('Enter the coefficients of the second equation of x1, x2, x3 and constant
term')
a21=input('a21=');
a22=input('a22=');
a23=input('a23=');
b2=input('b2=');
%--------------------------------
disp('Enter the coefficients of the third equation of x1, x2, x3 and constant
term')
a31=input('a31=');
a32=input('a32=');
a33=input('a33=');
b3=input('b3=');
%--------------------------------
A=[a11 a12 a13 ; a21 a22 a23 ; a31 a32 a33 ];
Det_A=det(A);
B=[b1 ; b2 ; b3];
%--------------------------------
if Det_A==0
disp('Matrix A is singular, it has no inverse')
disp('The system of equation is inconsistant, i.e. It has no solution')

else
A11=(-1)^(1+1)*det([a22 a23 ; a32 a33]);
A12=(-1)^(1+2)*det([a21 a23 ; a31 a33]);
A13=(-1)^(1+3)*det([a21 a22 ; a31 a32]);
A21=(-1)^(2+1)*det([a12 a13 ; a32 a33]);
A22=(-1)^(2+2)*det([a11 a13 ; a31 a33]);
A23=(-1)^(2+3)*det([a11 a12 ; a31 a32]);
A31=(-1)^(3+1)*det([a12 a13 ; a22 a23]);
A32=(-1)^(3+2)*det([a11 a13 ; a21 a23]);
A33=(-1)^(3+3)*det([a11 a12 ; a21 a22]);

Adj_A=[A11 A12 A13; A21 A22 A23 ; A31 A32 A33]';


Inverse_A=Adj_A/Det_A;
M=Inverse_A;
x=M*B;
disp('The solution of the given system of equations:')
fprintf('\n %f\n', x)
end

display(' Verify the results ')

2*x(1)+3*x(2)-6*x(3)
2*x(1)-2 *x(2)-4*x(3)
x(1)+2*x(2)+*x(3)

Question 10: Write a matlab program to find solution of a linear system of equations
AX = B
 2 3 4  x1   2
     
where, A=[2 3 4; 5 2 4; 6 5 1] A =  5 2 4  , X =  x 2  and B =  5 
6 5 1 x   4
   3  
whose input data are given by using array command. And also verify it.

Answer:
clc
clear all

A=[2 3 4; 5 2 4; 6 5 1]
%--------------------------------
B=[2; 5 ; 4];
Det_A=det(A);
%--------------------------------
if Det_A==0
disp('Matrix A is singular, it has no inverse')
disp('The system of equation is inconsistant, i.e. It has no solution')

else

A11= (-1)^(1+1)*det([A(2:3,2) A(2:3,3)]) ;


A12= (-1)^(1+2)*det([A(2:3,1) A(2:3,3)]) ;
A13= (-1)^(1+3)*det([A(2:3,1) A(2:3,2)]) ;
A21= (-1)^(2+1)*det([A(1,2:3); A(3,2:3)]) ;
A22= (-1)^(2+2)*det([A(1,1) A(1,3) ; A(3,1) A(3,3)]) ;
A23= (-1)^(2+3)*det([A(1,1:2); A(3,1:2)]) ;
A31= (-1)^(3+1)*det([A(1,2:3); A(2,2:3)]) ;
A32= (-1)^(3+2)*det([A(1:2,1) A(1:2,3)]) ;
A33= (-1)^(3+3)*det([A(1:2,1) A(1:2,2)]) ;
Adj_A=[A11 A12 A13; A21 A22 A23 ; A31 A32 A33]';

Inverse_A=Adj_A/Det_A;
M=Inverse_A;
x=M*B;
disp('The solution of the given system of equations:')

end
x

2*x(1)+ 3*x(2)+ 4*x(3)==2


5*x(1)+ 2*x(2)+ 4*x(3)==5
6*x(1)+ 5*x(2)+ 1*x(3)==4

Question 11: Write a matlab program to find solution of a linear system of equation
AX = B
 2 3 4  x1   2
     
where, A=[2 3 4; 5 2 4; 6 5 1] A =  5 2 4  , X =  x 2  and B =  5 
6 5 1 x   4
   3  
by using Cremers Rule.

Answer:
clc
clear all

A=[2 3 4; 5 2 4; 6 5 1]
%--------------------------------
B=[2; 5 ; 4]
Det_A=det(A);
%--------------------------------
if Det_A==0
disp('Matrix A is singular, it has no inverse')
disp('The system of equation is inconsistant, i.e. It has no solution')
else

A1= [B A(:,2) A(:,3)];


A2= [A(:,1) B A(:,3)];
A3= [A(:,1) A(:,2) B];

Det_A1=det(A1);
Det_A2=det(A2);
Det_A3=det(A3);

disp('The solution of the given system of equations:')

x1= Det_A1/Det_A
x2= Det_A2/Det_A
x3= Det_A3/Det_A

end

% Test
Equ1=2*x1+ 3*x2+ 4*x3

Question 12: Write a matlab program to find solution of a linear system of equation
AX = B
 2 3 4  x1   2
     
where, A=[2 3 4; 5 2 4; 6 5 1] A =  5 2 4  , X =  x 2  and B =  5 
6 5 1 x   4
   3  
by using row reduce echelon form.

Answer:

clc
clear all

A=[2 3 4; 5 2 4; 6 5 1]
%--------------------------------
B=[2; 5 ; 4]
Det_A=det(A);
%--------------------------------
if Det_A==0
disp('Matrix A is singular, it has no inverse')
disp('The system of equation is inconsistant, i.e. It has no solution')

else
A1= [A B]
RA1=rref(A1)

disp('The solution of the given system of equations:')


x=RA1(: , 4)

end

% Test
Equ1=2*x(1)+ 3*x(2)+ 4*x(3)

You might also like