Matlab Learning
Matlab Learning
Objective
To introduce the basic concepts of programming for FEM problems.
To develop the computer program for the addition, subtraction, multiplication and inverse of matrices.
Software used
MATLAB 2018
Basic commands
Command Description
clc clear text from command window
clear Removes data stored to variable or remove variable from workspace
close all Close all matlab figure windows
format Format changes the output format to default which is ‘short’
Format short g – best of fixed or floating points,5 digits
Format long g – best of fixed or floating points, 15 digits for double,7 digits for single
disp( ) Displays the string
fprintf( ) Prints an array of characters
‘% ‘notation in fprintf
%d – prints a whole number
%s – prints a string character
a%i – prints a character and a number together
sprintf( ) Returns data in a string variable rather than writing it to file.
input( ) Takes the input from user
\n Inserts a newline character
Addition of Matrices
2 6 3 1
A+B = [ ]+[ ]
0 1 7 4
5 7]
C=[
7 5
Subtraction of matrices
Example
4 6 5 9 7 5
A-B= [ ]− [ ]
2 9 4 1 6 3
−5 −1 0 ]
C= [
1 3 1
For multiplication, number of columns of first matrix must be equal to number of rows of second matrix
Create two matrices A and B such that column of matrix A is equal to rows of matrix B. For Matrix A
take number of rows as (m) and number of columns as (n).For Matrix B take number of rows as (n) and
number of columns as (q).
Multiply the two matrices by using C=A*B in MATLAB.
A new matrix C is generated which has (m) number of rows and (n) number of columns.
Example
1 3 2 5 1 3
A*B= [ ] ∗ [ 0 2 4]
0 5 4 2 3 1
Cofactor of aij = (-1)i+j |Minor by eliminating ith row and jth column|
For example:
7 8 4 5
1 2 9 8]
A=[
6 5 3 2
7 9 4 6
clc;
clear;
format short g;
disp('Multiply, Add and Subtract two matrices');
fprintf('__________________________________________\n');
fprintf('\nFor Matrix A :\n');
m=input('Enter number of rows - ');
n=input('Enter number of columns - ');
A=zeros(m,n);
a=size(A);
fprintf('\nFor Matrix B :\n');
p=input('Enter number of rows - ');
q=input('Enter number of columns - ');
B=zeros(p,q);
b=size(B);
if a==b
fprintf('\nBoth matrices A and B are of same order of %i x%i\n',m,n);
fprintf('Matrix A\n');
for n=1:n
for m=1:m
x=10.*m+n;
fprintf('Enter the element a%i ',x);
A(m,n)=input(' ');
end
end
fprintf('\nMatrix B\n')
for q=1:q
for p=1:p
x=10.*p+q;
fprintf('Enter the element b%i ',x);
B(p,q)=input(' ');
end
end
disp(' ')
prog=input('Enter 1 to Multiply,Enter 2 to Add,\nEnter 3 to Subtract,Enter 4 to terminate
: ' );
if prog==1
fprintf('\n1. Multiplication of two matrices A and B\n');
if n==p
C=zeros(m,q);
C=A*B;
fprintf('\nMatrix C of order %i x %i\n',m,q);
disp(C);
prog=input('Enter again ');
else
fprintf('\nMultiplication of matrices A and B is not possible\n');
prog=input('\nEnter again - ');
end
end
6/18/21 4:43 PM E:\M.TECH\SEM 2\FEM Lab\P...\Experiment1.m 2 of 2
if prog==2
fprintf('\n2. Addition of two matrices A and B\n');
if a==b
C=zeros(m,n);
C=A+B;
fprintf('\nMatrix C of order %i x %i\n',m,n);
disp(C);
prog=input('Enter again ');
else
fprintf('\nAddition not possible\n');
prog=input('Enter again ');
end
end
if prog==3
fprintf('\n3. Subtraction of two matrices A and B\n');
if a==b
C=zeros(m,n);
C=A-B;
fprintf('\nMatrix C of order %i x %i\n',m,n);
disp(C);
prog=input('Enter again ');
else
fprintf('\nSubtraction not possible\n');
prog=input('Enter again ');
end
end
if prog==4
fprintf('PROGRAM TERMINATED');
end
else
fprintf('\nMatrix A and B are of different order.\n');
fprintf('Multiply,Addition and Subtraction operations are not possible');
end
MATLAB Command Window Page 1
For Matrix A :
Enter number of rows - 2
Enter number of columns - 3
For Matrix B :
Enter number of rows - 2
Enter number of columns - 3
Matrix B
Enter the element b11 7
Enter the element b21 8
Enter the element b12 9
Enter the element b22 0
Enter the element b13 1
Enter the element b23 2
Enter again
2
Matrix C of order 2 x 3
8 12 6
10 4 8
Enter again 3
Matrix C of order 2 x 3
-6 -6 4
-6 4 4
MATLAB Command Window Page 2
Enter again 4
PROGRAM TERMINATED>>
6/18/21 4:54 PM E:\M.TECH\SEM 2\F...\Experiment1_Inverse.m 1 of 2
clc;
clear;
format short g;
disp('Inverse of a 3x3 order matrix');
fprintf('__________________________________________\n');
fprintf('\nConditions to find inverse of a matrix :\n')
disp('1. It must be a square matrix.');
disp('2. Determinant must be non zero.');
fprintf('\nMatrix A :\n');
disp('---------------');
m=input('Enter order of a matrix - ');
n=m;
A=zeros(m,n);
B=zeros(m,n-1);
F=zeros(m,n);
D=zeros(m-1,2*m-1);
a=size(A);
for i=1:m
for j=1:m
break
end
end
end
fprintf('\nMatrix A\n');
disp(A);
fprintf('The determinant of matrix A is %i',det(A));
if det(A)~=0
fprintf('\nBoth conditions are met.Inverse of matrix exists.\n');
else
fprintf('Condition 2 is not met. Hence Inverse does not exist')
end
disp(' ');
disp('Finding Inverse of A');
disp('----------------------')
disp(' ');
disp('1. Repeat first two columns after the last column ');
C=[A B];
disp(C);
for i=1:m
for j=1:2*m-1
while i<m && j<=2*m-1
D(i,j)=C(i,j);
break
6/18/21 4:54 PM E:\M.TECH\SEM 2\F...\Experiment1_Inverse.m 2 of 2
end
end
end
disp('2. Repeat first two rows after the last row ');
E=vertcat(C,D);
disp(E);
disp('3. Delete first row and first column of the matrix ');
E(1,:)=[];
E(:,1)=[];
disp(E);
disp('4. Calculate terms by cross multiplying ');
for i=1:m
for j= 1:m
F(i,j)=(E(i,j)*E(i+1,j+1)-E(i+1,j)*E(i,j+1));
end
end
disp(F);
disp('5. Take transpose of Matrix ');
G=F';
disp(G);
disp('6. Inverse of a matrix is ');
G = G/det(A);
disp(G);
MATLAB Command Window Page 1
Matrix A :
---------------
Enter order of a matrix - 3
Enter the element a11 1
Enter the element a12 2
Enter the element a13 -2
Enter the element a21 -1
Enter the element a22 3
Enter the element a23 0
Enter the element a31 0
Enter the element a32 -2
Enter the element a33 1
Matrix A
1 2 -2
-1 3 0
0 -2 1
Finding Inverse of A
----------------------
2 1 2
6 2 5
6. Inverse of a matrix is
3 2 6
1 1 2
2 2 5
>>
6/18/21 5:01 PM E:\M.TECH\SEM 2\F...\experiment1inverse2.m 1 of 2
clc;
clear;
format short g;
disp('Inverse of a matrix');
fprintf('__________________________________________\n');
fprintf('\nConditions to find inverse of a matrix :\n')
disp('1. It must be a square matrix.');
disp('2. Determinant must be non zero.');
fprintf('\nMatrix A :\n');
disp('---------------');
m=input('Enter order of a matrix - ');
n=m;
A=zeros(m,n);
C=zeros(m,n);
B=zeros(m,n);
fprintf('\n1.Elements of Matrix A \n');
disp(' ')
for i=1:m
for j=1:m
E= p\D;
disp(E);
MATLAB Command Window Page 1
Inverse of a matrix
__________________________________________
Matrix A :
---------------
Enter order of a matrix - 3
1.Elements of Matrix A
1 2 -2
-1 3 0
0 -2 1
Minor of a11 is
3 0
-2 1
Cofactor of a11 is 3
Minor of a12 is
-1 0
0 1
Cofactor of a12 is 1
Minor of a13 is
-1 3
0 -2
Cofactor of a13 is 2
MATLAB Command Window Page 2
Minor of a21 is
2 -2
-2 1
Cofactor of a21 is 2
Minor of a22 is
1 -2
0 1
Cofactor of a22 is 1
Minor of a23 is
1 2
0 -2
Cofactor of a23 is 2
Minor of a31 is
2 -2
3 0
Cofactor of a31 is 6
Minor of a32 is
1 -2
-1 0
Cofactor of a32 is 2
Minor of a33 is
1 2
-1 3
Cofactor of a33 is 5
Cofactor Matrix
3 1 2
2 1 2
6 2 5
Adjoint A
MATLAB Command Window Page 3
3 2 6
1 1 2
2 2 5
4.Inverse of matrix D is
3 2 6
1 1 2
2 2 5
>>