0% found this document useful (0 votes)
32 views17 pages

Matlab Learning

The document describes performing basic matrix operations in MATLAB. It introduces commands to add, subtract, and multiply matrices. Examples are provided to demonstrate how MATLAB performs each operation when the matrices have the appropriate dimensions. The key steps for each operation are outlined.

Uploaded by

Ashok Kumar
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)
32 views17 pages

Matlab Learning

The document describes performing basic matrix operations in MATLAB. It introduces commands to add, subtract, and multiply matrices. Examples are provided to demonstrate how MATLAB performs each operation when the matrices have the appropriate dimensions. The key steps for each operation are outlined.

Uploaded by

Ashok Kumar
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
You are on page 1/ 17

Experiment 1

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

 For addition, both matrices must be of the same order.


 Create two matrices A and B which has equal number of rows and columns.Take number of rows as (m)
and number of columns as (n).
 Add the two matrices by using C=A+B in MATLAB. It adds corresponding elements of two matrices.
 A new matrix C is generated which has the same number of rows and columns as that of A and B, m and
n respectively.

𝑎11 𝑎12 𝑎13 ⋯ 𝑎1𝑛 𝑏11 𝑏12 𝑏13 … 𝑏1𝑛


𝑎21 𝑎22 𝑎23 ⋯ 𝑎2𝑛 𝑏21 𝑏22 𝑏23 … 𝑏2𝑛
A+ B = [ ⋮ ⋮ ⋮ ⋱ ⋮ ] + [ ⋮ ]
⋮ ⋮ ⋱ ⋮
𝑎𝑚1 𝑎𝑚2 𝑎𝑚3 … 𝑎𝑚𝑛 𝑏𝑚2 𝑏𝑚2 𝑏𝑚3 … 𝑏𝑚𝑛

𝑎11 + 𝑏11 𝑎12 + 𝑏12 𝑎13 + 𝑏13 … 𝑎1𝑛 + 𝑏1𝑛


𝑎21 + 𝑏21 𝑎22 + 𝑏22 𝑎23 + 𝑏23 … 𝑎2𝑛 + 𝑏2𝑛
= [ ]
⋮ ⋮ ⋮ ⋱ ⋮
𝑎𝑚1 + 𝑏𝑚1 𝑎𝑚2 + 𝑏𝑚2 𝑎𝑚3 + 𝑏𝑚3 … 𝑎𝑚𝑛 + 𝑏𝑚𝑛
Example

2 6 3 1
A+B = [ ]+[ ]
0 1 7 4

5 7]
C=[
7 5

Subtraction of matrices

 For subtraction, both matrices must be of the same order.


 Create two matrices A and B which has equal number of rows and columns. Take number of rows as
(m) and number of columns as (n).
 Subtract the two matrices by using C=A-B in MATLAB. It subtracts corresponding elements of two
matrices.
 A new matrix C is generated which has the same number of rows and columns as that of A and B, m and
n respectively.

𝑎11 𝑎12 𝑎13 ⋯ 𝑎1𝑛 𝑏11 𝑏12 𝑏13 … 𝑏1𝑛


𝑎21 𝑎22 𝑎23 ⋯ 𝑎2𝑛 𝑏21 𝑏22 𝑏23 … 𝑏2𝑛
A-B = [ ⋮ ⋮ ⋮ ⋱ ⋮ ]− [ ⋮ ]
⋮ ⋮ ⋱ ⋮
𝑎𝑚1 𝑎𝑚2 𝑎𝑚3 … 𝑎𝑚𝑛 𝑏𝑚2 𝑏𝑚2 𝑏𝑚3 … 𝑏𝑚𝑛

𝑎11 − 𝑏11 𝑎12 − 𝑏12 𝑎13 − 𝑏13 … 𝑎1𝑛 − 𝑏1𝑛


𝑎21 − 𝑏21 𝑎22 − 𝑏22 𝑎23 − 𝑏23 … 𝑎2𝑛 − 𝑏2𝑛
= [ ]
⋮ ⋮ ⋮ ⋱ ⋮
𝑎𝑚1 − 𝑏𝑚1 𝑎𝑚2 − 𝑏𝑚2 𝑎𝑚3 − 𝑏𝑚3 … 𝑎𝑚𝑛 − 𝑏𝑚𝑛

Example

4 6 5 9 7 5
A-B= [ ]− [ ]
2 9 4 1 6 3
−5 −1 0 ]
C= [
1 3 1

Multiplication of two matrices

 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.

𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑏11 𝑏12 … 𝑏1𝑝


𝑎 𝑎22 ⋯ 𝑎2𝑛 𝑏21 𝑏22 … 𝑏2𝑝
A * B = [ 21 ]∗
⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ⋯ ⋮
𝑎𝑚1 𝑎𝑚2 … 𝑎𝑚𝑛 [ 𝑏𝑛1 𝑏𝑛2 … 𝑏𝑛𝑝 ]
𝑎11 ∗ 𝑏11 + 𝑎12 ∗ 𝑏21 + 𝑎1𝑛 ∗ 𝑏𝑛1 𝑎21 ∗ 𝑏12 + 𝑎22 ∗ 𝑏22 + 𝑎2𝑛 ∗ 𝑏𝑛2 … 𝑎𝑚1 ∗ 𝑏1𝑝 + 𝑎𝑚2 ∗ 𝑏2𝑝 + 𝑎𝑚𝑛 ∗ 𝑏𝑛𝑝
𝑎 ∗ 𝑏11 + 𝑎22 ∗ 𝑏21 + 𝑎1𝑛 ∗ 𝑏𝑛1 𝑎21 ∗ 𝑏12 + 𝑎22 ∗ 𝑏22 + 𝑎2𝑛 ∗ 𝑏𝑛2 … 𝑎𝑚1 ∗ 𝑏1𝑝 + 𝑎𝑚2 ∗ 𝑏2𝑝 + 𝑎𝑚𝑛 ∗ 𝑏𝑛𝑝
= [ 21 ⋮ ⋮ ⋱ ⋮
]
𝑎𝑚1 ∗ 𝑏11 + 𝑎𝑚2 ∗ 𝑏21 + 𝑎1𝑛 ∗ 𝑏𝑛1 𝑎𝑚1 ∗ 𝑏12 + 𝑎𝑚2 ∗ 𝑏22 + 𝑎𝑚𝑛 ∗ 𝑏𝑛2 … 𝑎𝑚1 ∗ 𝑏1𝑝 + 𝑎𝑚2 ∗ 𝑏2𝑝 + 𝑎𝑚𝑛 ∗ 𝑏𝑛𝑝

Example
1 3 2 5 1 3
A*B= [ ] ∗ [ 0 2 4]
0 5 4 2 3 1

1∗5+3∗0+2∗2 1∗1+3∗2+2∗3 1∗3+3∗4+2∗1


=[ ]
0∗5+5∗0+4∗2 0∗1+5∗2+4∗3 0∗1+5∗2+4∗3
57 74 43 ]
C= [
39 76 41
Inverse of a matrix

Conditions for inverse of a matrix


 A square matrix (equal number of rows and columns)
 A non singular matrix( Determinant of matrix is non zero)
Inverse
 Create a matrix A which has equal number of rows and columns, m and n respectively.
 Check if its determinant is non zero.
 Find adjoint of a matrix by transposing cofactor matrix.
 Divide adjoint of matrix with determinant of A.
 To confirm the result check if A* A-1 = I

𝑎11 𝑎12 ⋯ 𝑎1𝑚


𝑎 𝑎22 ⋯ 𝑎2𝑚
A = [ 21 ]
⋮ ⋮ ⋱ ⋮
𝑎𝑚1 𝑎𝑚2 … 𝑎𝑚𝑚

adj(A) [Cofactor of (A)]T


A-1 = =
|A| |A|

Cofactor of aij = (-1)i+j |Minor by eliminating ith row and jth column|

𝐴11 𝐴12 ⋯ 𝐴1𝑚


𝐴 𝐴22 ⋯ 𝐴2𝑚
Suppose the cofactor of A =[ 21 ]
⋮ ⋮ ⋱ ⋮
𝐴𝑚1 𝐴𝑚2 ⋯ 𝐴𝑚𝑚

𝐴11 𝐴21 ⋯ 𝐴𝑚1


𝐴 𝐴22 ⋯ 𝐴𝑚2
Then the adjoint after transpose would be, adj(A) = [ 12 ]
⋮ ⋮ ⋱ ⋮
𝐴1𝑚 𝐴2𝑚 ⋯ 𝐴𝑚𝑚
Determinate of A = |A| = 𝑎11 𝐴11 + 𝑎21 𝐴21 + … … … … … + 𝑎𝑚1 𝐴𝑚1

= 𝑎11 𝐴11 + 𝑎12 𝐴12 + … … … … … + 𝑎1𝑚 𝐴1𝑚

𝐴11 𝐴21 ⋯ 𝐴𝑚1


1 𝐴 𝐴22 ⋯ 𝐴𝑚2
So the inverse of (A) = [ 12 ]
𝑎11 𝐴11 + 𝑎21 𝐴21 + ……………+ 𝑎𝑚1 𝐴𝑚1 ⋮ ⋮ ⋱ ⋮
𝐴1𝑚 𝐴2𝑚 ⋯ 𝐴𝑚𝑚

For example:

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

24 −0.5 −8.5 −16.5


−27.3 0.5 9.83 18.83 ]
A-1 = [
−20 0.5 7.5 13.5
26.33 −0.5 −9.83 −17.83
6/18/21 4:43 PM E:\M.TECH\SEM 2\FEM Lab\P...\Experiment1.m 1 of 2

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

Multiply, Add and Subtract two matrices


__________________________________________

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

Both matrices A and B are of same order of 2 x3


Matrix A
Enter the element a11 1
Enter the element a21 2
Enter the element a12 3
Enter the element a22 4
Enter the element a13 5
Enter the element a23 6

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 1 to Multiply,Enter 2 to Add,


Enter 3 to Subtract,Enter 4 to terminate : 1

1. Multiplication of two matrices A and B

Multiplication of matrices A and B is not possible

Enter again
2

2. Addition of two matrices A and B

Matrix C of order 2 x 3
8 12 6
10 4 8

Enter again 3

3. Subtraction of two matrices A and B

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

fprintf('Enter the element a%i%i ',i,j);


A(i,j)=input(' ');
while j<m
B(i,j)=A(i,j);

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

Inverse of a 3x3 order matrix


__________________________________________

Conditions to find inverse of a matrix :


1. It must be a square matrix.
2. Determinant must be non zero.

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

The determinant of matrix A is 1.000000e+00


Both conditions are met.Inverse of matrix exists.

Finding Inverse of A
----------------------

1. Repeat first two columns after the last column


1 2 -2 1 2
-1 3 0 -1 3
0 -2 1 0 -2

2. Repeat first two rows after the last row


1 2 -2 1 2
-1 3 0 -1 3
0 -2 1 0 -2
1 2 -2 1 2
-1 3 0 -1 3

3. Delete first row and first column of the matrix


3 0 -1 3
-2 1 0 -2
2 -2 1 2
3 0 -1 3

4. Calculate terms by cross multiplying


3 1 2
MATLAB Command Window Page 2

2 1 2
6 2 5

5. Take transpose of Matrix


3 2 6
1 1 2
2 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

fprintf('Enter the element a%i%i ',i,j);


A(i,j)=input(' ');
end
end
disp(' ')
disp(A);
p=det(A);
fprintf('\n2.Determinant of matrix A is %i\n',p)
C=A;
B=C;
fprintf('\n3.Finding Adjoint of matrix\n');
for i=1:n
for j=1:m

fprintf('\nMinor of a%i%i is \n',i,j);


B(i,:)=[];
B(:,j)=[];
disp(B);
fprintf('Cofactor of a%i%i is',i,j);
c=((-1)^(i+j))*det(B);
disp(c)
D(i,j)=c;
B=C;
end
end
fprintf('\nCofactor Matrix\n');
disp(D);
fprintf('\nAdjoint A\n');
D=D';
disp(D);
fprintf('\n4.Inverse of matrix D is\n ');
6/18/21 5:01 PM E:\M.TECH\SEM 2\F...\experiment1inverse2.m 2 of 2

E= p\D;
disp(E);
MATLAB Command Window Page 1

Inverse of a matrix
__________________________________________

Conditions to find inverse of a matrix :


1. It must be a square matrix.
2. Determinant must be non zero.

Matrix A :
---------------
Enter order of a matrix - 3

1.Elements of Matrix A

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

1 2 -2
-1 3 0
0 -2 1

2.Determinant of matrix A is 1.000000e+00

3.Finding Adjoint of matrix

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

>>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy