Matrix
Matrix
Matrix
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
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
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);
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);
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)
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
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)
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
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]);
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
Inverse_A=Adj_A/Det_A;
M=Inverse_A;
x=M*B;
disp('The solution of the given system of equations:')
end
x
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
Det_A1=det(A1);
Det_A2=det(A2);
Det_A3=det(A3);
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)
end
% Test
Equ1=2*x(1)+ 3*x(2)+ 4*x(3)