You are on page 1of 59

Chapter

12
April 14, 2014
Chapter 12 includes a general introduction to MATLAB functions, selected topics in
linear algebra with MATLAB, and a collection of finite element programs for trusses
!Chapter 2", general one#dimensional problems !Chapter $", heat conduction in 2%
!Chapter &" and elasticit' in 2% !Chapter (") This Chapter is published electronic format
onl' for se*eral reasons
1) the data structure of the finite element program will be periodicall' updated to
reflect emerging finite element technologies and MATLAB s'nta+ changes,
2) to allow the course instructors to use their own MALAB or other finite element
codes)
-) to create a forum where students and instructors would e+change ideas and place
alternati*e finite element program data structures) The forum is hosted at
http..1coursefem)blogspot)com.
12.1 Using MATLAB for FEM
1

12.1.1 The MATLAB Windows
/pon opening MATLAB 'ou should see three windows the wor0space window, the
command window, and the command histor' window as shown in 1igure 12)1) 2f 'ou do
not see these three windows, or see more than three windows 'ou can change the la'out
b' clic0ing on the following menu selections 3iew 4 des0top la'out 4 default)
1
Ma' not be co*ered in the class) 5ecommended as independent reading)
Finite Element Programming with
MATLAB

Figure 12.1: Matlab Windows
12.1.2 The Command Window
2f 'ou clic0 in the command window a cursor will appear for 'ou to t'pe and enter *arious
commands) The cursor is indicated b' two greater than s'mbols !66")
12.1.3 Entering Expressions
After clic0ing in the command window 'ou can enter commands 'ou wish MATLAB to e+ecute)
Tr' entering the following &74) 8ou will see that MATLAB will then return ans 9 12)

12.1.4 Creating Variabes
:ust as commands are entered in MATLAB, *ariables are created as well) The general
format for entering *ariables is *ariable 9 e+pression) 1or e+ample, enter ' 9 1 in the
command window) MATLAB returns ' 9 1) A *ariable ' has been created and assigned
a *alue of 1) This *ariable can be used instead of the number 1 in future math operations)
1or e+ample t'ping ';' at the command prompt returns ans 9 1) MATLAB is case
sensiti*e, so '91, and 89$ will create two separate *ariables)
2
12.1.! "#n$tions
MATLAB has man' standard mathematical functions such as sine !sin!+"" and cosine
!cos!+"" etc) 2t also has software pac0ages, called toolbo+es, with speciali<ed functions
for specific topics)
12.1.% &etting 'ep and "inding "#n$tions
The abilit' to find and implement MATLAB=s functions and tools is the most important
s0ill a beginner needs to de*elop) MATLAB contains man' functions besides those
described below that ma' be useful)
There are two different wa's obtain help
1 > Clic0 on the little ?uestion mar0 icon at the top of the screen) This will open up the
help window that has se*eral tabs useful for finding information)
2 > T'pe @helpA in the command line MATLAB returns a list of topics for which it has
functions) At the bottom of the list it tells 'ou how to get more information about a
topic) As an e+ample, if 'ou t'pe @help s?rtA and MATLAB will return a list of
functions a*ailable for the s?uare root)
12.1.( Matrix Agebra with MATLAB
MATLAB is an interacti*e software s'stem for numerical computations and graphics) As
the name suggests, MATLAB is especiall' designed for matri+ computations) 2n addition,
it has a *ariet' of graphical and *isuali<ation capabilities, and can be e+tended through
programs written in its own programming language) Bere, we introduce onl' some basic
procedures so that 'ou can perform essential matri+ operations and basic programming
needed for understanding and de*elopment of the finite element program)
12.1.) *e+inition o+ matri$es
A matri+ is an mxn arra' of numbers or *ariables arranged in m rows and n columns, such
a matri+ is said to ha*e dimension mxn as shown below
11 12 1
21 22
1
n
m mn
a a a
a a
a a




=





a
L
M O
Bold letters will denote matrices or *ectors) The elements of a matri+ a are denoted b'
ij
a
, where i is the row number and j is the column number) Cote that in both describing
the dimension of the matri+ and in the subscripts identif'ing the row and column number,
the row number is alwa's placed first)
An e+ample of a -+- matri+ is
-
1 2 3
4 5 6
7 8 0



=





a
The abo*e matri+ a is is an e+ample of a square matrix since the number of rows and
columns are e?ual)
The following commands show how to enter matrices in MATLAB !66 is the
MATLAB prompt, it ma' be different with different computers or different *ersions of
MATLAB)"
>> a = [1 2 3; 4 5 6; 7 8 0]
a =
1 2 3
4 5 6
7 8 0
Cotice that rows of a matri+ are separated b' semicolons, while the entries on a row are
separated b' spaces !or commas") The order of matri+
a
can be determined from
( ) size a
The transpose of an' matri+ is obtained b' interchanging rows and columns) Do for
e+ample, the transpose of a is
1 4 7
2 5 8
3 6 0
T



=





a
2n MATLAB the transpose of a matri+ is denoted b' an apostrophe !E")
2f
T
= a a
, the matri+
a
is symmetric)
A matri+ is called a column matri+ or a *ector if n=1, e)g)
1
2
3
b
b
b



=




b
2n MATLAB, single subscript matrices are considered row matrices, or row *ectors)
Therefore, a column *ector in MATLAB is defined b'
4
>> b = [1 2 3]'
b =
1
2
3
Cote the transpose that is used to define b as a column matri+) The components of the
*ector b are
1 2 3
, , b b b
) The transpose of b is a row *ector
1 2 3
T
b b b

=


b
or in MATLAB
>> b = [1 2 3]
b =
1 2 3
A matri+ is called a diagonal matrix if onl' the diagonal components are non<ero,
i)e),
0,
ij
a i j =
) 1or e+ample, the matri+ below is a diagonal matri+
1 0 0
0 $ 0
0 0 F
a
1
1

1
1
]
A diagonal matri+ in MATLAB is constructed b' first defining a row *ector b 9 G1 $ FH,
and then placing this row *ector on the diagonal
>> b = [1 5 6];
>> a = diag (b)
a =
1 0 0
0 5 0
0 0 6
A diagonal matri+ where all diagonal components are e?ual to one is called an identity or
unit matrix and is denoted b' I) 1or e+ample, 2 2 identit' matri+ is gi*en b'
1 0
0 1


=



I
$
The MATLAB e+pression for an order n unit matri+ is
( ) eye n
Thus, the MATLAB e+pression
(2) eye =
gi*es the abo*e matri+)
A matri+ in which all components are <ero is called a zero matrix and is denoted b' 0) 2n
MATLAB, B 9 zeros (m, n) creates
m n
matri+ B of <eros) A random
m n
matri+ can
be created b' rand (m,n))
2n finite element method, matrices are often sparse, i)e), the' contain man' <eros)
MATLAB has the abilit' to store and manipulate sparse matrices, which greatl' increases
its usefulness for realistic problems) The command sparse (m, n) stores an
m n
<ero
matri+ in a sparse format, in which onl' the non<ero entries and their locations are sorted)
The non<ero entries can then be entered one#b'#one or in a loop)
>> a = s!a"se (3#2)
a =
$%% ze"& s!a"se' 3(by(2
>> a(1#2)=1;
>> a(3#1)=4;
>> a(3#2)=(1;
>> a
a =
(3#1) 4
(1#2) 1
(3#2) (1
Cotice that the displa' in an' MATLAB statement can be suppressed b' ending the line
with a semicolon)
The in*erse of a s?uare matri+ is defined b'
1 1 - -
= = a a aa I
if the matri+
a
is not singular) The MATLAB e+pression for the in*erse is
( ) in) a
) Linear
algebraic e?uations can also be sol*ed b' using bac0slash operator as shown in Dection
1)-)10, which a*oids computations of the in*erse and is therefore faster)
The matri+
a
is nonsingular if its determinant, denoted b'
( ) det a , is not e?ual to
<ero) A determinant of a 2+2 matri+ is defined b'
F
( )
11 12
11 22 12 21
22 22
det
a a
a a a a
a a


= = -


a a
The MATLAB e+pression for the determinant is
det ( ) a
1or e+ample,
>> a = [1 3; 4 2];
>> de* (a)
ans =
(10
12.1., -peration with matri$es
Addition and Subtraction
11 11 12 12 1 1
21 21 22 22
1 1
n n
m m mn mn
a b a b a b
a b a b
a b a b





= =






c b
L
M O
a
An e+ample of matri+ addition in MATLAB is gi*en below
>> a = [1 2 3;4 5 6;7 8 +];
>> a = [1 1 1;2 2 2;3 3 3];
>> , = [1 2;3 4;5 6];
>> a-b
ans =
2 3 4
6 7 8
10 11 12
>> a-,
... /""&" 0sing ==> -
1a*"i2 dimensi&ns m0s* ag"ee
I
Multiplication
1. Multiplication of a matrix by a scalar
11 12 1 11 12 1
21 22 21 22
1 1
n n
m mn m mn
a a a ,a ,a ,a
a a ,a ,a
, ,
a a ,a ,a




= =





a
L L
M O M O
. !calar product of two column "ectors
1
2
1 2
1
n
T
n i i
i
n
b
b
a a a ab
b
=





= =






b K
M
a
2n MATLAB the scalar product as defined abo*e is gi*en b' either * a bor
( , ) d&* a b
)
The length of a *ector a is denoted b' JaJ and is gi*en b'
2 2 2
1 2 n
a a a = + + + L a
The length of a *ector is also called its norm)
#. $roduct of two matrices
The product of two matrices a
( ) m 3
and b
( ) 3 n
is defined as
1 1 1 2 1
1 1 1
2 1 2 2
1 1
1
1 1
3 3 3
j j j j j jn
j j j
3 3
j j j j
j j
3 3
mj j mj jn
j j
a b a b a b
a b a b
a b a b
= = =
= =
= =







= =











c ab
L
M O
Alternati*el' we can write the abo*e as
1
n
ij i3 3j
3
, a b
=
=

&
Cote the the i,j entr' of c is the scalar product of row i of a and column j of b)

The product of two matrices a and b c is defined onl' if the number of columns in
a e?uals the number of rows in a) 2n other words, if a is an
( ) m 3
matri+, then
b must be an
( ) 3 n
matri+, where % is arbitrar') The product c will then ha*e
the same number of rows as a and the same number of columns as b, i)e) it will be
an
m n
matri+)
An important fact to remember is that matri+ multiplication is not commutati"e,
i)e) ab ba e+cept in unusual circumstances)
The MATLAB e+pression for matri+ multiplication is
* , a b =
Consider the same matrices a and c as before) An e+ample of matri+
multiplication with MATLAB is
>> a4,
ans =
22 28
4+ 64
76 100
>> ,4,
... /""&" 0sing ==> 4
nne" ma*"i2 dimensi&ns m0s* ag"ee5
&. 't(er matrix operations
a" Transpose of product ( )
T T T
= ab b a
b" Kroduct with identit' matri+ = aI a
c" Kroduct with <ero matri+ = a0 0
12.1.1. /o#tion o+ s0stem o+ inear e1#ations
Consider the following s'stem of n e?uations with n un0nowns,
3
d
,
1, 2, , : 3 n = L
(
11 1 2 2 1 1
21 1 22 2 2 2
1 1 2 2
n n
n n
n n nn n n
6 d 6d 6 d 7
6 d 6 d 6 d 7
6 d 6 d 6 d 7
+ + + =

+ + + =

+ + + =

L
L
M
L
Le can rewrite this s'stem of e?uations in matri+ notation as follows
= Kd f
where
11 12 1 1 1
21 22 2 2
1
n
n nn n n
6 6 6 7 d
6 6 7 d
6 6 7 d




= = =





K f d
L
M O M M
The s'mbolic solution of the abo*e s'stem of e?uation can be found b' multipl'ing both
sides with in*erse of K, which 'ields
1 -
= d K f
MATLAB e+pression for sol*ing the s'stem of e?uations is
\ d 6 7 =
or
( ) * d in) 6 7 =
An e+ample of solution of s'stem of e?uations with MATLAB is gi*en below
10
>> $ = "and (3#3)
$ =
0521+0 0567+3 0551+4
050470 05+347 058310
05678+ 053835 050346
>> b = "and (3#1)
b =
050535
0552+7
056711
>> 2 = $8b
2 =
(15+53380
31458625
(34455078
As mentioned before, the bac0slash pro*ides a faster wa' to sol*e e?uations and should
alwa's be used for large s'stems) The reason for this is that the bac0slash uses
elimination to sol*e with one right hand side, whereas determining the in*erse of an n+n
matri+ in*ol*es sol*ing the s'stem with n right hand sides) Therefore, the bac0slash
should alwa's be used for sol*ing large s'stem of e?uations)
12.1.11 /trings in MATLAB
MATLAB *ariables can also be defined as strin) *ariables) A string character is a te+t
surrounded b' single ?uotes) 1or e+ample
>> s*"='9e%%& :&"%d'
s*" =
9e%%& :&"%d
2t is also possible to create a list of strings b' creating a matri+ in which each row is a
separate string) As with all standard matrices, the rows must be of the same length) Thus
11
>> s*";ma* = ['s*"ing $' ; 's*"ing <']
s*";ma* =
s*"ing $
s*"ing <
Dtrings are used for defining file names, plot titles, and data formats) Dpecial built#in
string manipulation functions are a*ailable in MATLAB that allow 'ou to wor0 with
strings) 2n the MATALB codes pro*ided in the boo0 we ma0e use of strings to compare
functions) 1or e+ample the function strcmpi compares two strings

>> s*" = '!"in* &0*!0*';
>> s*",m!i(s*"#'=>?T @AT=AT')
ans =
1
A true statment results in 1 and a false statement in *) To get a list of all the built#in
MATLAB functions t'pe
>> 9e%! s*"70n
Another function used in the codes is fprintf) This function allows the user to print to the
screen !or to a file" strings and numeric information in a tabulated fasion) 1or e+ample
>>7!"in*7(1#'T9e n0mbe" &7 n&des in *9e mes9 is Bd 8n'#10)
T9e n0mbe" &7 n&des in *9e mes9 is 10
The first argument to the function tells MATLAB to print the message to the screen) The
second argument is a string, where +d defines a decimal character with the *alue of 1*
and the ,n defines a new line) To get a complete description t'pe
>> 9e%! 7!"in*7
12.1.11 2rogramming with MATLAB
MATLAB is *er' con*enient for writing simple finite element programs) 2t pro*ides the
standard constructs, such as loops and conditionals, these constructs can be used
interacti*el' to reduce the tedium of repetiti*e tas0s, or collected in programs stored in
MMm#filesMM !nothing more than a te+t file with e+tension NN)mMM")
12
12.1.11.1 Conditional and Loops
MATLAB has a standard if#elseif#else conditional)
The general form An e+ample
if e+pression1
statements1
elseif e+pression2
statements2
O
O
O
else
statements
end
66 t 9 0)IF,
66 if t 6 0)I$
s 9 0,
elseif t P 0)2$
s 9 1,
else
s 9 1#2;!t#0)2$",
end
66 s
s 9
0
MATLAB pro*ides two t'pes of loops, a for#loop !comparable to a 1ortran do#loop or a
C for#loop" and a while#loop) A for#loop repeats the statements in the loop as the loop
inde+ ta0es on the *alues in a gi*en row *ector, the while#loop repeats as long as the
gi*en e+pression is true !non<ero"
The general form Q+amples
for inde+ 9 startincrementend
statements
end
66 for i911-
disp!iR2"
end
1
4
(
while e+pression
statements
end
66 +91,
66 while 17+ 6 1
+ 9 +.2,
end
66 +
+ 9
1)1102e#1F
12.1.11.2 Functions
1unctions allow the user to create new MATLAB commands) A function is defined in an
m#file that begins with a line of the following form
function -output1,output,.... = cmd/name(input1,input,...)
The rest of the m#file consists of ordinar' MATLAB commands computing the *alues of
the outputs and performing other desired actions) Below is a simple e+ample of a
function that computes the ?uadratic function
2
( ) 3 1 7 2 2 2 = - - ) The following
commands should be stored in the file fcn.m !the name of the function within MATLAB
is the name of the m#file, without the e+tension"
1-
70n,*i&n y = 7,n( 2 )
y=2C2(342(1;
T9en *y!e ,&mmand'
>> 7,n(051)
ans =
(152+00
12.1.12 Basi$ graphi$s
MATLAB is an e+cellent tool for *isuali<ing and plotting results) To plot a graph the user
specifies the + coordinate *ector and ' coordinate *ector using the following s'nta+
>> 2=[0'0501'1];
>> y=25C2;
>> !%&*(2#y);
The abo*e will generate
Figure 12.2 Typical outpout of plot(x,y) function
3arious line t'pes, plot s'mbols and colors ma' be obtained with plot(x,y,s) where s is a
character string consisting of elements from an' combination of the following - columns

b blue ) point # solid
g green o circle dotted
r red + +#mar0 #) dashdot
c c'an 7 plus ## dashed
m magenta ; star !none" no line
' 'ellow s s?uare
0 blac0 d diamond
14

To add a title, + and ' labels, or a grid, the user should use the following MATLAB
functions) Cote that the arguments to the functions are strings
>> *i*%e(',i",%e');
>> 2%abe%('2');
>> y%abe%('y');
>> g"id
2n the MATLAB 1inite Qlement code pro*ided in the boo0, we also use two speciali<ed
plots) The first plot is the patc( function) This function is used to *isuali<e 2% pol'gons
with colors) The colors are interpolated from nodes of the pol'gon to create a colored
surface) The following e+ample generates a filled s?uare) The colors along the x a+is are
the same while the colors along the y a+is are interpolated between the *alues G0,1H)
>> 2 = [0 1 1 0];
>> y = [0 0 1 1];
>> , = [0 0 1 1];
>> !a*,9(2#y#,)
Figure 12.3 Typical outpout of patch(x,y,c) function
Le will use the patc( function to *isuali<e temperatures, stresses and other *ariables
obtained at the finite element solutions) Another speciali<ed plot function is the qui"er)
This function is used to *isuali<e gradients of functions as an arrow plot) The following
1$
e+ample demonstrates the use of qui"er function for plotting the gradients to the function
y=x

>> 2=0'051'1; y=25C2;


>> ,2=&nes(1#11); ,y=242;
>> !%&*(2#y); 9&%d &n
>> D0i)e"(2#y#,2#,y)
Figure 12.4 Typical outpout of quiver(x,y,cx,cy) function
The (old on command is used to hold the current plot and all a+is properties so that
subse?uent graphing commands will e+ecuted on the e+isting graph)
/sing the text function, the user can add to a plot a te+t message) 1or e+ample
*e2*(1#1#'7%02')
The first and second arguments define the position of the te+t on the plot, while the
string gi*es the te+t)
12.1.13 3emar4s
a" 2n practice the number of e?uations n can be *er' large) KCs can toda' sol*e
thousands of e?uations in a matter of minutes if the' are sparse !as the' are in
1QM anal'sis#'ou will learn about this later" but sometimes millions of
e?uations are needed, as for an aircraft carrier or a full model of an aircraft,
parallel computers are then needed)
b" Qfficient solution techni?ues that ta0e ad*antage of the sparsit' and other
ad*antageous properties of 1QM e?uations are essential for treating e*en
1F
moderatel' large s'stems) The issue of how to efficientl' sol*e large s'stems
will not be considered in this course)
c" 2n this course, we will see that
The matri+ corresponding to the s'stem of e?uations arising from
1QM !denoted as K" is non#singular !often called regular", i)e),
1 -
K
e+ists if the correct boundar' conditions are prescribed and the
elements are properl' formulated) 1urthermore, for good models it is
usuall' well#conditioned, which means it is not *er' sensiti*e to
roundoff errors)
K is s'mmetric, i)e)
T
= K K
)
K is positi*e definite, i)e), 0
T
> " x Kx x !meaning for an' *alue of x"
Alternati*el', K is said to be positi*e definite if all the eigen*alues are
strictl' positi*e) The eigen*alue problem consists of finding non<ero
eigen*ectors
y
and the corresponding eigen*alues l satisf'ing
l = Ky y
The MATLAB e+pression for the eigen*alues problem is
>> 6=[2 (2;(2 4];
>> [y# %amda]=eig(6)
y =
058507 (055257
(055257 058507
%amda =
05763+ 0
0 552361
12.2 Finite element programming with MATLAB for trusses
2n Chapter 2 the basic structure of the finite element method for truss structures
has been illustrated) 2n this section we present a simple finite element program using
MATLAB programming language) Dince MATLAB manipulates matrices and *ectors
with relati*e ease the reader can focus on fundamentals ideas rather than on algorithmic
details)
The code is written to *er' closel' follow the formulation gi*en in this chapter)
2n order to better understand how the program wor0s 1igure 2)& and Q+ample Kroblem
2)2 in Chapter 2 ha*e been included as e+amples sol*ed b' the program) Soing through
the code along with this guide and the e+ample problems is an effecti*e method to
comprehend the program)
The main routines in the finite element code are
1I
1) Kreprocessing including input data and assembling the proper arra's, *ectors,
and matrices)
2) Calculation of element stiffness matrices and force *ectors
-) %irect assembl' of matrices and *ectors
4) Kartition and solution
$) Kostprocessing for secondar' *ariables
Q+planation for *arious MATLAB routines !stored in ;)m files" are described as
comments within each subroutine)
12.2.1 5otations and de+initions
12.2.1.1 ser pro!ided
nsd" number of space dimension !1 for 1% problems"
ndof" number of degrees#of#freedom per node
nnp" number of nodal points
nel" number of elements
nen" number of element nodes !2 in this case"
nd" number of prescribed !0nown" displacements
CArea" cross#sectional area
0rea = CArea(element number)
#" 8oung=s Modulus
1oun) = #(element number)
len$" element length
2en)t( = len$(element number)
phi" angle from 2
a+is to + a+is for each element specified in degrees) 5emember,
2 is
alwa's from local node 1 to 2
p(i = phi(element number)
I#%" connecti*it' information matri+
)lobal node number = I#% (local node number, element number)
d&bar" prescribed displacement *ector '
d
in Q?) )
f&hat" gi*en force *ector '

f
in Q?) )
plot&truss" string for output control GE'es=H to plot truss elements
plot&nod" string for output control GE'es=H to plot truss global node numbers
plot&stress" string for output control GE'es=H to plot stresses
12.1.1.2 Calculated or deri!ed b( pro$ra)
ne*" total number of e?uations
K" global stiffness matri+
1&
d" global displacement *ector is stored as
for 1#% problems for 2#% problems
1
n
0
0







= d M
1
1
2
y
ny
0
0
0









= d
M

f" global force *ector !e+cluding the reactions" is stored as

for 1#% problems for 2#% problems
1
n
7
7








= f M

1
1
2
y
ny
7
7
7










= f
M

e" element number
+e" element stiffness matri+
de" element nodal displacement *ector
for 1#% problems for 2#% problems
1
2
0
0




9de
1
1
2
2
e
2
e
y
e
2
e
y
0
0
0
0










= de
LM" gather matri+
The gather matri+ is used to e+tract the element and local degrees#of#freedom) 2t
has the following structure
)lobal de)ree3of3freedom=LM (local de)ree3of3freedom, element number)

Lhen ndof , 1 !see e+ample in 1igure 2)&" 2QC and LM are defined as follows
} }
2 1
1 2
2 3
e e = =






, I#%
} }
2 1
1 2
2 3
e e = =






, LM
Lhen ndof , 2 !e+ample Kroblem 2)2", 2QC and LM are defined as
1(
} }
2 1
1 2
3 3
e e = =






, I#%
1 3
2 4
5 5
6 6









, LM
2n both e+amples, columns indicate the elements and rows indicate global degrees#of#
freedom)
K&#" partition of the global stiffness matri+ K based on Q?)
K&#F" partition of the global stiffness matri+ K based on Q?)
K&F" partition of the global stiffness matri+ K based on Q?)
d&F" un0nown !free" part of the global displacement *ector d based on Q?)
d&#" prescribed !essential" part of the global displacement *ector d based on
Q?)
f&#" reaction force !un0nown" *ector based on Q?)
stress" stress for each element
5emar0 2n this chapter nodes where the displacements are prescribed ha*e to be
numbered first)
12.21.2 MATLAB Finite element code for trusses
truss.)
%%%%%%%%%%%%%%%%%%%%%%
% 2D Truss (Chapter 2) %
% Haim Waisman, Rensselaer %
%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;

% include global variables
include!lags;

% "reprocessor "hase
#$,!,d% & preprocessor;
% Calculation and assembl' o! element matrices
!or e & ()nel
*e & trusselem(e);
$ & assembl'($,e,*e);
end
% +olution "hase
#d,!,% & solvedr($,!,d);

% "ostprocessor "hase
postprocessor(d)
20
include&fla$s.)
% !ile to include global variables
global nsd ndo! nnp nel nen ne- nd
global C.rea , leng phi
global plottruss plotnod plotstress
global /0 1,2 3 ' stress
preprocessor.)
% preprocessing4 read input data and set up mesh in!ormation
!unction #$,!,d% & preprocessor;
include!lags;

% input !ile to include all variables
input!ilee3ample22;
%input!ilee3ample25;

% generate /0 arra'
!or e & ()nel
!or 6 & ()nen
!or m & ()ndo!
ind & (67()8ndo! 9 m;
/0(ind,e) & ndo!81,2(6,e) 7 ndo! 9 m;
end
end
end

input&file&exa)ple2&2.)
% 1nput Data !or ,3ample 2:2
nsd & 2; % 2umber o! space dimensions
ndo! & 2; % 2umber o! degrees7o!7!reedom per node
nnp & ;; % 2umber o! nodal points
nel & 2; % 2umber o! elements
nen & 2; % 2umber o! element nodes

ne- & ndo!8nnp; % 2umber o! e-uations

! & <eros(ne-,(); % 1nitiali<e !orce vector
d & <eros(ne-,(); % 1nitiali<e displacement matri3
$ & <eros(ne-); % 1nitiali<e sti!!ness matri3

% ,lement properties
C.rea & #( ( %; % ,lements area
leng & #( s-rt(2)%; % ,lements length
phi & #=> ?@ %; % .ngle
, & #( ( %; % AoungBs 0odulus

% prescribed displacements
% displacement d(3 d(' d23 d2'
d & #> > > >%C;
nd & ?; % 2umber o! prescribed displacement degrees7o!7!reedom

21
% prescribed !orces
!(@) & (>; % Dorce at node ; in the 37direction
!(E) & >; % Dorce at node ; in the '7direction

% output plots
plottruss & C'esC;
plotnod & C'esC;
% mesh Feneration
trussmesh22;

truss&)esh&2&2.)
% geometr' and connectivit' !or e3ample 2:2
!unction trussmesh22
include!lags;

% 2odal coordinates (origin placed at node 2)
3 & #(:> >:> (:> %; % 3 coordinate
' & #>:> >:> (:> %; % ' coordinate

% connectivit' arra'
1,2 & #( 2
; ;%;
% plot truss
plottruss;
input&file&exa)ple2&-.)
% 1nput Data !rom Chapter 2 Digure 2:5
nsd & (; % 2umber o! spatial dimensions
ndo! & (; % 2umber o! degrees7o!7!reedom per node
nnp & ;; % Total number o! global nodes
nel & 2; % Total number o! elements
nen & 2; % 2umber o! nodes in each element

ne- & ndo!8nnp; % 2umber o! e-uations

! & <eros(ne-,(); % 1nitiali<e !orce vector
d & <eros(ne-,(); % 1nitiali<e displacement vector
$ & <eros(ne-); % 1nitiali<e sti!!ness matri3

% ,lement properties
C.rea & #:@ (%; % ,lements cross7sectional area
leng & #2 2%; % ,lements length
, & #( (%; % AoungBs 0odulus
% prescribed displacements
d(() & >;
nd & (; % 2umber o! prescribed displacement degrees o!
!reedom

% prescribed !orces
22
!(;) & (>; % !orce at node ; in the 37direction

% output controls
plottruss & C'esC;
plotnod & C'esC;

% mesh generation
trussmesh25;
truss&)esh&2&-.)
% geometr' and connectivit' !or e3ample problem in Digure 2:5
!unction trussmesh25;
include!lags;

% 2ode coordinates (origin placed at node ()
3 & #>:> (:> 2:> %; % 3 coordinate
' & #>:> >:> >:> %; % ' coordinate

% connectivit' arra'
1,2 & #( 2
2 ;%;

% plot truss
plottruss;


.lottruss.)
% !unction to plot the elements, global node numbers and print mesh
parameters
!unction plottruss;
include!lags;

% chec* i! truss plot is re-uested
i! strcmpi(plottruss,C'esC)&&(;
!or i & ()nel
GG & #3(1,2((,i)) 3(1,2(2,i)) 3(1,2((,i)) %;
AA & #'(1,2((,i)) '(1,2(2,i)) '(1,2((,i)) %;
line(GG,AA);hold on;

% chec* i! node numbering is re-uested
i! strcmpi(plotnod,C'esC)&&(;
te3t(GG((),AA((),sprint!(C%>:@gC,1,2((,i)));
te3t(GG(2),AA(2),sprint!(C%>:@gC,1,2(2,i)));
end
end
title(CTruss "lotC);
end

% print mesh parameters
!print!((,CHtTruss "arams HnC);
!print!((,C2o: o! ,lements %d HnC,nel);
!print!((,C2o: o! 2odes %d HnC,nnp);
2-
!print!((,C2o: o! ,-uations %d HnHnC,ne-);


trussele).)
% generate the element sti!!ness matri3 !or each element
!unction *e & trusselem(e)
include!lags;

const & C.rea(e)8,(e)Ileng(e); % constant coe!!icient Jithin the
truss element

i! ndo! && (
*e & const 8 #( 7( ; % (7D sti!!ness
7( (%;
elsei! ndo! && 2
p & phi(e)8piI(5>; % Converts degrees to radians

s & sin(p); c & cos(p);
s2 & sK2; c2 & cK2;

*e & const8#c2 c8s 7c2 7c8s; % 27D sti!!ness
c8s s2 7c8s 7s2;
7c2 7c8s c2 c8s;
7c8s 7s2 c8s s2%;

end
asse)bl(.)
% assemble element sti!!ness matri3
!unction $ & assembl'($,e,*e)
include!lags;

!or loop( & ()nen8ndo!
i & /0(loop(,e);
!or loop2 & ()nen8ndo!
6 & /0(loop2,e);
$(i,6) & $(i,6) 9 *e(loop(,loop2);
end
end

sol!edr.)
% partition and solve the s'stem o! e-uations
!unction #d,!,% & solvedr($,!,d)
include!lags;

% partition the matri3 $, vectors ! and d
$, & $(()nd,()nd); % ,3tract $,
matri3
$D & $(nd9()ne-,nd9()ne-); % ,3tract $, matri3
$,D & $(()nd,nd9()ne-); % ,3tract $,D
matri3
24
!D & !(nd9()ne-); % ,3tract !D
vector
d, & d(()nd); % ,3tract
d, vector

% solve !or dD
dD &$DH( !D 7 $,DC8 d,);

% reconstruct the global displacement d
d & #d,
dD%;

% compute the reaction r
!, & $,8d,9$,D8dD;

% Jrite to the Jor*space
solutionvectord & d
reactionsvector & !,
postprocessor.)
% postprocessing !unction
!unction postprocesser(d)
include!lags;

% prints the element numbers and corresponding stresses
!print!((,CelementHtHtHtstressHnC);
% compute stress vector
!or e&()nel
de & d(/0(),e)); % displacement at the current
element
const & ,(e)Ileng(e); % constant parameter Jithin the
element

i! ndo! && ( % Dor (7D truss element
stress(e) & const8(#7( (%8de);
end
i! ndo! && 2 % Dor 27D truss element
p & phi(e)8piI(5>; % Converts
degrees to radians
c & cos(p); s & sin(p);
stress(e) & const8#7c 7s c s%8de; % compute
stresses
end

!print!((,C%dHtHtHt%!HnC,e,stress(e));
end
12.3 hape functions and !auss "uadrature with MATLAB
2n Chapter 2 the basic finite element programming structure was introduced for
one# and two#dimensional anal'sis of truss structures) 2n this section we gi*e the
functions for the construction of element shape functions in one#dimension and their
deri*ati*es) The shape functions are defined in the ph'sical coordinate s'stem)
2$
12.3.1 5otations and de+initions
xe" element nodal +#coordinates
xt" + coordinate at which the functions are e*aluated
%" arra' of shape functions
/" arra' of deri*ati*es of the shape functions
$p" arra' of position of Sauss points in the parent element domain #
1 2 ng!
x x x



L
0" arra' of weights #
1 2 ng!
E E E



L
12.3.2 MATLAB $ode +or shape +#n$tions and deri6ati6es
%)atrix11.)
% shape !unctions computed in the ph'sical coordinate 7 3t
!unction 2 & 2matri3(D(3t,3e)
include!lags;

i! nen && 2 % linear shape !unctions
2(() & (3t73e(2))I(3e(()73e(2));
2(2) & (3t73e(())I(3e(2)73e(());
elsei! nen && ; % -uadratic shape !unctions
2(()&(3t73e(2))8(3t73e(;))I((3e(()73e(2))8(3e(()73e(;)));
2(2)&(3t73e(())8(3t73e(;))I((3e(2)73e(())8(3e(2)73e(;)));
2(;)&(3t73e(())8(3t73e(2))I((3e(;)73e(())8(3e(;)73e(2)));
end
/)atrix11.)
% derivative o! the shape !unctions computed in the ph'sical coordinate
7 3t
!unction L & Lmatri3(D(3t,3e)
include!lags;

i! nen && 2 % derivative o! linear shape !unctions (constant)
L & (I(3e(()73e(2))8#7( (%;
elsei! nen && ; % derivative o! -uadratic shape !unctions
L(()&(283t73e(2)73e(;))I((3e(()73e(2))8(3e(()73e(;)));
L(2)&(283t73e(()73e(;))I((3e(2)73e(())8(3e(2)73e(;)));
L(;)&(283t73e(()73e(2))I((3e(;)73e(())8(3e(;)73e(2)));
end
2F
12.3.3 MATLAB $ode +or &a#ss 1#adrat#re
$auss.)
% get gauss points in the parent element domain #7(, (% and the
corresponding Jeights
!unction #J,gp% & gauss(ngp)

i! ngp && (
gp & >;
J & 2;
elsei! ngp && 2
gp & #7>:@MM;@>2M, >:@MM;@>2M%;
J & #(, (%;
elsei! ngp && ;
gp & #7>:MM?@=EEE=2, >:MM?@=EEE=2, >:>%;
J & #>:@@@@@@@@@E, >:@@@@@@@@@E, >:555555555=%;
end
12.# Finite element programming in 1$ with MATLAB
2n Dection 12)2 the basic finite element programming structure was introduced
for one# and two# dimensional anal'sis of truss structures) 2n 12)-, the program functions
for the calculation of the element shape functions, their deri*ati*es and Sauss ?uadrature
in one#dimension were introduced) 2n this section we introduce a more general finite
element program structure for one#dimensional problems that in principle is similar to
that in multidimensions to be de*eloped in Dections 12)$ and 12)F for heat conduction
and elasticit' problems, respecti*el')
2n Chapter 2 we discussed *arious methodologies for imposing boundar'
conditions) 2n the partition#based approach, the so#called 43nodes !where displacements
are prescribed" are numbered first) 2n general, howe*er, node and element numberings
are initiall' defined b' mesh generators and subse?uentl' renumbered to ma+imi<e
efficienc' of sol*ing a s'stem of linear e?uations) 2n our implementation we tag nodes
located on the natural boundar' or essential boundar') Codes on a natural boundar' are
assigned flag=1, while nodes on an essential boundar' are tagged as flag=2)
Dubse?uentl', nodes are renumbered b' the program so that 43nodes are numbered first)
This is accomplished b' constructing the ID and LM arra's in the function
setup_ID_LM) Lith some minor modifications the program for the one#dimensional
elasticit' problems can be modified to anal'<e heat conduction problems)
Q+planation for *arious MATLAB routines is gi*en as comments within each function)
Tnl' the nomenclature and definitions which ha*e been modified from the pre*ious
chapters are included below) Much of the code is either identical or *er' similar to the
code de*eloped in Dection 12)2) An input file for the Q+ample $)2 in Chapter $ modeled
2I
with two ?uadratic elements is gi*en below) Additional input files for one ?uadratic
element mesh and four ?uadratic elements mesh are pro*ided in the dis0)
12.4.1 5otations and de+initions
User provided
nd5 number of nodes on the essential boundar' !4#nodes"
n)p5 number of Sauss points
body5 *ector of *alues of bod' forces U defined at the nodes and then interpolated using
shape functions
E: *ector of nodal *alues of 8oung=s modulus
CArea: *ector of nodal *alues of cross#sectional area
fla)s5 1lag arra' denoting essential and natural boundar' conditions
fla)s(6nitial )lobal node number) = fla) "alue
1lag *alues are 1 U natural boundar', 2 U essential boundar'
x5 *ector of nodal x#coordinates
y5 *ector of nodal y#coordinates !used for the plots onl'"
e/bc5 *ector of essential boundar' conditions !displacements or temperatures"
n/bc5 *ector of natural boundar' conditions !tractions or boundar' flu+es"
$5 *ector of point forces !point sources in heat conduction"
xp5 *ector of the x#coordinates where the point forces are applied
np5 number of point forces !point sources in heat conduction"
nplot5 number of points used to plot displacements and stresses !temperatures and flu+es
in heat conduction"
6475 location matri+
The location matri+ relates initial global node number and element local node
numbers) Dubse?uentl' nodes are renumbered !see setup/68/2M.m" so that
43nodes are numbered first) 2QC matri+ has the following structure
( , ) ni*ia% g%&ba% n&de n0mbe" /? %&,a% n&de n0mbe" e%emen* n0mbe" =
Calculated by FE program:
685 %estination arra'
( ) >e&"de"ed g%&ba% n&de n0mbe" F ni*ia% g%&ba% n&de n0mbe" =
2M5 Location matri+

( , ) >e&"de"ed g%&ba% n&de n0mbe" G1 G&,a% n&de n0mbe" e%emen* n0mbe" =
Cote that 2M matri+ is related to 647 matri+ b'
2&
( , ) ( ( , )) G1 e F /? e =
12.4.2 MATLAB "inite eement $ode +or one7dimensiona probems
bar11.)
%%%%%%%%%%%%%%%%%%
% (D D,0 "rogram (Chapter @) %
% Haim Waisman, Rensselaer %
%%%%%%%%%%%%%%%%%%
clear all;
close all;

% include global variables
include!lags;

% "reprocessing
#$,!,d% & preprocessor;

% ,lement matri3 computations and assembl'
!or e & ()nel
#*e,!e% & barelem(e);
#$, !% & assembl'($,!,e,*e,!e);
end

% .dd nodal boundar' !orce vector
! & 2aturalLC(!);

% "artition and solution
#d,!,% & solvedr($,!,d);

% "ostprocessing
postprocessor(d);

% plot the e3act solution
,3act+olution;

include&fla$s.)
% 1nclude global variables
global nsd ndo! nnp nel nen ne- nd C.rea ,
global !lags 1D 1,2 /0 bod' 3 '
global 3p " ngp 3plot nbc ebc np
global plotbar plotnod nplot
preprocessor.)
% preprocessing4 reads input data and sets up mesh in!ormation
!unction #$,!,d% & preprocessor;
include!lags;

% input !ile to include all variables
input!ile@22ele;
2(
%input!ile@2(ele;
%input!ile@2?ele;

% generate /0 and 1D arra's
d & setup1D/0(d);
input&file2&2&2ele.)
% 1nput Data !or ,3ample @:2 (2 elements)

nsd & (; % number o! space dimensions
ndo! & (; % number o! degrees7o!7!reedom per node
nnp & @; % number o! nodal points
nel & 2; % number o! elements
nen & ;; % number o! element nodes

ne- & ndo!8nnp; % number o! e-uations

! & <eros(ne-,(); % initiali<e nodal !orce vector
d & <eros(ne-,(); % initiali<e nodal displacement vector
$ & <eros(ne-); % initiali<e sti!!ness matri3

!lags & <eros(ne-,(); % initiali<e !lag vector
ebc & <eros(ne-,(); % initiali<e vector o! essential boundar'
condition
nbc & <eros(ne-,(); % initiali<e vector o! natural boundar' condition

% element and material data (given at the element nodes)
, & 58ones(nnp,(); % nodal values AoungCs
modulus
bod' & 58ones(nnp,(); % nodal values bod'
!orces
C.rea & #? M (> (( (2%C; % nodal values o!
cross7sectional area

% gauss integration
ngp & 2; % number o! gauss points

% essential boundar' conditions
!lags(() & 2; % !lags to mar* nodes located on the essential
boundar'
ebc(() & >; % value o! essential L:C
nd & (; % number o! nodes on the essential boundar'

% natural boundar' conditions
!lags(@) & (; % !lags to mar* nodes located on the natural
boundar'
nbc(@) & >; % value o! natural L:C

% point !orces
" & 2?; % arra' o! point !orces
3p & @; % arra' o! coordinates Jhere point !orces are
applied
np & (; % number o! point !orces

-0
% output plots
plotbar & C'esC;
plotnod & C'esC;
nplot & nnp8(>; % number o! points in the element to plot
displacements and stresses

% mesh generation
barmesh@22ele;

bar&)esh2&2&2ele.)
!unction barmesh@22ele
include!lags;

% 2ode) ( 2 ; ? @
3 & #2:> ;:@ @:> @:@ E:> %; % 3 coordinate
' & 283; % ' is used onl' !or
the bar plot

% connectivit' arra'
1,2 & # ( ;
2 ?
; @%;
plotbar;

setup&I1&LM.)
% setup 1D and /0 arra's
!unction d & setup1D/0(d);
include!lags;

count & >; count( & >;
!or i & ()ne-
i! !lags(i) && 2 % chec* i! essential boundar'
count & count 9 (;
1D(i) & count; % number !irst the nodes on
essential boundar'
d(count)& ebc(i); % store the reordered values o!
essential L:C
else
count( & count( 9 (;
1D(i) & nd 9 count(;
end
end

!or i & ()nel
!or 6 & ()nen
/0(6,i)&1D(1,2(6,i)); % create the /0 matri3
end
end
-1
barele).)
% generate element sti!!ness matri3 and element nodal bod' !orce vector
!unction #*e, !e% & barelem(e);
include!lags;

1,2e & 1,2(),e); % e3tract local connectivit'
in!ormation
3e & 3(1,2e); % e3tract element 3
coordinates
N & (3e(nen) 7 3e(())I2; % compute Nacobian
#J , gp% & gauss(ngp); % e3tract Fauss points and
Jeights

*e & <eros(nen,nen); % initiali<e element sti!!ness
matri3
!e & <eros(nen,(); % initiali<e element nodal !orce
vector

!or i & ()ngp
3t & >:@8(3e(()93e(nen))9N8gp(i); % Compute Fauss points
in ph'sical coordinates

2 & 2matri3(D(3t,3e); % shape !unctions matri3
L & Lmatri3(D(3t,3e); % derivative o! shape
!unctions matri3

.e & 28C.rea(1,2e); % cross7sectional area at element
gauss points
,e & 28,(1,2e); % AoungCs modulus at element gauss
points
be & 28bod'(1,2e); % bod' !orces at element
gauss points
*e & *e 9 J(i)8(LC8.e8,e8L); % compute element sti!!ness
matri3
!e & !e 9 J(i)82C8be; % compute element nodal
bod' !orce vector
end
*e & N8*e;
!e & N8!e;

% chec* !or point !orces in this element
!or i&()np % loop over all point !orces
"i & "(i); % e3tract point !orce
3pi & 3p(i); % e3tract the location o! point
!orce Jithin an element
i! 3e(()O&3pi P 3piO3e(nen)
!e & !e 9 "i8#2matri3(D(3pi,3e)%C; % add to the nodal
!orce vector
end
end

-2
asse)bl(.)
% assemble element sti!!ness matri3 and nodal !orce vector
!unction #$,!% & assembl'($,!,e,*e,!e)
include!lags;

!or loop( & ()nen
i & /0(loop(,e);
!(i) & !(i) 9 !e(loop(); % assemble nodal !orce vector
!or loop2 & ()nen
6 & /0(loop2,e);
$(i,6) & $(i,6) 9 *e(loop(,loop2); % assemble sti!!ness matri3
end
end

natural/C.)
% compute and assemble nodal boundar' !orce vector
!unction ! & naturalLC(!);
include!lags;

!or i & ()nnp
i! !lags(i) && (
node & 1D(i);
!(node) & !(node) 9 C.rea(node)8nbc(node);
end
end
postprocessor.)
% postprocessing
!unction postprocessor(d)
include!lags;

!print!((,CHn "rint stresses at the Fauss points HnC)
!print!((,C,lementHtHt 3(gauss() HtHt 3(gauss2) HtHt stress(gauss()
HtHt stress(gauss2)HnC)

!print!((,C777777777777777777777777777777777777777777777777777777777777
777777777777777777777 HnC)

% loop over elements to compute the stresses
!or e & ()nel
% compute stresses and displacements !or the current element
dispandstress(e,d);
end

12.% MATLAB finite element program for heat conduction in 2$
2n Dection 12)2 the basic finite element program structure was introduced for one# and
two# dimensional anal'sis of truss structures) 2n Dection 12)- a more general finite
element program structure for one#dimensional problems was de*eloped) 2n this section
we describe a finite element program for scalar field problems in two#dimensions
--
focusing on heat conduction) 8ou will notice that the program structure is *er' similar to
that introduced for one#dimensional problems) A brief description of *arious functions is
pro*ided below)
Main pro$ra)" (eat8.m
The main program is gi*en in (eat8.m file) The finite element program structure
consists of the following steps
# preprocessing
# e*aluation of element conductance matrices, element nodal source *ectors and
their assembl'
# adding the contribution from point sources and nodal boundar' flu+ *ector
# solution of the algebraic s'stem of e?uations
# postprocessing
heat2d.)
%%%%%%%%%%%%%%%%%%%%%
% Heat conduction in 2D (Chapter 5) %
% Haim Waisman, Rensselaer %
%%%%%%%%%%%%%%%%%%%% %
clear all;
close all;

% 1nclude global variables
include!lags;

% "reprocessing
#$,!,d% & preprocessor;
% ,valuate element conductance matri3, nodal source vector and assemble
!or e & ()nel
#*e, !e% & heat2Delem(e);
#$,!% & assembl'($,!,e,*e,!e);
end

% Compute and assemble nodal boundar' !lu3 vector and point sources
! & srcand!lu3(!);

% +olution
#d,!,% & solvedr($,!,d);

% "ostprocessing
postprocessor(d);
.reprocessin$" preprocessor.m
2n the preprocessing phase, the input file !input_file", which defines material properties,
mesh data, *ector and matrices initiali<ations, essential and natural conditions, point
sources, the re?uired output, is defined b' the user) 2n the implementation pro*ided here,
flu+es are prescribed along element edges and are defined b' nodal *alues interpolated
using shape functions) The n_bc arra' is used for the flu+es data structure) 1or the heat
-4
conduction problem gi*en in Q+ample &)1 with 1F ?uadrilateral elements !see 1igure
&)(", the n_bc arra' is defined as follows
21 22 2- 24
22 2- 24 2$
V
20)0 20)0 20)0 20)0
20)0 20)0 20)0 20)0
n bc
1
1
1

1
1
]
The number of columns corresponds to the number of edges !specified b' nbe) on the
natural boundar', the first and second rows indicate the first and the second node
numbers that define the element edge, the third and fourth rows correspond to the
respecti*e nodal flu+ *alues) Cote that a discontinuit' in flu+es at the element boundaries
could be prescribed) The input files for the 1#element and F4#element meshes are gi*en
on the website)
2n the preprocessing phase, the finite mesh is generated and the wor0ing arra's IEN, ID
and LM are defined) The mesh generation function mesh2d utili<es MATLAB=s built#in
function linspace !see Chapter 1" for bisection of lines)
preprocessor.)
!unction #$,!,d% & preprocessor;
include!lags;

% read input !ile
%input!ile(ele;
input!ile(Eele;
%input!ileE?ele;

% generate 1D and /0 arra's
d & setup1D/0(d);

input&file&13ele.)
% 1nput !ile !or ,3ample 5:( ((E7element mesh)

% material properties
* & @; % thermal conductivit'
D & *8e'e(2); % conductivit' matri3

% mesh speci!ications
nsd & 2; % number o! space dimensions
nnp & 2@; % number o! nodes
nel & (E; % number o! elements
nen & ?; % number o! element nodes
ndo! & (; % number o! degrees7o!7!reedom per node
ne- & nnp8ndo!; % number o! e-uations

-$
! & <eros(ne-,(); % initiali<e nodal !lu3 vector
d & <eros(ne-,(); % initiali<e nodal temperature vector
$ & <eros(ne-); % initiali<e conductance matri3

!lags & <eros(ne-,(); % arra' to set L:C !lags
ebc & <eros(ne-,(); % essential L:C arra'
nbc & <eros(ne-,(); % natural L:C arra'
" & <eros(ne-,(); % initiali<e point source vector
de!ined at a node
s & E8ones(nen,nel); % heat source de!ined over the nodes

ngp & 2; % number o! Fauss points in
each direction

% essential L:C:
!lags(()@) & 2; ebc(()@) & >:>;
!lags(E)@)2() & 2; ebc(E)@)2() & >:>;
nd & =; % number o! nodes on essential boundar'

% Jhat to plot
compute!lu3 & C'esC;
plotmesh & C'esC;
plotnod & C'esC;
plottemp & C'esC;
plot!lu3 & C'esC;

% natural L:C 7 de!ined on edges positioned on the natural boundar'
nbc & # 2( 22 2; 2? % node(
22 2; 2? 2@ % node2
2> 2> 2> 2> % !lu3 value at
node (
2> 2> 2> 2> %; % !lu3 value at
node 2
nbe & ?; % number o! edges on the natural
boundar'

% mesh generation
mesh2d;

)esh2d.)
!unction mesh2d;
include!lags;

lp & s-rt(nnp); % number o! nodes in 3 and '
direction
3> & linspace(>,2,lp); % e-ual bisection o! the 3 nodes
'> & >:@83>I2; % ' coordinates o! the bottom edge
3 & #%;
!or i & ()lp
3 & #3 3>%; % de!ine 3 coordinates
'( & linspace('>(i),(,lp); % bisection o! ' coordinates
starting !rom a neJ location
'(i)lp)lp8(lp7()9i) & '(; % de!ine ' coordinates
end

-F
% generate connectivit' arra' 1,2
roJcount & >;
!or elementcount & ()nel
1,2((,elementcount) & elementcount 9 roJcount;
1,2(2,elementcount) & elementcount 9 ( 9 roJcount;
1,2(;,elementcount) & elementcount 9 (lp 9 () 9 roJcount;
1,2(?,elementcount) & elementcount 9 (lp) 9 roJcount;
1! mod(elementcount,lp7() && >
roJcount & roJcount 9 (;
end
end

% plot mesh and natural boundar'
plotmesh,
plot)esh.)
!unction plotmesh;
include!lags;

i! strcmpi(plotmesh,C'esC)&&(;
% plot natural LC
!or i&()nbe

node( & nbc((,i); % !irst node
node2 & nbc(2,i); % second node
3( & 3(node(); '(&'(node(); % coordinates o! the !irst
node
32 & 3(node2); '2&'(node2); % coordinates o! the second
node

plot(#3( 32%,#'( '2%,CrC,C/ineWidthC,?); hold on
end
legend(Cnatural L:C: (!lu3)C);

!or i & ()nel
GG & #3(1,2((,i)) 3(1,2(2,i)) 3(1,2(;,i)) 3(1,2(?,i))
3(1,2((,i))%;
AA & #'(1,2((,i)) '(1,2(2,i)) '(1,2(;,i)) '(1,2(?,i))
'(1,2((,i))%;
plot(GG,AA);hold on;

i! strcmpi(plotnod,C'esC)&&(;
te3t(GG((),AA((),sprint!(C%>:@gC,1,2((,i)));
te3t(GG(2),AA(2),sprint!(C%>:@gC,1,2(2,i)));
te3t(GG(;),AA(;),sprint!(C%>:@gC,1,2(;,i)));
te3t(GG(?),AA(?),sprint!(C%>:@gC,1,2(?,i)));
end
end
end

!print!((,C 0esh "arams HnC);
!print!((,C2o: o! ,lements %d HnC,nel);
!print!((,C2o: o! 2odes %d HnC,nnp);
!print!((,C2o: o! ,-uations %d HnHnC,ne-);
-I

include&fla$s.)
% !ile to include global variables
global ndo! nnp nel nen nsd ne- ngp nee ne-
global nd ebc s " D
global /0 1D 1,2 !lags nbc
global 3 ' nbe
global compute!lu3 plotmesh plottemp plot!lu3 plotnod
#le)ent conductance )atrix and nodal source flux !ector" (eat8elem.m
This function is used for the integration of the ?uadrilateral element conductance matri+
and nodal source *ector using Sauss ?uadrature) The integration is carried o*er the parent
element domain) The shape functions are computed in 7mat(eat8 and their deri*ati*es
along with the :acobian matri+ and its determinant are computed in Bmat(eat8) The
source is obtained b' interpolation from nodal *alues)
heat21ele).)
% Quadrilateral element conductance matri3 and nodal source vector
!unction #*e, !e% & heat2Delem(e)
include!lags;

*e & <eros(nen,nen); % initiali<e element conductance matri3
!e & <eros(nen,(); % initiali<e element nodal source vector

% get coordinates o! element nodes
6e & 1,2(),e);
C & #3(6e); '(6e)%C;

#J,gp% & gauss(ngp); % get Fauss points and Jeights

% compute element conductance matri3 and nodal source vector
!or i&()ngp
!or 6&()ngp
eta & gp(i);
psi & gp(6);

2 & 2matHeat2D(eta,psi); % shape !unctions
matri3
#L, detN% & LmatHeat2D(eta,psi,C); % derivative o!
the shape !unctions

*e & *e 9 J(i)8J(6)8LC8D8L8detN; % element conductance
matri3
se & 28s(),e); % compute s(3)
!e & !e 9 J(i)8J(6)82C8se8detN; % element nodal source
vector

end
end

-&
%)at4eat21.)
% +hape !unction
!unction 2 & 2matHeat2D(eta,psi)

2 & >:2@ 8 #((7psi)8((7eta) ((9psi)8((7eta) ((9psi)8((9eta)
((7psi)8((9eta)%;

/)at4eat21.)
% L matri3 !unction

!unction #L, detN% & LmatHeat2D(eta,psi,C)

% calculate the Frad(2) matri3
F2 & >:2@ 8 #eta7( (7eta (9eta 7eta7(;
psi7( 7psi7( (9psi (7psi%;

N & F28C; % Fet the Nacobian matri3
detN & det(N); % Nacobian
L & NHF2; % compute the L matri3

.oint sources and nodal boundar( flux function src/and/flux.m
This function adds the contribution of point sources $ !prescribed at nodes onl'" and the
boundar' flu+ *ector to the global flu+ *ector) The 68 arra' is used to relate the initial
and reordered node numbering) To calculate the nodal boundar' flu+ *ector, the function
loops o*er all boundar' edges nbe and performs one#dimensional integration using Sauss
?uadrature) The integration is performed b' transforming the boundar' edge to the parent
domain) The boundar' flu+ *ector is then assembled to the global nodal flu+ *ector using
the 68 arra') Cote that
7
G
has a minus sign based on )
src&and&flux.)
% 7 Compute and assemble nodal boundar' !lu3 vector and point sources
!unction ! & srcand!lu3(!);
include!lags;

% assemble point sources to the global !lu3 vector
!(1D) & !(1D) 9 "(1D);

% compute nodal boundar' !lu3 vector
!or i & ()nbe

!- & #> >%C; % initiali<e the nodal
source vector
node( & nbc((,i); % !irst node
node2 & nbc(2,i); % second node
nbce & nbc(;)?,i); % !lu3 values at an edge

3( & 3(node(); '( & '(node(); % coordinates o! the
!irst node
32 & 3(node2); '2 & '(node2); % coordinates o! the
second node
-(

leng & s-rt((3273()K2 9 ('27'()K2); % length o! an
edge
N & lengI2; % (D
Nacobian
#J,gp% & gauss(ngp); % get Fauss points and
Jeights

!or i&()ngp % integrate along the
edge

psi & gp(i);
2 & >:@8#(7psi (9psi%; % (D shape !unctions in
the parent domain
!lu3 & 2 8 nbce; % interpolate !lu3
using shape !unctions
!- & !- 9 J(i)82C 8!lu38N; % nodal !lu3
end
!- & 7!-; % de!ine nodal !lu3 vectors
as negative

% assemble the nodal !lu3 vector
!(1D(node()) & !(1D(node()) 9 !-(() ;
!(1D(node2)) & !(1D(node2)) 9 !-(2);

end

5he postprocessin$" postprocessor.m
The postprocessing is the final phase of the finite element method) The results are plotted
in 1igures &)10#&)12 in Chapter &)
postprocess.)
% plot temperature and !lu3
!unction postprocess(d);
include!lags

% plot the temperature !ield
i! strcmpi(plottemp,C'esC) && (;
d( & d(1D);
!igure(2);
!or e & ()nel
GG & #3(1,2((,e)) 3(1,2(2,e)) 3(1,2(;,e)) 3(1,2(?,e))
3(1,2((,e))%;
AA & #'(1,2((,e)) '(1,2(2,e)) '(1,2(;,e)) '(1,2(?,e))
'(1,2((,e))%;
dd & #d((1,2((,e)) d((1,2(2,e)) d((1,2(;,e)) d((1,2(?,e))
d((1,2((,e))%;
patch(GG,AA,dd);hold on;
end
title(CTemperature distributionC); 3label(CGC); 'label(CAC); colorbar;
end
40

%compute !lu3 vector at Fauss points
i! strcmpi(compute!lu3,C'esC)&&(;
!print!((,CHn Heat Dlu3 at Fauss "oints HnC)

!print!((,C777777777777777777777777777777777777777777777777777777777777
77777777777777777 HnC)
!or e & ()nel
!print!((,C,lement %d HnC,e)
!print!((,C7777777777777HnC)
get!lu3(d,e);
end
end

$et&flux.)
!unction get!lu3(d,e);
include!lags;

de & d(/0(),e)); % e3tract temperature at element nodes

% get coordinates o! element nodes
6e & 1,2(),e);
C & #3(6e); '(6e)%C;

#J,gp% & gauss(ngp); % get Fauss points and Jeights

% compute !lu3 vector
ind & (;
!or i&()ngp
!or 6&()ngp
eta & gp(i); psi & gp(6);

2 & 2matHeat2D(eta,psi);
#L, detN% & LmatHeat2D(eta,psi,C);

G(ind,)) & 28C; % Fauss points in ph'sical
coordinates
-(),ind) & 7D8L8de; % compute !lu3 vector
ind & ind 9 (;
end
end
-3 & -((,));
-' & -(2,));

% R37coord '7coord -3(eta,psi) -'(eta,psi)
!lu3e( & #G(),() G(),2) -3C -'C%;
!print!((,CHtHtHt37coordHtHtHtHt'7coordHtHtHtHt-3HtHtHtHtHt-'HnC);
!print!((,CHtHtHt%!HtHtHt%!HtHtHt%!HtHtHt%!HnC,!lu3e(C);

i! strcmpi(plot!lu3,C'esC)&&( P strcmpi(plotmesh,C'esC) &&(;
!igure(();
-uiver(G(),(),G(),2),-3C,-'C,C*C);
plot(G(),(),G(),2),Cr3C);
title(CHeat Dlu3C);
3label(CGC);
41
'label(CAC);
end

1unctions, which are identical to those in Chapter $
setupV2%VLM)m, assembl')m, sol*edr)m

disp&and&stress.)
% compute stresses and displacements
!unction dispandstress(e,d)
include!lags;

de & d(/0(),e)); % e3tract element nodal
displacements
1,2e & 1,2(),e); % e3tract element
connectivit' in!ormation
3e & 3(1,2e); % e3tract element coordinates
N & (3e(nen) 7 3e(())I2; % Nacobian
#J , gp% & gauss(ngp); % Fauss points and Jeights

% compute stresses at Fauss points
!or i & ()ngp
3t & >:@8(3e(()93e(nen))9N8gp(i); % location o! Fauss point
in the ph'sical coordinates
gausspt(i) & 3t; % store Fauss point
in!ormation

2 & 2matri3(D(3t,3e); % e3tract shape !unctions
L & Lmatri3(D(3t,3e); % e3tract derivative o! shape
!unctions

,e & 28,(1,2e); % AoungCs modulus at element
Fauss points
stressgauss(i) & ,e8L8de; % stresses at Fauss points
end

% print stresses at element Fauss points !print!((,C%dHtHtHt%!HtHtHt
%!HtHtHt%!HtHtHt
%!HnC,e,gausspt((),gausspt(2),stressgauss((),stressgauss(2));

% compute displacements and stresses
3plot & linspace(3e((),3e(nen),nplot); % e-uall' distributed
coordinate Jithin an element
!or i & ()nplot
3i & 3plot(i); % 3 coordinate
2 & 2matri3(D(3i,3e); % shape !unction value
L & Lmatri3(D(3i,3e); % derivative o! shape !unctions

,e & 28,(1,2e); % AoungCs modulus
displacement(i) & 28de; % displacement output
stress(i) & ,e8L8de; % stress output
end

% plot displacements and stress
42
!igure(2)
subplot(2,(,();
plot(3plot,displacement); legend(Csd!C); hold on;
'label(CdisplacementC); title(CD, anal'sis o! (D barC);

subplot(2,(,2); plot(3plot,stress); hold on;
'label(CstressC); 3label(C3C);
legend(CD,C);
#xactSolution.)
% plot the e3act stress
!unction ,3act+olution
include!lags;

% divide the problem domain into tJo regions
3a & 2)>:>()@;
3b & @)>:>()E;

subplot(2,(,();
% e3act displacement !or 3a
c( & M2; c2 & ( 7 (c(I(E)8log(2);
u( & 7:@83a 9 (c(I(E)8log(3a) 9 c2;
% e3act displacement !or 3b
c; & ?5; c? & log(@)I(E8(c(7c;) 9 c2;
u2 & 7:@83b 9 (c;I(E)8log(3b) 9 c?;
% plot displacement
h & plot(#3a 3b%,#u( u2%, C77rC );
legend(h,Ce3actC);

subplot(2,(,2);
% e3act stresses !or 3a
'a & (;E7?83a):I3a;
% e3act stress !or 3b
'b & (2?7?83b):I3b;
% plot stresses
plot(#3a 3b%,#'a 'b%, C77rC );
1unctions pro*ided in Chapter 4 Cmatri+1%)m, Bmatri+1%)m, gauss)m
1unctions pro*ided in Chapter 2 sol*edr)m
12.& 2$ Elasticit' FE (rogram with MATLAB
2n this chapter, we introduce the finite element program for the two#dimensional linear
elasticit' problems) Tnl' 4#node ?uadrilateral element is implemented) 2n Kroblems (#F
and (#I in Chapter (, students are assigned to implement the -#node and F#node
triangular elements)
Main file" elasticity8.m
The main program is gi*en in elasticity8.m file) The 1Q program structure consists of
the following steps
# preprocessing
4-
# e*aluation of element stiffness matrices and element bod' force *ectors and
assembl'
# assembl' of point forces !point forces defined at nodes onl'"
# e*aluation and assembl' of nodal boundar' force *ector
# solution of the algebraic s'stem of e?uations
# postprocessing
elasticit(21.)
%%%%%%%%%%%%%%%%%
% 2D ,lasticit' (Chapter =) %
% Haim Waisman %
%%%%%%%%%%%%%%%% %
clear all;
close all;

% include global variables
include!lags;

% "reprocessing (same as in Chapter 5)
#$,!,d% & preprocessor;

% ,lement computations and assembl'
!or e & ()nel
#*e, !e% & elast2Delem(e);
#$,!% & assembl'($,!,e,*e,!e); % (same as in Chapter 5)
end

% Compute and assemble point !orces and boundar' !orce vector
! & pointandtrac(!);

% +olution (same as in Chapter 5)
#d,r% & solvedr($,!,d);

% "ostprocessing (same as in Chapter 5)
postprocessor(d);
Input file" input/file.m
The data for natural B)C) is gi*en in n/bc arra') 1or e+ample, for the 1F#element mesh in
Q+ample ()- in Chapter (, the n/bc arra' is gi*en as
21 22 2- 24
22 2- 24 2$
0)0 0)0 0)0 0)0
V
20)0 20)0 20)0 20)0
0)0 0)0 0)0 0)0
20)0 20)0 20)0 20)0
n bc
1
1
1
1

1

1
1
1
1
]
The number of columns indicates the number of edges that lie on the natural boundar'
!specified b' nbe") The first and second rows indicate the first and the second node that
44
define of an element edge) The third and fourth rows correspond to the appropriate
traction *alues in + and ' directions at the first node, respecti*el', whereas rows fifth and
si+th correspond to tractions in + and ' directions specified at the second node) 2nput files
for the 1 and F4 element meshes are gi*en on the program website)
Input&file&13ele.)
% 1nput Data !or ,3ample =:; ((E7element mesh)

% material properties
, & ;>eE; % AoungBs modulus
ne & >:;; % "oissonBs ratio
D & ,I((7neK2) 8 # ( ne > % Hoo*eBs laJ 4
"lane stress
ne ( >
> > ((7ne)I2%;

% mesh speci!ications
nsd & 2; % number o! space dimensions
nnp & 2@; % number o! nodal nodes
nel & (E; % number o! elements
nen & ?; % number o! element nodes
ndo! & 2; % degrees7o!7!reedom per node
ne- & nnp8ndo!; % number o! e-uations

! & <eros(ne-,(); % initiali<e nodal !orce vector
d & <eros(ne-,(); % initiali<e nodal displacement matri3
$ & <eros(ne-); % initiali<e sti!!ness matri3

counter & <eros(nnp,(); % counter o! nodes !or the stress plots
nodestress & <eros(nnp,;); % nodal stress values !or plotting #s33
s'' s3'%
!lags & <eros(ne-,(); % an arra' to set L:C !lags
ebc & <eros(ne-,(); % essential L:C arra'
nbc & <eros(ne-,(); % natural L:C arra'

" & <eros(ne-,(); % point !orces applied at nodes
b & <eros(nen8ndo!,nel); % bod' !orce values de!ined
at nodes

ngp & 2; % number o! gauss points in each direction
nd & (>; % number o! do!s on essential boundar' (3
and ')
% essential L:C:
ind( & ()(>)(2(7()8ndo!9(; % all 3 do!s along the line '&>
ind2 & 2)(>)(2(7()8ndo!92; % all ' do!s along the line 3&>
!lags(ind() & 2; ebc(ind() & >:>;
!lags(ind2) & 2; ebc(ind2) & >:>;

% plots
computestress & C'esC;
plotmesh & C'esC; % (same as in Chapter 5)
plotnod & C'esC;
plotdisp & C'esC;
4$
plotstress & C'esC;
plotstress33 & C'esC;
plotmises & C'esC;
!act & =:22(e;; % !actor !or scaled displacements
plot

% natural L:C 7 de!ined on edges
nbc & # 2( 22 2; 2? % node(
22 2; 2? 2@ % node2
> > > > % traction
value given at node( in 3
72> 72> 72> 72> % traction
value given at node( in '
> > > > % traction
value given at node2 in 3
72> 72> 72> 72>%; % traction
value given at node2 in '
nbe & ?; % number
o! edges on natural boundar'

% mesh generation
mesh2d;

include&fla$s.)
% !ile to include global variables
global ndo! nnp nel nen nsd ne- ngp nee ne-
global nd ebc " b D
global /0 1D 1,2 !lags nbc
global 3 ' nbe counter nodestress
global computestress plotmesh plotdisp plotnod
global plotstress33 plotmises !act
%ode 6enu)berin$" setup/68/2M.m
The generation of the 68 arra' is similar to that in heat conduction problem with the
e+ception that nd defines the number of degrees#of#freedom on the essential boundar')
The 2M arra' is a pointer to the renumbered degrees#of#freedom) 1or this purpose, we
treat e*er' node as a bloc0 consisting of two degrees#of#freedom) Le define a pointer,
denoted as bl% to the beginning of each bloc0 and loop o*er all degrees#of#freedom ndof
in that bloc0)
setup&I1&LM.)
!unction d&setup1D/0(d);
include!lags;

count & >; count( & >;
!or i & ()ne-
i! !lags(i) && 2 % chec* i! a node on essential
boundar'
count & count 9 (;
1D(i) & count; % arrange essential L:C nodes !irst
d(count)& ebc(i); % store essential L:C in reordered !orm
(dbar)
4F
else
count( & count( 9 (;
1D(i) & nd 9 count(;
end
end

!or i & ()nel
n & (;
!or 6 & ()nen
bl* & ndo!8(1,2(6,i)7();
!or * & ()ndo!
/0(n,i) & 1D( bl* 9 * ); % create the /0 matri3
n & n 9 (;
end
end
end
#le)ent stiffness and forces function" elast8elem.m
The elast8elem function for numerical integration of the element stiffness matri+ and
element nodal bod' force *ector remains the same as for heat conduction code e+cept for
the shape functions 7mat4last8 and their deri*ati*es Bmat4last8)
elast21ele).)
!unction #*e, !e% & elast2Delem(e)
include!lags;

*e & <eros(nen8ndo!,nen8ndo!); % initiali<e element
sti!!ness matri3
!e & <eros(nen8ndo!,(); % initiali<e element bod'
!orce vector

% get coordinates o! element nodes
6e & 1,2(),e);
C & #3(6e); '(6e)%C;

#J,gp% & gauss(ngp); % get gauss points and Jeights

% compute element sti!!ness and nodal !orce vector
!or i&()ngp
!or 6&()ngp
eta & gp(i);
psi & gp(6);
2 & 2mat,last2D(eta,psi); % shape
!unctions
#L, detN% & Lmat,last2D(eta,psi,C); %
derivative o! the shape !unctions
*e & *e 9 J(i)8J(6)8LC8D8L8detN; % element
sti!!ness matri3
be & 28b(),e); %
interpolate bod' !orces using shape !unctions
!e & !e 9 J(i)8J(6)82C8be8detN; % element bod'
!orce vector

4I
end
end
%)at#las21.)
% +hape !unctions !or 2D elasticit' de!ined in parent element
coordinate s'stem
!unction 2 & 2mat,last2D(eta,psi)

2( & >:2@8((7psi)8((7eta);
22 & >:2@8((9psi)8((7eta);
2; & >:2@8((9psi)8((9eta);
2? & >:2@8((7psi)8((9eta);

2 & #2( > 22 > 2; > 2? >; %
shape !unctions
> 2( > 22 > 2; > 2?%;
/)at#las21.)
% L matri3 !unction !or 2D elasticit'
!unction #L, detN% & Lmat,last2D(eta,psi,C)

% Calculate the Frad(2) matri3
F2 & >:2@ 8 #eta7( (7eta (9eta 7eta7(;
psi7( 7psi7( (9psi (7psi%;
N & F28C; % Fet the Nacobian matri3

detN & det(N); % compute Nacobian

LL & NHF2; % compute derivatives o! the shape
!unction in ph'sical coordinates
L(3 & LL((,();
L23 & LL((,2);
L;3 & LL((,;);
L?3 & LL((,?);
L(' & LL(2,();
L2' & LL(2,2);
L;' & LL(2,;);
L?' & LL(2,?);

L & # L(3 > L23 > L;3 > L?3
> ;
> L(' > L2' > L;' >
L?';
L(' L(3 L2' L23 L;' L;3 L?'
L?3%;
.oint forces and nodal boundar( force !ector function point/and/trac.m
The function loops o*er nbe edges on the essential and performs a one#dimensional
integration using Sauss ?uadrature) The integration is performed b' transforming the
boundar' edge to the parent coordinate s'stem
[0, 1] x
) The nodal boundar' force
4&
*ector is then assembled to the global force *ector using 68 arra') Dimilarl', point forces
defined at the nodes are assembled into the global nodal force *ector using the 68 arra')
point&and&trac.)
% Compute and assemble point !orces and boundar' !orce vector
!unction ! & pointandtrac(!);
include!lags;

% .ssemble point !orces
!(1D) & !(1D) 9 "(1D);

% Calculate the nodal boundar' !orce vector
!or i & ()nbe

!t & #> > > >%C; % initiali<e the nodal
boundar' vector
node( & nbc((,i); % !irst node
node2 & nbc(2,i); % second node
nbce & nbc(;)E,i); % traction values

3( & 3(node(); '(&'(node(); % coordinate o! the !irst node
32 & 3(node2); '2&'(node2); % coordinate o! the second node
leng & s-rt((3273()K2 9 ('27'()K2); % edge length
N & lengI2; % (D
Nacobian
#J,gp% & gauss(ngp); % get gauss
points and Jeights

!or i&()ngp % per!orm(D
numerical integration

psi & gp(i);
2 & >:@8#(7psi > (9psi >; % (D
shape !unctions in the parent edge
> (7psi > (9psi%;
% !or interpolating tractions in 3 and '
T & 2 8 nbce;
!t & !t 9 J(i)82C 8T 8N; % compute
traction
end

% .ssemble nodal boundar' !orce vector
ind( & ndo!8(node(7()9(; % do! corresponding to
the !irst node
ind2 & ndo!8(node27()9(; % do! corresponding to
the second node
!(1D(ind()) & !(1D(ind()) 9 !t(() ;
!(1D(ind(9()) & !(1D(ind(9()) 9 !t(2) ;
!(1D(ind2)) & !(1D(ind2)) 9 !t(;) ;
!(1D(ind29()) & !(1D(ind29()) 9 !t(?);

end

4(
.ostprocessin$" postprocessor.m
The postprocessing function first calls displacements function to plot the deformed
configuration based on the nodal displacements) The user sets a scaling factor in the input
file to scale the deformation as shown in 1igure ()1- in Chapter ()
To obtain the fringe or contour plots of stresses, stresses are computed at element nodes
and then a*eraged o*er elements connected to the node) Alternati*el', stresses can be
computed at the Sauss points where the' are most accurate and then interpolated to the
nodes) The user is often interested not onl' in the indi*idual stress components, but in
some o*erall stress *alue such as 3on#Mises stress) 2n case of plane stress, the *on Mises
stress is gi*en b'
2 2
1 2 1 2
2
1
+ , where W
1
and W
2
are principal stresses gi*en b'
2
2
1,2
2 2
x y x y
xy


+ _
t +

,
) 1igure ()14 in Chapter ( plots the 22
s
stress contours
for the F4#element mesh)
postprocessor.)
% de!ormation and stress ouput
!unction postprocess(d);
include!lags

% plot the de!ormed mesh
displacements(d);

% Compute strains and stresses at gauss points
s & <eros(ne-,();
i! strcmpi(computestress,C'esC)&&(;
!print!((,CHn +tress at Fauss "oints HnC)

!print!((,C777777777777777777777777777777777777777777777777777777777777
77777777777777777 HnC)
!or e&()nel
!print!((,C,lement %d HnC,e)
!print!((,C7777777777777HnC)
getstress(d,e);
nodalstress(d,e);
end
stresscontours;
end


displace)ent.)
% scale and plot the de!ormed con!iguration
!unction displacements(d);
include!lags;


$0
i! strcmpi(plotdisp,C'esC)&&(;
displacement & d(1D)8!act; % scale displacements

% Compute de!ormed coordinates
6 & (;
!or i & ()ndo!)nnp8ndo!
3neJ(6) & 3(6) 9 displacement(i);
'neJ(6) & '(6) 9 displacement(i9();
6 & 6 9 (;
end
% "lot de!ormed con!iguration over the initial con!iguration
!or e & ()nel
GGneJ & #3neJ(1,2((,e)) 3neJ(1,2(2,e)) 3neJ(1,2(;,e))
3neJ(1,2(?,e)) 3neJ(1,2((,e))%;
AAneJ & #'neJ(1,2((,e)) 'neJ(1,2(2,e)) 'neJ(1,2(;,e))
'neJ(1,2(?,e)) 'neJ(1,2((,e))%;
plot(GGneJ,AAneJ,C*C);hold on;
end
title(C1nitial and de!ormed structureC); 3label(CGC); 'label(CAC);
end

$et&stress.)
% Compute strains and stresses at the gauss points
!unction getstress(d,e);
include!lags;

de & d(/0(),e)); % element nodal displacements

% get coordinates o! element nodes
6e & 1,2(),e);
C & #3(6e); '(6e)%C;

#J,gp% & gauss(ngp); % get gauss points and Jeights

% compute strains and stress at gauss points
ind & (;
!or i&()ngp
!or 6&()ngp
eta & gp(i); psi & gp(6);
2 & 2mat,last2D(eta,psi);
#L, detN% & Lmat,last2D(eta,psi,C);

2a & #2((,() 2((,;) 2((,@) 2((,M)%;
G(ind,)) & 2a8C; % gauss points in the
ph'sical coordinates
strain(),ind) & L8de;
stress(),ind) & D8strain(),ind); % compute the stress
#s33 s'' s3'%;
ind & ind 9 (;
end
end
e33 & strain((,)); e'' & strain(2,)); e3' & strain(;,));
% strains at gauss points
$1
s33 & stress((,)); s'' & stress(2,)); s3' & stress(;,));
% stress at gauss points

% "rint 37coord '7coord sigma33 sigma''
sigma3'
stressgauss & #G(),() G(),2) s33C s''C
s3'C %;
!print!((,CHt37coordHtHtHt'7coordHtHtHts33HtHtHts''HtHtHts3'HnC);
!print!((,CHt%!HtHt%!HtHt%!HtHt%!HtHt%!HnC,stressgaussC);

nodal&stress.)
% compute the average nodal stress values
!unction nodalstress(d,e);
include!lags;

de & d(/0(),e)); % displacement at the current element nodes

% get coordinates o! element nodes
6e & 1,2(),e);
C & #3(6e); '(6e)%C;

psival & #7( ( ( 7(%; % psi values at the nodes
etaval & #7( 7( ( (%; % eta values at the nodes

% Compute strains and stress at the element nodes
ind & (;
!or i&()nen
eta & etaval(i);
psi & psival(i);

#L, detN% & Lmat,last2D(eta,psi,C);
strain(),ind) & L8de;
stress(),ind)& D8strain(),ind); % compute the stress #s33
s'' s3'%;
ind & ind 9 (;
end
e33 & strain((,)); e'' & strain(2,)); e3' & strain(;,));
% strains at gauss points
s33 & stress((,)); s'' & stress(2,)); s3' & stress(;,));
% stress at gauss points

counter(6e) & counter(6e) 9 ones(nen,(); % count tnumber o!
elements connected to the node
nodestress(6e,)) & #s33C s''C s3'C %; % accumulate stresses
at the node

Stress&contours.)
!unction stresscontours;
include!lags;

i! strcmpi(plotstress33,C'esC)&&(;
!igure(2);
!or e&()nel
$2
GG & #3(1,2((,e)) 3(1,2(2,e)) 3(1,2(;,e)) 3(1,2(?,e))
3(1,2((,e))%;
AA & #'(1,2((,e)) '(1,2(2,e)) '(1,2(;,e)) '(1,2(?,e))
'(1,2((,e))%;

s33 & nodestress(1,2(),e),():Icounter(1,2(),e));
dd & #s33C s33(()%;
patch(GG,AA,dd);hold on;
end
title(CHsigma33 contoursC); 3label(CGC); 'label(CAC); colorbar
end

i! strcmpi(plotmises,C'esC)&&(;
!or e&()nel
GG & #3(1,2((,e)) 3(1,2(2,e)) 3(1,2(;,e)) 3(1,2(?,e))
3(1,2((,e))%;
AA & #'(1,2((,e)) '(1,2(2,e)) '(1,2(;,e)) '(1,2(?,e))
'(1,2((,e))%;

s33 & nodestress(1,2(),e),():Icounter(1,2(),e));
s'' & nodestress(1,2(),e),2):Icounter(1,2(),e));
s3' & nodestress(1,2(),e),;):Icounter(1,2(),e));

+( & >:@8(s339s'') 9 s-rt( (>:@8(s337s'')):K2 9 s3':K2); %
!irst principal stress
+2 & >:@8(s339s'') 7 s-rt( (>:@8(s337s'')):K2 9 s3':K2); %
second principal stress
mises & s-rt( +(:K2 9 +2:K2 7 +(:8+2 ); %
plane7stress case

dd & #misesC mises(()%;

!igure(;);
patch(GG,AA,dd);hold on;

end
title(CSon 0ises Hsigma contoursC); 3label(CGC); 'label(CAC);
colorbar
end

1unctions, which are identical to those in Chapter &
preprocessor)m, mesh2d)m, plotmesh)m, assembl')m, sol*edr)m

12.& MATLAB finite element program for )eams in 2$
2n this section we describe a finite element program for beams in two#dimensions) The
program structure is *er' similar to that introduced for one#dimensional problems in
Dection 12)- with a main differences being two degrees#of freedom per node) A brief
description of *arious functions is pro*ided below)
bea).)
$-
The main program is gi*en in beam.m file) 2t can be seen that it is almost identical to that
in Dection 12)-)
%%%%%%%%%%%%%%%%%%%%%%
% Bea !"#apter 1$% %
% &uleian M. Bani'ani( )ensselaer %
% )ensselaer *olytec#nic +nstitute %
%%%%%%%%%%%%%%%%%%%%%%
clear all,
close all,

% include global -ariables
include.flags,
% *reprocessing
/0(f(d1 2 preprocessor,
% 3leent atri4 coputations and assebly
for e 2 1:nel
/5e(fe1 2 beaele!e%,
/0( f1 2 assebly!0(f(e(5e(fe%,
end
% 6dd nodal boundary force -ector
f 2 7aturalB"!f%,
% *artition and solution
/d(f.31 2 sol-edr!0(f(d%,

% *ostprocessing
postprocessor!d%
include&fla$s.)
% +nclude global -ariables
global nsd ndof nnp nel nen ne8 nd ngp
global "6rea 3 leng p#i 4p *
global plot.bea plot.nod plot.stress
global 9M +37 4 y stress body
global flags +: 4plot n.bc e.bc np nplot ne8e
preprocessor.)
T#e preprocessor function reads input file and generates +: and 9M arrays. T#e structure of +:
array is identical to t#at for t#e scalar field probles !see for instance progra in "#apter ;%, T#e
9M relates eleents !coluns% to e8uation nubers after renubering. T#e 9M array for
34aple *roble 1$.1 is
1 -
2 4
- $
4 F
2M
1
1
1
1
1
]
.
% reads input data and sets up es# inforation
function /0(f(d1 2 preprocessor,
include.flags,
% input file to include all -ariables
input.file.e4aple1$.1,
% <enerate 9M array
count 2 $, count1 2 $,
$4
for i 2 1:ne8
if flags!i% 22 2 % c#ec5 if essential boundary
count 2 count = 1,
+:!i% 2 count, % nuber first t#e degrees>of>freedo on essential boundary
d!count%2 e.bc!i%, % store t#e reordered -alues of essential B."
else
count1 2 count1 = 1,
+:!i% 2 nd = count1,
end
end
for e 2 1:nel
for ? 2 1:nen
for 2 1:ndof
ind 2 !?>1%@ndof = ,
9M!ind(e% 2 +:!ndof@+37!?(e% > ndof = % , % create t#e 9M atri4
end
end
end
input&file&exa)ple10&1.)
T#e cross>sectional area is prescribed at t#e nodes and interpolated using linear s#ape functions.
T#e AoungBs odulus and body forces are assued to be constant wit#in one eleent, t#ey are
prescribed for eac# eleent. 3ssential and natural boundary conditions are prescribed for eac#
degree of freedo on essential and natural boundary( respecti-ely.
% +nput :ata for 34aple 1$.1
nsd 2 2, % 7uber of spatial diensions
ndof 22, % 7uber of degrees>of>freedo per node
nnp 2 3, % Total nuber of global nodes
nel 2 2, % Total nuber of eleents
nen 2 2, % 7uber of nodes in eac# eleent
ne8 2 ndof@nnp, % 7uber of e8uations
ne8e 2 ndof@nen, % 7uber of e8uations for eac# eleent
f 2 Ceros!ne8(1%, % +nitialiCe force -ector
d 2 Ceros!ne8(1%, % +nitialiCe displaceent -ector
0 2 Ceros!ne8%, % +nitialiCe stiffness atri4
flags 2 Ceros!ne8(1%, % initialiCe flag -ector
e.bc 2 Ceros!ne8(1%, % initialiCe -ector of essential boundary condition
n.bc 2 Ceros!ne8(1%, % initialiCe -ector of natural boundary condition
% 3leent properties
"6rea 2 /1 1 11D, % 3leents cross>sectional area
leng 2 /E 4 1, % 3leents lengt#
body 2 />1 $ 1D, % body forces
3 2 /1e4 1e41D, % AoungBs Modulus
% gauss integration
ngp 2 2, % nuber of gauss points
% essential boundary conditions
% odd nubers for displaceents, e-en for nubers for rotations
flags!1% 2 2, % flags to ar5 degrees>of>freedo located on t#e essential boundary
flags!2% 2 2, % flags to ar5 degrees>of>freedo located on t#e essential boundary
e.bc!1% 2 $, % -alue of prescribed displaceent
e.bc!2% 2 $, % -alue of prescribed rotation
nd 2 2, % nuber of degrees>of>freedo on t#e essential boundary

% natural boundary conditions
% odd nubers for s#ear forces, e-en nubers for oents
$$
flags!;% 2 1, % flags to ar5 degrees>of>freedo located on t#e natural boundary
n.bc!;% 2 >2$, % -alue of force
flags!F% 2 1, % flags to ar5 degrees>of>freedo located on t#e natural boundary
n.bc!F% 2 2$, % -alue of oent
% 6pplied point forces
* 2 />1$ ;1D, % array of point forces
4p 2 /4 E1D , % array of coordinates w#ere point forces are applied
np 2 2 , % nuber of point forces
% output controls
plot.bea 2 DyesD,
plot.nod 2 DyesD,

% es# generation
bea.es#.1$.1,
% nuber of points for plot
nplot23$$,
bea)&)esh&10&1.)
function bea.es#.1$.1
include.flags,
% 7ode: 1 2 3 !origin placed at node 2%
4 2 /$.$ E.$ 12.$ 1, % G coordinate
y 2 /$.$ $.$ $.$ 1, % A coordinate
% connecti-ity array
+37 2 /1 2
2 31,
% plot bea
plotbea,
bea)ele).)
% generate eleent stiffness atri4 and eleent nodal body force -ector
function /5e( fe1 2 beaele!e%
include.flags,
+37e 2 +37!:(e%, % e4tract local connecti-ity inforation
4e 2 4!+37e%, % e4tract 4 coordinates
H 2 !4e!nen% > 4e!1%%I2, % copute Hacobian
/w ( gp1 2 gauss!ngp%, % e4tract <auss points and weig#ts
5e 2 Ceros!ne8e(ne8e%, % initialiCe eleent stiffness atri4
fe 2 Ceros!ne8e(1%, % initialiCe eleent nodal force -ector
for i 2 1:ngp
7 2 7atri4Bea!gp!i%(4e%, % s#ape functions atri4
B 2 Batri4Bea!gp!i%(4e% @1IHJ2, % deri-ati-e of s#ape functions
6e 2 /7!1% 7!3%1@"6rea!+37e%, % calculate cross>sectional area at eleent gauss points
3e 2 3!e%, % e4tract AoungDs odulus
be 2 body!e%, % e4tract body forces
5e 2 5e = w!i%@!BD@6e@3e@B%, % calculate eleent stiffness atri4
fe 2 fe = w!i%@7D@be, % calculate eleent nodal force -ector
end
5e 2 H@5e,
fe 2 H@fe,
% c#ec5 for point forces in t#is eleent
$F
for i21:np % loop o-er all point forces
*i 2 *!i%, % e4tract point force
4pi 2 4p!i%, % e4tract t#e location of point force wit#in an eleent
if 4e!1%K24pi L 4piK4e!nen%
fe 2 fe = *i@/7atri4Bea! ! !2@4pi>4e!1%>4e!nen%%I!4e!nen% > 4e!1%% % (4e%1D,
end
end
%)atrix/ea).)
% &#ape functions in t#e natural coordinate s
function 7 2 7atri4Bea!s(4e%
924e!2%>4e!1%,
7!1%21I4@!1>s%J2@!2=s%,
7!2%29IE@!1>s%J2@!1=s%,
7!3%21I4@!1=s%J2@!2>s%,
7!4%29IE@!1=s%J2@!s>1%,
/)atrix/ea).)
% :eri-ati-e of t#e s#ape functions in t#e natural coordinate s
function B 2 Batri4Bea!s(4e%
924e!2%>4e!1%,
B!1%23I2@s,
B!2%29@!3I4@s>1I4%,
B!3%2>3I2@s,
B!4%2 9@!3I4@s=1I4%,
S)atrix/ea).)
% &econd deri-ati-e of t#e s#ape functions
function & 2 &atri4Bea!s(4e%
924e!2%>4e!1%,
&!1%23I2,
&!2%23I4@9,
&!3%2>3I2,
&!4%2 3I4@9,
natural/C.)
% copute and asseble nodal boundary force -ector
function f 2 naturalB"!f%,
include.flags,
for i 2 1:ne8
if flags!i% 22 1
dof 2 +:!i%,
f!dof% 2 f!dof% = n.bc!dof%,
end
end
postprocessor.)
% postprocessing function
function postprocessor!d%
include.flags,
% loop o-er eleents to plot displaceents( oents and s#ear forces
for e 2 1:nel

de 2 d!9M!:(e%%, % e4tract eleent nodal displaceents
+37e 2 +37!:(e%, % e4tract eleent connecti-ity inforation
4e 2 4!+37e%, % e4tract eleent coordinates
$I
H 2 !4e!nen% > 4e!1%%I2, % Hacobian
/w ( gp1 2 gauss!ngp%, % e4tract <auss points and weig#ts

% copute displaceents( oents and s#ear forces
4plot 2 linspace!4e!1%(4e!nen%(nplot%, % e8ually distributed coordinate wit#in an eleent
4plotgauss2 !2@4plot>4e!1%>4e!nen%%I!4e!nen% > 4e!1%%,
for i 2 1:nplot
4i 2 4plotgauss!i%, % current coordinate
7 2 7atri4Bea!4i(4e%, % s#ape functions
B 2 Batri4Bea!4i(4e%@1IHJ2, % first deri-ati-e of s#ape functions
& 2 &atri4Bea!4i(4e%@1IHJ3, % second deri-ati-e of s#ape functions
3e 2 3!e%, % AoungDs odulus
displaceent!i% 2 7@de , % displaceent output
oent!i% 2 3e@B@de, % oent output
s#ear!i% 2 3e@&@de, % &#ear force output
end

% plot displaceents( oent and s#ear forces
/4.plot(&.e4(M.e4(w.e412e4act, % call e4act bea solution
figure!2%
plot!4plot(displaceent(D>.rD%, #old on,
plot!4.plot(w.e4(D>5D%, legend!DF3D(D34act &olutionD%, #old on,
ylabel!DdisplaceentD%, title!D:isplaceents: F3 -ersus analytical bea solutionsD%,

figure!3%
plot!4plot(oent(D>.rD%, #old on,
plot!4.plot(M.e4(D>5D%, legend!DF3D(D34act &olutionD%, #old on,
ylabel!DoentD%, 4label!D4D%, title!DMoents: F3 -ersus analytical bea solutionsD%,
figure!4%
plot!4plot(s#ear(D>.rD%, #old on,
plot!4.plot(&.e4(D>5D%, legend!DF3D(D34act &olutionD%, #old on,
ylabel!Ds#earD%, 4label!D4D%, title!D&#ear: F3 -ersus analytical bea solutionsD%,

end
Functions w#ic# are identical to t#ose in "#apter ; are: assebly.( sol-edr.( gauss.
(ro)lems *Linear Alge)ra
*roble 1>1
Write a M6T96B progra to generate a set of linear algebraic e8uations Ax2b w#ere
A is an
n n
atri4 gi-en by
2
0
ij
31 if i j 1 or i j 1
0 if i j
ot(erwise
+


'

and copute
1
A
. T#en c#ec5 #ow closely
1
/ A A
corresponds to I . :o t#is for
n 9,1*,1*** and a larger -alue you c#oose. T#e accuracy of t#e coputed product
can be copared to t#e correct results by coputing a nor of t#e error gi-en by
$&
( )
2
2
1 1
1
n n
ij ij
i j
err B 6
n

)epeat t#e abo-e wit# t#e atri4 A defined by


1
ij
0
i j

+
)epeat t#e abo-e wit# n #,&,9,:,;... . &top w#en t#e error is greater t#an one( since
t#e solution is t#en eaningless.
T#e top e8uation is of t#e for we will see in finite eleent e8uations. T#ey can be
accurately sol-ed e-en for -ery large syste for t#ey are not susceptible to roundoff
error. T#ey are 5nown as well>conditioned. T#e second atri4 is called a 'ilbert atri4.
+t is an e4aple of an estreely ill>conditioned atri4.
*roble 1>2
"onsider a syste of linear e8uations
1 2 3
1 2 3
1 2 3
8 6 7.5
3 5 7 4
4 9 2 12
2 2 2
2 2 2
2 2 2

+ + =

+ + =

+ + =

a% Write t#e syste of e8uations in atri4 notation Ax b = and sol-e for t#e un5nown
x using M6T96B
b% &uppose we ipose an additional constraint on t#e solution
( )
1 2
1 0 g 2 2 2 = + + - = x . Msing M6T96B find a new -ector
ne:
x so t#at it will
satisfy e4actly t#e constraint e8uation ( )
ne:
g x and will iniiCe t#e error
( ) ( ) ( )
T
ne: ne: ne:
e"" = - - x Ax b Ax b
*roble 1>3: "onsider t#e following syetri4 atrices K:
1 1
1 2 2
1 1 2 2
2 2
2 2
0
0
; ; ;
0
0
3 3
3 3 3 3
3 3 3 3
3 3 3
3 3

-

+ -


- + -

-




-


w#ere
1 2
, , 3 3 3 are positi-e constants.
a% "#ec5 if t#e abo-e t#ree atrices are positi-e definite. )ecall t#at if for any -ector
0 x we #a-e
0
T
> x Kx
t#en atri4 K is called &yetric *ositi-e :efinite
!&*:%. +f( on t#e ot#er #and( 0
T
x Kx for any -ector 0 x t#en t#e atri4 K is
syetric sei>positi-e definite. "#oose one of t#e sei>positi-e definite atrices
s#own abo-e and s#ow t#at for any rig#t #and side -ector( f ( t#e syste of
e8uations = Kd f #as no uni8ue solution.
b% Nerify your results by coputing t#e eigen-alues for t#e abo-e t#ree atrices
$(

You might also like