Professional Documents
Culture Documents
For example
X+Y = [2 6 8] + [2 4 1]
3 5 4 3 4 1
Z = [4 10 9]
6 9 5
Subtraction of two matrices:
Analysis:
For the subtraction of two matrices the very first step is to check
that both the matrices must have the same number of rows and
columns.
Suppose, we have a matrix X of size m*n than for subtraction
the other matrix Y must be ofsame size.
The subtraction of X and Y is denoted by X - Y, and the program
is performed by subtractingcorresponding elements of X and Y.
After subtraction of the two matrices we get the matrix Z, which is also of
the size m*n.
For example
X - Y = [2 6 8] - [2 8 1]
3 5 4 3 4 1
Z = [0 −2 7]
0 1 3
MATLAB program for the addition and subtraction of two matrices
% ADDITION AND SUBTRACTION OF TWO MATRICES
clc
disp('PROGRAM FOR ADDITION OF TWO MATRIX OF ORDER m,n');
p=input('Enter the number of rows (p) = ');
q=input('Enter the number of columns (q) = ');
A=zeros(p,q);
B=zeros(p,q);
fprintf('\nELEMENTS OF FIRST MATRIX (A) OF ORDER %i * %i
IS\n',p,q)
for p=1:p;
fprintf('Enter the all elements of raw of %i =',p);
% for display alphabet and string in one step
A(p,:)=input('');
end
fprintf('\nELEMENTS OF SECOND MATRIX (B) OF ORDER %i * %i
IS\n',p,q)
for p=1:p;
fprintf('Enter the all elements of raw of %i =',p);
B(p,:)=input('');
end
fprintf('\nFIRST MATRIX (A) OF ORDER %i * %i IS = \n',p,q)
disp(A)
fprintf('\nSECOND MATRIX (B) OF ORDER %i * %i IS = \n',p,q)
disp(B)
fprintf('\nADDITION OF MATRICES A & B OF ORDER %i * %i IS =
\n',p,q)
disp(A+B)
fprintf('\nSUBTRACTION OF MATRIX A & B OF ORDER %i * %i IS =
\n',p,q)
disp(A-B);
Multiplication of two matrices:
Analysis:
For the multiplication of two matrices the very first step is to check
that the number of columnsof the first matrix is equal to the
number of rows of second matrix.
Suppose, we have a matrix X of size m*n than for multiplication
the other matrix Y must have n number of rows and the size might
be n*p.
The multiplication of X and Y is denoted by X * Y.
After multiplication of the two matrices we get the matrix Z, which would be
of size m*p.
𝑎11 ∗ 𝑏11 + 𝑎12 ∗ 𝑏21 + 𝑎1𝑛 ∗ 𝑏𝑛1 𝑎11 ∗ 𝑏12 + 𝑎22 ∗ 𝑏22 + 𝑎2𝑛 ∗ 𝑏𝑛2 ⋯ 𝑎𝑚1 ∗ 𝑏1𝑝 + 𝑎𝑚2 ∗ 𝑏2𝑝 + 𝑎𝑚𝑛 ∗ 𝑏𝑛𝑝
𝑎21 ∗ 𝑏11 + 𝑎22 ∗ 𝑏21 + 𝑎1𝑛 ∗ 𝑏𝑛1 𝑎21 ∗ 𝑏12 + 𝑎22 ∗ 𝑏22 + 𝑎2𝑛 ∗ 𝑏𝑛2 ⋯ 𝑎𝑚1 ∗ 𝑏1𝑝 + 𝑎𝑚2 ∗ 𝑏2𝑝 + 𝑎𝑚𝑛 ∗ 𝑏𝑛𝑝
⋮ ⋮ ⋯ ⋱
[𝑎𝑚1 ∗ 𝑏11 + 𝑎𝑚2 ∗ 𝑏21 + 𝑎1𝑛 ∗ 𝑏𝑚1 𝑎𝑚1 ∗ 𝑏12 + 𝑎𝑚2 ∗ 𝑏22 + 𝑎𝑚𝑛 ∗ 𝑏𝑛2 ⋯ 𝑎𝑚1 ∗ 𝑏1𝑝 + 𝑎𝑚2 ∗ 𝑏2𝑝 + 𝑎𝑚𝑛 ∗ 𝑏𝑛𝑝 ]
For example
5 2 2 1 3 5
X * Y = [4 4 5] * [4 2 1]
2 3 3 3 4 2
19 27 31
Z = [35 40 34]
23 24 19
MATLAB program for the multiplication of two matrices
% MULTIPLICTION OF TWO MATRICES
clc
disp('Program for multiplication of two matrix');
fprintf('\n First matrix (A) of order (m,n) \n' );
m=input('enter the no. of rows (m) = ');
n=input('enter the no. of column (n) = ');
fprintf('\n\n Second matrix (B) of order (n,p) \n' );
fprintf('the no.of rows of matrix is(n) = %i \n' ,n);
p=input('enter the no. of column (p) = ');
A=zeros(m,n);
B=zeros(n,p);
fprintf('\nELEMENTS OF FIRST MATRIX (A) OF ORDER %i * %i
IS\n',m,n)
for m=1:m ;
fprintf('Enter the all elements of raw of %i =',m);
% for display alphabet and string in one step
A(m,:)=input('');
end
Cofactor of aij = (-1)i+j |Minor by eliminating ith row and jth column|
For example:
17 8 14 15
11 2 9 8]
A =[
6 15 3 2
7 9 14 6
Figure 2.1
Step 1: Discretization
The domain is subdivided into three elements and four nodes as shown in the
figure.
40 −40 20 −20
𝑘1 = [ ] 𝑘2 = [ ]
−40 40 −20 20
60 −60
𝑘3 = [ ]
−60 60
40 −40 0 0
−40 60 −20 0
K=[ ]
0 −20 80 −60
0 0 −60 60
Step 4- Applying the Boundary conditions
The boundary conditions for this problem are given as:
𝑈1 = 0, 𝑃2 = −100𝑁, 𝑃3 = 50𝑁
40 −40 0 0 0 𝑃1
−40 60 −20 0 𝑈 −100
[ ] ∗ [ 2] = [ ]
0 −20 80 −60 𝑈3 50
0 0 −60 60 𝑈4 𝑃2
oneD_Assemble (K,k,i,j)
function y = oneD_Assemble(K,k,i,j )
% Assemble- this function assemble the element stiffness matrix k
% of element with nodes i and j into the globel stiffness
% matrix K
% This funcation call K after matrix k is assembled
K(i,i)=K(i,i) +k(1,1);
K(i,j)=K(i,j) +k(1,2);
K(j,i)=K(j,i) +k(2,1);
K(j,j)=K(j,j) +k(2,2);
y=K;
end
disp('===========================================================
============================
===============')
n=input('Total no of nodes (n) = ');
m=input('Total no of elements (m) = ');
K=zeros(n); U=zeros(n,1);F=zeros(n,1);
Ke=zeros(1,m); i=zeros(1,m); j=zeros(1,m);
for m=1:m
fprintf('\nFOR ELEMENT-%i (Enter)=> i,j,Ke%i = ',m,m);
IJKeL=input('','s');
IJKeL=str2num(IJKeL);
i(m)=IJKeL(1);
j(m)=IJKeL(2);
Ke(m)=IJKeL(3);
end
% Boundary condition
% Displacement
fprintf('\nEnter Boundary condition');
fprintf('\n----------------------------');
disp('Note: In (+ve)X-direction enter positive value of known
variable')
ask=input('\nIs there any displacement value known ?? Y/N
=','s');
if ask=='Y'||ask=='y'
dof1=input('\nEnter the node numbers at which displacement value
are known\n(for more
then one node separate node by comma) =','s');
dof1=str2num(dof1);
for a=dof1
fprintf('\nEnter the displacement at node-%i (U%i)= ',a,a)
U(a)=input('');
end
elseif ask=='N'||ask=='n'
disp('So we have all displacement values are unknown')
dof1=0;
else
exit
end
% Force
dof2=input('\nEnter the node numbers at which external force
value are known =','s');
dof2=str2num(dof2);
for dof2=dof2
fprintf('\nEnter the force at node-%i (F%i)= ',dof2,dof2)
F(dof2)=input('');
end
%%
%%
Solution Procedure
%%
% Solution on the basis of given Input
disp
('===============================================================
========================
===============')
disp('SOLUTION OF THE PROBLEM STEP WISE STEP:')
disp
('===============================================================
========================
===============')
%% *STEP-1: DISCRITIZATION*
fprintf('STEP-1: DISCRITIZATION \n')
disp('-----------------------')
fprintf('\nTotal no of node = %i',n);
fprintf('\nTotal no of element = %i\n',m );
elements=1:m;
nodes=[i;j];
T1=table(elements',nodes',Ke','VariableNames',{'Element','nodes',
'Stiffness'});
disp(' ');
disp(T1)
%% *STEP-2: ELEMENTS MATRIX*
fprintf('\nSTEP-2: ELEMENTS MATRIX (i.s each element stiffness
matrix ) \n')
disp('-----------------------------------------------------------
----')
for m=1:m
fprintf('\nFOR ELEMENT%i, Element Matrix (K%i)=\n',m,m);
Kb=oneD_ElementStiffness(Ke(m));
disp(Kb);
K=oneD_Assemble(K,Kb,i(m),j(m));
end
%% *STEP-3: ASSEMBLY OF ELEMENTS MATRIX*
fprintf('\nSTEP-3: ASSEMBLY OF ELEMENTS MATRICES (K)(i.s assemble
of all element
stiffness matrix )\n')
disp('-----------------------------------------------------------
---------------------------- ---')
disp('K = ')
disp(K);
%% *STEP-4: BOUNDARY CONDITIONS*
fprintf('\nSTEP-4: GIVEN BOUNDARY CONDITIONS ARE:\n ')
disp('----------------------------------------')
if dof1~=0
for a=dof1
fprintf('\nDisplacement at node-%i (U%i)= %g ',a,a,U(a))
end
end
for dof2=dof2
fprintf('\nExternal Force at node-%i (F%i)= %g
',dof2,dof2,F(dof2))
end
%% *STEP-5: SOLUTION ( FIND OUT U=? FROM K*U=F )*
fprintf('\n\nSTEP-5: SOLUTION (i.s determine the unknown
displacement and reaction force
at nodes\n ');
disp
('---------------------------------------------------------------
------------------------
--')
S=K;
if dof1~=0
for a=dof1
F=F-K(:,a)*U(a);
K(:,a)=0;
K(a,:)=0;
K(a,a)=1;
F(a)=U(a);
end
end
U=K\F;
F=S*U;
% Print Result
dof=1:n; % degree of freedom of all node i.s total degree of
freedom
% dof1=degree of restriction due to given displacement (that
nodes where displacement are
given)
% dof2= that node where force value are given
% dof3= that nodes where displacement value are not given
% dof4= that nodes where force value are not given
dof3=dof;
dof3(dof1)=0;
dof3=nonzeros(dof3);
for dof3=dof3'
fprintf('\nDisplacement at node-%i (U%i)= %g ',dof3,dof3,U(dof3))
end
if dof1~=0
for a=dof1
fprintf('\nReaction Force at node-%i (R%i)= %g ',a,a,F(a))
end
end
%% *STEP-6: POST PROCESING*
fprintf('\n\nSTEP-6: POST PROCESING (i.s determine element wise
force in spring)\n')
disp
('---------------------------------------------------------------
----------------')
element_load=zeros(1,m);
for m=1:m
fprintf('\nFOR ELEMENT-%i and node-(%i,%i)',m,i(m),j(m));
j1=i(m);j2=j(m);
element_load(m)=Ke(m)*(U(j2)-U(j1));
if element_load(m)>0
X=' (tensile force)';
elseif element_load(m)<0
X=' (compressive force)';
else
X=' (no force)';
end
fprintf('\n (1) displacement: U%i=%g and
U%i=%g',i(m),U(i(m)),j(m),U(j(m)))
fprintf('\n (2) element load (f%i)={K%i*(U%i-U%i)}= %g
',m,m,j(m),i(m),element_load
(m)),disp(X);
end
%% * CONCLUSION :--*
fprintf('\n\nSTEP-7: CONCLUSION :-- \n');
disp('--------------------------')
T2=table([1:n]',U,F,'VariableNames',{'Node','Displacement','Force
'});
T3=table(elements', nodes', element_load' ,'VariableNames',
{'Element','nodes','element_load'});
disp('Nodes wise Result:')
disp('------------------')
disp(T2)
disp('Elements wise Result:')
disp('---------------------')
disp(T3)
fprintf('\n NOTE: (+ve) element load means tensile in nature and
(-ve) means compressive
in nature\n')
%% * GRAPH OF RESULT *
% example-1 f=1000 then take 1 kN by dividing 1000,stress=5*10^6
then take 5 MPa by
dividing 10^6,u=10^-3 m then take 1mm
% copy below lines and paste in command window then edit the unit
and remove % mark
%stem(elements,load/1000,'k'),title('element no. vs
load'),xlabel('elements number'),
ylabel('Force(kN)')