You are on page 1of 21

EXPERIMENT No.

Aim: Introduction to basic concepts of programming for FEM


problems. To develop the computer program for the addition,
subtraction, multiplication and inverse of matrices.

Addition of two matrices:


Analysis:
 For the addition of two matrices the very first step is to check that
both the matrices must havethe same number of rows and
columns.
 Suppose, we have a matrix X of size m*n than for addition the
other matrix Y must be ofsame size.
 The sum of X and Y is denoted by X+Y, and the program is
performed by addingcorresponding elements of X and Y.
 After addition of the two matrices we get the matrix Z, which is also of the
size m*n.

𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑏11 𝑏12 ⋯ 𝑏1𝑛


𝑎21 𝑎22 ⋯ 𝑎2𝑛 𝑏21 𝑏22 ⋯ 𝑏2𝑛
X+Y=[ ⋮ ⋮ ⋱ ⋮ ] + [ ⋮ ⋮ ⋱ ⋮
]
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛 𝑏𝑚1 𝑏𝑚2 ⋯ 𝑏𝑚𝑛

𝑎11 + 𝑏11 𝑎12 + 𝑏12 ⋯ 𝑎1𝑛 + 𝑏1𝑛


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

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.

𝑎11 𝑎12 ⋯ 𝑎1𝑛 𝑏11 𝑏12 ⋯ 𝑏1𝑛


𝑎 𝑎22 ⋯ 𝑎2𝑛 𝑏21 𝑏22 ⋯ 𝑏2𝑛
X - Y = [ 21
⋮ ⋮ ⋱ ⋮ ] - [ ⋮ ]
⋮ ⋱ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛 𝑏𝑚1 𝑏𝑚2 ⋯ 𝑏𝑚𝑛

𝑎11 − 𝑏11 𝑎12 − 𝑏12 ⋯ 𝑎1𝑛 − 𝑏1𝑛


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

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 𝑎12 ⋯ 𝑎1𝑛 𝑏11 𝑏12 ⋯ 𝑏1𝑝


𝑎 𝑎22 ⋯ 𝑎2𝑛 𝑏21 𝑏22 ⋯ 𝑏2𝑝
X * Y = [ 21
⋮ ⋮ ⋱ ⋮ ] * ⋮ ⋮ ⋱ ⋮
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛 [𝑏𝑛1 𝑏𝑛2 ⋯ 𝑏𝑛𝑝 ]

𝑎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

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


Z = [4 ∗ 1 + 4 ∗ 4 + 5 ∗ 3 4 ∗ 3 + 4 ∗ 2 + 5 ∗ 4 4 ∗ 5 + 4 ∗ 1 + 5 ∗ 2]
2∗1+3∗4+3∗3 2∗3+3∗2+3∗4 2∗5+3∗1+3∗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

fprintf('\nELEMENTS OF SECOND MATRIX (B) OF ORDER %i * %i


IS\n',n,p)
for n=1:n ;
fprintf('Enter the all elements of raw of %i =',n);
% for display alphabet and string in one step
B(n,:)=input('');
end
fprintf('\n FIRST MATRIX (A) OF ORDER %i * %i is = \n',m,n );
disp(A);
fprintf('\n SECOND MATRIX (B) OF ORDER %i * %i is =\n',n,p );
disp(B);
fprintf('\n MULTIPLICATION OF MATRIX A AND B OF ORDER %i * %i is
= \n',m,p );
disp(A*B);
Inverse of a matrix:
Analysis:
 For the inverse of a matrix the very first step is to check that the
number of rows are equal to the number of columns or the matrix
we have should be a square matrix.
 Suppose we need to find the inverse of a matrix A, then A
must be square. (Otherwise, the multiplication wouldn't work.)
If the matrix isn't square, it cannot have a (properly two-
sided) inverse.
 The determinant of the matrix should not be equal to zero.
 The inverse of A is A-1 only when A × A-1 = A-1 × A = I
 After inversion the matrix obtained would also be a square matrix.]T

𝑎11 𝑎12 ⋯ 𝑎1𝑛


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

𝑎𝑑𝑗(𝐴) [𝐶𝑜𝑓𝑎𝑐𝑡𝑜𝑟 𝑜𝑓 (𝐴)]𝑇


𝑨−𝟏 = =
⃓ 𝐴⃓ ⃓ 𝐴⃓

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) = a11A11 + a21A21 + … … … … … + am1Am1 [ ⋮12 ]
⋮ ⋱ ⋮
𝐴1𝑚 𝐴2𝑚 ⋯ 𝐴𝑚𝑚

For example:

17 8 14 15
11 2 9 8]
A =[
6 15 3 2
7 9 14 6

−0.169 0.352 0.089 −0.076


0.041 −0.099 0.050 0.011
A −1 =[ ]
−0.072 0.060 −0.037 0.113
0.304 −0.403 −0.093 −0.024

MATLAB program for the Inverse of a matrix


%Program for the Inverse of matrix A
clc
disp('program for inverse of matrix of size m ');
m=input('size of matrix A (m) = ');
n=m;
A=zeros(m,n);
disp(' '),
disp('MATRIX ELEMENTS')
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
fprintf('\n\n MATRIX A is = \n');
disp(A);
fprintf('\n\n DETERMINANT OF MATRIX A IS = ');
disp(det(A));
if (det(A))==0
fprintf('\n\n determinant is not possible because matrix A is
singular ');
else
fprintf('\n\n INVERSE OF MATRIX A IS = \n');
disp(inv(A))
end
EXPERIMENT No. 2

Aim: Finite Element Formulation and analysis of one dimensional problem


by developingcomputer program.
Software used: MATLAB 2014
One dimensional problem: A system of three springs in series subjected to two
axial loads. The stiffnesses of the springs and the axial loads applied are given by
k1 = 40 N/mm, k2 = 20 N/mm, k3 = 60 N/mm, P2 = –100 N, and P3 = 50 N.
Determine the displacements of the nodes of the system.

Figure 2.1

Step 1: Discretization
The domain is subdivided into three elements and four nodes as shown in the
figure.

Step 2- writing the stiffness matrices


The two element stiffness matrices k1, k2 and k3 are obtained by making calls to
the MATLAB function linear triangle element stiffness. Each matrix has size
2×2..

40 −40 20 −20
𝑘1 = [ ] 𝑘2 = [ ]
−40 40 −20 20

60 −60
𝑘3 = [ ]
−60 60

Step 3- Assembling the global stiffness Matrix


Since the structure has four nodes, the size of the global matrix is 4×4. So by
assembling k1, k2and k3 we get K.

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𝑁

So using these boundary conditions:

40 −40 0 0 0 𝑃1
−40 60 −20 0 𝑈 −100
[ ] ∗ [ 2] = [ ]
0 −20 80 −60 𝑈3 50
0 0 −60 60 𝑈4 𝑃2

Step 5- Solving the equation


Solution of the system equation is performed by Gaussian elimination (with
MATLAB).
Step 6- Post processing
In this step, we obtain stresses and strains in both element using MATLAB.

MATLAB functions used

oneD_ElementStiffness(k) where k is this function calculates the element


stiffness matrix for each linear line element where k is element stiffness which is
calculated by EA/L where E is modulus of elasticity, A is the area of cross
section, and L is the length of element. It returns the 2×2 element stiffness matrix
k.
oneD_Assemble (K,k,i,j) this function assembles the element stiffness matrix k
of the linear lineelement joining nodes i and j into the global stiffness matrix K. it
returns the n×n global stiffness matrix K every time an element is assembled.
oneD_ElementStiffness(k)
function y = oneD_ElementStiffness(k)
%element_stiffness i.s this function call the stiffness matrix of
2*2 size
y=[k -k;-k k];
end

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

Generalized MATLAB program for a one dimensional problem:

clear ; close all;warning('off');


clc; format short g
Matlab_version=['Matlab-R' version('-release')];
disp
('===============================================================
========================
===============')
fprintf('This Output Window is generated on %s At %s in
%s\n',datestr
(now,'dd/mmmm/yyyy'),datestr(now,'HH:MM AM'),Matlab_version)
disp
('===============================================================
========================
===============')
%% Start from here---
%%
fprintf('\n SPRING ELEMENT PROBLEM\n\n' )
%% Input Parameter
disp
('===============================================================
========================
===============')
disp('INPUT PARAMETER:')

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)')

You might also like