Finite Element Programming With MATLAB
Finite Element Programming With MATLAB
Chapter
1. the data structure of the finite element program will be periodically updated to
reflect emerging finite element technologies and MATLAB syntax changes;
2. to allow the course instructors to use their own MALAB or other finite element
codes.
3. to create a forum where students and instructors would exchange ideas and place
alternative finite element program data structures. The forum is hosted at
http://1coursefem.blogspot.com/
1
May not be covered in the class. Recommended as independent reading.
Figure 12.1: Matlab Windows
2
12.1.5 Functions
MATLAB has many standard mathematical functions such as sine (sin(x)) and cosine
(cos(x)) etc. It also has software packages, called toolboxes, with specialized functions
for specific topics.
• Type “help” in the command line: MATLAB returns a list of topics for which it has
functions. At the bottom of the list it tells you how to get more information about a
topic. As an example, if you type “help sqrt” and MATLAB will return a list of
functions available for the square root.
Bold letters will denote matrices or vectors. The elements of a matrix a are denoted
by aij , where i is the row number and j is the column number. Note that in both
describing the dimension of the matrix and in the subscripts identifying the row and
column number, the row number is always placed first.
An example of a 3x3 matrix is:
3
⎡ 1 2 3⎤
⎢ ⎥
⎢ ⎥
a = ⎢ 4 5 6⎥
⎢ ⎥
⎢ 7 8 0⎥
⎣⎢ ⎥⎦
The above matrix a is is an example of a square matrix since the number of rows and
columns are equal.
The following commands show how to enter matrices in MATLAB (>> is the
MATLAB prompt; it may be different with different computers or different versions of
MATLAB.)
>> a = [1 2 3; 4 5 6; 7 8 0]
a=
1 2 3
4 5 6
7 8 0
Notice that rows of a matrix are separated by semicolons, while the entries on a row are
separated by spaces (or commas). The order of matrix a can be determined from
size(a )
The transpose of any matrix is obtained by interchanging rows and columns. So for
example, the transpose of a is:
⎡1 4 7 ⎤
⎢ ⎥
T ⎢ ⎥
a = ⎢2 5 8 ⎥
⎢ ⎥
⎢ 3 6 0⎥
⎣⎢ ⎥⎦
⎡b ⎤
⎢ 1⎥
⎢ ⎥
b = ⎢b2 ⎥
⎢ ⎥
⎢b3 ⎥
⎣⎢ ⎦⎥
In MATLAB, single subscript matrices are considered row matrices, or row vectors.
Therefore, a column vector in MATLAB is defined by
4
>> b = [1 2 3]'
b=
1
2
3
Note the transpose that is used to define b as a column matrix. The components of the
vector b are b1, b2 , b3 . The transpose of b is a row vector
bT = ⎡⎢b1 b2 b3 ⎤⎥
⎣ ⎦
or in MATLAB
>> b = [1 2 3]
b=
1 2 3
A matrix is called a diagonal matrix if only the diagonal components are nonzero,
i.e., aij = 0, i ≠ j . For example, the matrix below is a diagonal matrix:
⎡1 0 0 ⎤
a = ⎢⎢0 5 0 ⎥⎥
⎢⎣0 0 6 ⎥⎦
A diagonal matrix where all diagonal components are equal to one is called an identity or
unit matrix and is denoted by I. For example, 2 × 2 identity matrix is given by
⎡ 1 0⎤
I = ⎢⎢ ⎥
⎥
0 1
⎣⎢ ⎥⎦
5
The MATLAB expression for an order n unit matrix is
eye(n )
A matrix in which all components are zero is called a zero matrix and is denoted by 0. In
MATLAB, B = zeros (m, n) creates m ×n matrix B of zeros. A random m ×n matrix can
be created by rand (m,n).
In finite element method, matrices are often sparse, i.e., they contain many zeros.
MATLAB has the ability to store and manipulate sparse matrices, which greatly increases
its usefulness for realistic problems. The command sparse (m, n) stores an m × n zero
matrix in a sparse format, in which only the nonzero entries and their locations are sorted.
The nonzero entries can then be entered one-by-one or in a loop.
Notice that the display in any MATLAB statement can be suppressed by ending the line
with a semicolon.
a −1a = aa −1 = I
if the matrix a is not singular. The MATLAB expression for the inverse is inv(a ) . Linear
algebraic equations can also be solved by using backslash operator as shown in Section
1.3.10, which avoids computations of the inverse and is therefore faster.
The matrix a is nonsingular if its determinant, denoted by det (a ) , is not equal to
zero. A determinant of a 2x2 matrix is defined by
6
⎡a11 a12 ⎤
a = ⎢a ⎥ det (a ) = a11a22 − a12a21
⎢⎣ 22 a22 ⎥⎦
det(a )
For example,
>> a = [1 3; 4 2];
>> det (a)
ans =
-10
7
Multiplication
⎡ k k k ⎤
⎢ ∑ a1 jbj 1 ∑a b ∑a b ⎥
⎢ j =1 1j j 2 1 j jn
⎥
⎢ j =1 j =1
⎥
⎢ k k ⎥
⎢ ∑ a2 jbj 1 ∑a b ⎥
c = ab = ⎢⎢ j =1 ⎥
2j j2
j =1 ⎥
⎢ ⎥
⎢ ⎥
⎢ k ⎥
⎢ k ⎥
⎢ ∑ amjbj 1 ∑ amjbjn ⎥
⎢ j =1 ⎥
⎣ j =1 ⎦
8
Note the the i,j entry 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 only if the number of columns in
a equals the number of rows in a. In other words, if a is an (m × k ) matrix, then
b must be an (k × n ) matrix, where k is arbitrary. The product c will then have
the same number of rows as a and the same number of columns as b, i.e. it will be
an m × n matrix.
c = a *b
>> a*c
ans =
22 28
49 64
76 100
>> c*c
??? Error using ==> *
Inner matrix dimensions must agree.
9
⎧
⎪K11d1 + K 2d2 + + K1ndn = f1
⎪
⎪
⎪
⎪K d + K 22d2 + + K 2ndn = f2
⎪ 21 1
⎨
⎪
⎪
⎪
⎪
⎪
⎪K d + K n 2d2 + + K nndn = fn
⎩ n1 1
Kd = f
where
⎡ K11 K12 K1n ⎤ ⎡ f1 ⎤ ⎡d1 ⎤
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢K ⎥ ⎢f ⎥ ⎢d ⎥
⎢ 21 K 22 ⎥ ⎢ 2⎥ ⎢ 2⎥
K=⎢ ⎥ f=⎢ ⎥ d=⎢ ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢K n 1 K nn ⎥⎥ ⎢ fn ⎥ ⎢dn ⎥
⎣⎢ ⎦ ⎣⎢ ⎦⎥ ⎣⎢ ⎦⎥
The symbolic solution of the above system of equation can be found by multiplying both
sides with inverse of K, which yields
d = K−1f
d =K \f
or
d = inv (K ) * f
10
>> A = rand (3,3)
A=
0.2190 0.6793 0.5194
0.0470 0.9347 0.8310
0.6789 0.3835 0.0346
>> b = rand (3,1)
b=
0.0535
0.5297
0.6711
>> x = A\b
x=
-159.3380
314.8625
-344.5078
As mentioned before, the backslash provides a faster way to solve equations and should
always be used for large systems. The reason for this is that the backslash uses
elimination to solve with one right hand side, whereas determining the inverse of an nxn
matrix involves solving the system with n right hand sides. Therefore, the backslash
should always be used for solving large system of equations.
MATLAB variables can also be defined as string variables. A string character is a text
surrounded by single quotes. For example:
It is also possible to create a list of strings by creating a matrix in which each row is a
separate string. As with all standard matrices, the rows must be of the same length. Thus:
11
>> str_mat = ['string A' ; 'string B']
str_mat =
string A
string B
Strings are used for defining file names, plot titles, and data formats. Special built-in
string manipulation functions are available in MATLAB that allow you to work with
strings. In the MATALB codes provided in the book we make use of strings to compare
functions. For example the function strcmpi compares two strings
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. For example
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 value of 10
and the \n defines a new line. To get a complete description type
12
12.1.11.1 Conditional and Loops
MATLAB has a standard if-elseif-else conditional.
The general form An example
>> t = 0.76;
if expression1
>> if t > 0.75
statements1
s = 0;
elseif expression2
elseif t < 0.25
statements2
s = 1;
…
else
…
s = 1-2*(t-0.25);
…
end
else
>> s
statements
s=
end
0
12.1.11.2 Functions
Functions 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:
The rest of the m-file consists of ordinary MATLAB commands computing the values of
the outputs and performing other desired actions. Below is a simple example of a
function that computes the quadratic function f (x ) = x 2 − 3x − 1 . 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 extension)
13
function y = fcn( x )
y=x^2-3*x-1;
Then type command:
>> fcn(0.1)
ans =
-1.2900
Various line types, plot symbols and colors may be obtained with plot(x,y,s) where s is a
character string consisting of elements from any combination of the following 3 columns:
14
To add a title, x and y labels, or a grid, the user should use the following MATLAB
functions. Note that the arguments to the functions are strings
>> title('circle');
>> xlabel('x');
>> ylabel('y');
>> grid
In the MATLAB Finite Element code provided in the book, we also use two specialized
plots. The first plot is the patch function. This function is used to visualize 2D polygons
with colors. The colors are interpolated from nodes of the polygon to create a colored
surface. The following example generates a filled square. The colors along the x axis are
the same while the colors along the y axis are interpolated between the values [0,1].
>> x = [0 1 1 0];
>> y = [0 0 1 1];
>> c = [0 0 1 1];
>> patch(x,y,c)
We will use the patch function to visualize temperatures, stresses and other variables
obtained at the finite element solutions. Another specialized plot function is the quiver.
This function is used to visualize gradients of functions as an arrow plot. The following
15
example demonstrates the use of quiver function for plotting the gradients to the function
y=x2
The hold on command is used to hold the current plot and all axis properties so that
subsequent graphing commands will executed on the existing graph.
Using the text function, the user can add to a plot a text message. For example
text(1,1,'flux')
The first and second arguments define the position of the text on the plot, while the
string gives the text.
12.1.13 Remarks
a) In practice the number of equations n can be very large. PCs can today solve
thousands of equations in a matter of minutes if they are sparse (as they are in
FEM analysis-you will learn about this later) but sometimes millions of
equations are needed, as for an aircraft carrier or a full model of an aircraft;
parallel computers are then needed.
b) Efficient solution techniques that take advantage of the sparsity and other
advantageous properties of FEM equations are essential for treating even
16
moderately large systems. The issue of how to efficiently solve large systems
will not be considered in this course.
c) In this course, we will see that
• The matrix corresponding to the system of equations arising from
FEM (denoted as K) is non-singular (often called regular), i.e.,
K−1 exists if the correct boundary conditions are prescribed and the
elements are properly formulated. Furthermore, for good models it is
usually well-conditioned, which means it is not very sensitive to
roundoff errors.
• K is symmetric, i.e. KT = K .
• K is positive definite, i.e., xT Kx > 0 ∀x (meaning for any value of x)
Alternatively, K is said to be positive definite if all the eigenvalues are
strictly positive. The eigenvalue problem consists of finding nonzero
eigenvectors y and the corresponding eigenvalues λ satisfying
Ky = λ y
17
1. Preprocessing including input data and assembling the proper arrays, vectors,
and matrices.
2. Calculation of element stiffness matrices and force vectors
3. Direct assembly of matrices and vectors
4. Partition and solution
5. Postprocessing for secondary variables
Explanation for various MATLAB routines (stored in *.m files) are described as
comments within each subroutine.
18
d: global displacement vector is stored as:
When ndof = 1 (see example in Figure 2.8) IEN and LM are defined as follows:
⎡e =1 e =2⎤ ⎡e =1 e =2⎤
⎢1 2⎥ ⎢1 2⎥
⎢ ⎥ = IEN ⎢ ⎥ = LM
⎢2 3 ⎥⎥ ⎢2 3 ⎥⎥
⎢⎣ ⎦ ⎢⎣ ⎦
When ndof = 2 (example Problem 2.2), IEN and LM are defined as:
19
⎡1 3⎤
⎢ ⎥
⎡e =1 e =2⎤ ⎢2 4⎥⎥
⎢1 2⎥ ⎢
⎢ ⎥ = IEN ⎢ ⎥ = LM
⎢3 3 ⎥⎥ ⎢5 5⎥
⎢⎣ ⎦ ⎢ ⎥
⎢6 6⎥
⎣ ⎦
In both examples, columns indicate the elements and rows indicate global degrees-of-
freedom.
Remark: In this chapter nodes where the displacements are prescribed have to be
numbered first.
truss.m
%%%%%%%%%%%%%%%%%%%%%%
% 2D Truss (Chapter 2) %
% Haim Waisman, Rensselaer %
%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;
% Preprocessor Phase
[K,f,d] = preprocessor;
20
% Solution Phase
[d,f_E] = solvedr(K,f,d);
% Postprocessor Phase
postprocessor(d)
include_flags.m
% file to include global variables
global nsd ndof nnp nel nen neq nd
global CArea E leng phi
global plot_truss plot_nod plot_stress
global LM IEN x y stress
preprocessor.m
% preprocessing– read input data and set up mesh information
function [K,f,d] = preprocessor;
include_flags;
% generate LM array
for e = 1:nel
for j = 1:nen
for m = 1:ndof
ind = (j-1)*ndof + m;
LM(ind,e) = ndof*IEN(j,e) - ndof + m;
end
end
end
input_file_example2_2.m
% Input Data for Example 2.2
nsd = 2; % Number of space dimensions
ndof = 2; % Number of degrees-of-freedom per node
nnp = 3; % Number of nodal points
nel = 2; % Number of elements
nen = 2; % Number of element nodes
% Element properties
CArea = [1 1 ]; % Elements area
21
leng = [1 sqrt(2)]; % Elements length
phi = [90 45 ]; % Angle
E = [1 1 ]; % Young’s Modulus
% prescribed displacements
% displacement d1x d1y d2x d2y
d = [0 0 0 0]';
nd = 4; % Number of prescribed displacement degrees-of-freedom
% prescribed forces
f(5) = 10; % Force at node 3 in the x-direction
f(6) = 0; % Force at node 3 in the y-direction
% output plots
plot_truss = 'yes';
plot_nod = 'yes';
% mesh Generation
truss_mesh_2_2;
truss_mesh_2_2.m
% geometry and connectivity for example 2.2
function truss_mesh_2_2
include_flags;
% connectivity array
IEN = [1 2
3 3];
% plot truss
plottruss;
input_file_example2_8.m
% Input Data from Chapter 2 Figure 2.8
nsd = 1; % Number of spatial dimensions
ndof = 1; % Number of degrees-of-freedom per node
nnp = 3; % Total number of global nodes
nel = 2; % Total number of elements
nen = 2; % Number of nodes in each element
22
% Element properties
CArea = [.5 1]; % Elements cross-sectional area
leng = [2 2]; % Elements length
E = [1 1]; % Young’s Modulus
% prescribed displacements
d(1) = 0;
nd = 1; % Number of prescribed displacement degrees of freedom
% prescribed forces
f(3) = 10; % force at node 3 in the x-direction
% output controls
plot_truss = 'yes';
plot_nod = 'yes';
% mesh generation
truss_mesh_2_8;
truss_mesh_2_8.m
% geometry and connectivity for example problem in Figure 2.8
function truss_mesh_2_8;
include_flags;
% connectivity array
IEN = [1 2
2 3];
% plot truss
plottruss;
Plottruss.m
% function to plot the elements, global node numbers and print mesh parameters
function plottruss;
include_flags;
23
% check if node numbering is requested
if strcmpi(plot_nod,'yes')==1;
text(XX(1),YY(1),sprintf('%0.5g',IEN(1,i)));
text(XX(2),YY(2),sprintf('%0.5g',IEN(2,i)));
end
end
title('Truss Plot');
end
trusselem.m
% generate the element stiffness matrix for each element
function ke = trusselem(e)
include_flags;
if ndof == 1
ke = const * [1 -1 ; % 1-D stiffness
-1 1];
elseif ndof == 2
p = phi(e)*pi/180; % Converts degrees to radians
s = sin(p); c = cos(p);
s2 = s^2; c2 = c^2;
end
assembly.m
% assemble element stiffness matrix
function K = assembly(K,e,ke)
include_flags;
24
solvedr.m
% partition and solve the system of equations
function [d,f_E] = solvedr(K,f,d)
include_flags;
postprocessor.m
% postprocessing function
function postprocesser(d)
include_flags;
fprintf(1,'%d\t\t\t%f\n',e,stress(e));
25
end
Nmatrix1D.m
% shape functions computed in the physical coordinate - xt
function N = Nmatrix1D(xt,xe)
include_flags;
Bmatrix1D.m
% derivative of the shape functions computed in the physical coordinate - xt
function B = Bmatrix1D(xt,xe)
include_flags;
26
elseif nen == 3 % derivative of quadratic shape functions
B(1)=(2*xt-xe(2)-xe(3))/((xe(1)-xe(2))*(xe(1)-xe(3)));
B(2)=(2*xt-xe(1)-xe(3))/((xe(2)-xe(1))*(xe(2)-xe(3)));
B(3)=(2*xt-xe(1)-xe(2))/((xe(3)-xe(1))*(xe(3)-xe(2)));
end
gauss.m
% get gauss points in the parent element domain [-1, 1] and the corresponding weights
function [w,gp] = gauss(ngp)
if ngp == 1
gp = 0;
w = 2;
elseif ngp == 2
gp = [-0.57735027, 0.57735027];
w = [1, 1];
elseif ngp == 3
gp = [-0.7745966692, 0.7745966692, 0.0];
w = [0.5555555556, 0.5555555556, 0.8888888889];
end
Explanation for various MATLAB routines is given as comments within each function.
27
Only the nomenclature and definitions which have been modified from the previous
chapters are included below. Much of the code is either identical or very similar to the
code developed in Section 12.2. An input file for the Example 5.2 in Chapter 5 modeled
with two quadratic elements is given below. Additional input files for one quadratic
element mesh and four quadratic elements mesh are provided in the disk.
Initial global node number = IEN (local node number, element number )
Calculated by FE program:
ID: Destination array
28
Reordered global node number = LM (Local node number, element number )
LM (I , e) = ID(IEN (I , e))
bar1D.m
%%%%%%%%%%%%%%%%%%
% 1D FEM Program (Chapter 5) %
% Haim Waisman, Rensselaer %
%%%%%%%%%%%%%%%%%%
clear all;
close all;
% Preprocessing
[K,f,d] = preprocessor;
% Postprocessing
postprocessor(d);
include_flags.m
% Include global variables
global nsd ndof nnp nel nen neq nd CArea E
global flags ID IEN LM body x y
global xp P ngp xplot n_bc e_bc np
global plot_bar plot_nod nplot
preprocessor.m
29
% preprocessing– reads input data and sets up mesh information
function [K,f,d] = preprocessor;
include_flags;
input_file5_2_2ele.m
% Input Data for Example 5.2 (2 elements)
% gauss integration
ngp = 2; % number of gauss points
% point forces
P = 24; % array of point forces
xp = 5; % array of coordinates where point forces are applied
30
np = 1; % number of point forces
% output plots
plot_bar = 'yes';
plot_nod = 'yes';
nplot = nnp*10; % number of points in the element to plot displacements and stresses
% mesh generation
bar_mesh5_2_2ele;
bar_mesh5_2_2ele.m
function bar_mesh5_2_2ele
include_flags;
% Node: 1 2 3 4 5
x = [2.0 3.5 5.0 5.5 6.0 ]; % x coordinate
y = 2*x; % y is used only for the bar plot
% connectivity array
IEN = [1 3
2 4
3 5];
plotbar;
setup_ID_LM.m
% setup ID and LM arrays
function d = setup_ID_LM(d);
include_flags;
count = 0; count1 = 0;
for i = 1:neq
if flags(i) == 2 % check if essential boundary
count = count + 1;
ID(i) = count; % number first the nodes on essential boundary
d(count)= e_bc(i); % store the reordered values of essential B.C
else
count1 = count1 + 1;
ID(i) = nd + count1;
end
end
for i = 1:nel
for j = 1:nen
LM(j,i)=ID(IEN(j,i)); % create the LM matrix
end
end
31
barelem.m
% generate element stiffness matrix and element nodal body force vector
function [ke, fe] = barelem(e);
include_flags;
for i = 1:ngp
xt = 0.5*(xe(1)+xe(nen))+J*gp(i); % Compute Gauss points in physical coordinates
assembly.m
% assemble element stiffness matrix and nodal force vector
function [K,f] = assembly(K,f,e,ke,fe)
include_flags;
32
naturalBC.m
% compute and assemble nodal boundary force vector
function f = naturalBC(f);
include_flags;
for i = 1:nnp
if flags(i) == 1
node = ID(i);
f(node) = f(node) + CArea(node)*n_bc(node);
end
end
postprocessor.m
% postprocessing
function postprocessor(d)
include_flags;
33
heat2d.m
%%%%%%%%%%%%%%%%%%%%%
% Heat conduction in 2D (Chapter 8) %
% Haim Waisman, Rensselaer %
%%%%%%%%%%%%%%%%%%%% %
clear all;
close all;
% Preprocessing
[K,f,d] = preprocessor;
% Compute and assemble nodal boundary flux vector and point sources
f = src_and_flux(f);
% Solution
[d,f_E] = solvedr(K,f,d);
% Postprocessing
postprocessor(d);
Preprocessing: preprocessor.m
In the preprocessing phase, the input file (input_file), which defines material properties,
mesh data, vector and matrices initializations, essential and natural conditions, point
sources, the required output, is defined by the user. In the implementation provided here,
fluxes are prescribed along element edges and are defined by nodal values interpolated
using shape functions. The n_bc array is used for the fluxes data structure. For the heat
conduction problem given in Example 8.1 with 16 quadrilateral elements (see Figure
8.9), the n_bc array is defined as follows:
⎡ 21 22 23 24 ⎤
⎢ 22 23 24 25 ⎥⎥
n _ bc = ⎢
⎢ 20.0 20.0 20.0 20.0 ⎥
⎢ ⎥
⎣ 20.0 20.0 20.0 20.0 ⎦
The number of columns corresponds to the number of edges (specified by nbe) on the
natural boundary; 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
34
respective nodal flux values. Note that a discontinuity in fluxes at the element boundaries
could be prescribed. The input files for the 1-element and 64-element meshes are given
on the website.
In the preprocessing phase, the finite mesh is generated and the working arrays IEN, ID
and LM are defined. The mesh generation function mesh2d utilizes MATLAB’s built-in
function linspace (see Chapter 1) for bisection of lines.
preprocessor.m
function [K,f,d] = preprocessor;
include_flags;
input_file_16ele.m
% Input file for Example 8.1 (16-element mesh)
% material properties
k = 5; % thermal conductivity
D = k*eye(2); % conductivity matrix
% mesh specifications
nsd = 2; % number of space dimensions
nnp = 25; % number of nodes
nel = 16; % number of elements
nen = 4; % number of element nodes
ndof = 1; % number of degrees-of-freedom per node
neq = nnp*ndof; % number of equations
% essential B.C.
flags(1:5) = 2; e_bc(1:5) = 0.0;
35
flags(6:5:21) = 2; e_bc(6:5:21) = 0.0;
nd = 9; % number of nodes on essential boundary
% what to plot
compute_flux = 'yes';
plot_mesh = 'yes';
plot_nod = 'yes';
plot_temp = 'yes';
plot_flux = 'yes';
% mesh generation
mesh2d;
mesh2d.m
function mesh2d;
include_flags;
plotmesh.m
function plotmesh;
include_flags;
36
if strcmpi(plot_mesh,'yes')==1;
% plot natural BC
for i=1:nbe
for i = 1:nel
XX = [x(IEN(1,i)) x(IEN(2,i)) x(IEN(3,i)) x(IEN(4,i)) x(IEN(1,i))];
YY = [y(IEN(1,i)) y(IEN(2,i)) y(IEN(3,i)) y(IEN(4,i)) y(IEN(1,i))];
plot(XX,YY);hold on;
if strcmpi(plot_nod,'yes')==1;
text(XX(1),YY(1),sprintf('%0.5g',IEN(1,i)));
text(XX(2),YY(2),sprintf('%0.5g',IEN(2,i)));
text(XX(3),YY(3),sprintf('%0.5g',IEN(3,i)));
text(XX(4),YY(4),sprintf('%0.5g',IEN(4,i)));
end
end
end
include_flags.m
% file to include global variables
global ndof nnp nel nen nsd neq ngp nee neq
global nd e_bc s P D
global LM ID IEN flags n_bc
global x y nbe
global compute_flux plot_mesh plot_temp plot_flux plot_nod
37
heat2Delem.m
% Quadrilateral element conductance matrix and nodal source vector
function [ke, fe] = heat2Delem(e)
include_flags;
end
end
NmatHeat2D.m
% Shape function
function N = NmatHeat2D(eta,psi)
BmatHeat2D.m
% B matrix function
38
Point sources and nodal boundary flux function: src_and_flux.m
This function adds the contribution of point sources P (prescribed at nodes only) and the
boundary flux vector to the global flux vector. The ID array is used to relate the initial
and reordered node numbering. To calculate the nodal boundary flux vector, the function
loops over all boundary edges nbe and performs one-dimensional integration using Gauss
quadrature. The integration is performed by transforming the boundary edge to the parent
domain. The boundary flux vector is then assembled to the global nodal flux vector using
the ID array. Note that fΓ has a minus sign based on
Error! Reference source not found..
src_and_flux.m
% - Compute and assemble nodal boundary flux vector and point sources
function f = src_and_flux(f);
include_flags;
psi = gp(i);
N = 0.5*[1-psi 1+psi]; % 1D shape functions in the parent domain
flux = N * n_bce; % interpolate flux using shape functions
fq = fq + w(i)*N' *flux*J; % nodal flux
end
fq = -fq; % define nodal flux vectors as negative
end
39
The postprocessing is the final phase of the finite element method. The results are plotted
in Figures 8.10-8.12 in Chapter 8.
postprocess.m
% plot temperature and flux
function postprocess(d);
include_flags
get_flux.m
function get_flux(d,e);
include_flags;
40
N = NmatHeat2D(eta,psi);
[B, detJ] = BmatHeat2D(eta,psi,C);
disp_and_stress.m
% compute stresses and displacements
function disp_and_stress(e,d)
include_flags;
41
% print stresses at element Gauss points
fprintf(1,'%d\t\t\t%f\t\t\t%f\t\t\t%f\t\t\t%f\n',e,gauss_pt(1),gauss_pt(2),stress_gauss(1),stress_gauss
(2));
for i = 1:nplot
xi = xplot(i); % x coordinate
N = Nmatrix1D(xi,xe); % shape function value
B = Bmatrix1D(xi,xe); % derivative of shape functions
ExactSolution.m
% plot the exact stress
function ExactSolution
include_flags;
subplot(2,1,1);
% exact displacement for xa
c1 = 72; c2 = 1 - (c1/16)*log(2);
u1 = -.5*xa + (c1/16)*log(xa) + c2;
% exact displacement for xb
c3 = 48; c4 = log(5)/16*(c1-c3) + c2;
u2 = -.5*xb + (c3/16)*log(xb) + c4;
% plot displacement
h = plot([xa xb],[u1 u2], '--r' );
legend(h,'exact');
subplot(2,1,2);
% exact stresses for xa
ya = (36-4*xa)./xa;
% exact stress for xb
42
yb = (24-4*xb)./xb;
% plot stresses
plot([xa xb],[ya yb], '--r' );
elasticity2D.m
%%%%%%%%%%%%%%%%%
% 2D Elasticity (Chapter 9) %
% Haim Waisman %
%%%%%%%%%%%%%%%% %
clear all;
close all;
43
[d,r] = solvedr(K,f,d);
⎡ 21 22 23 24 ⎤
⎢ 22 23 24 25 ⎥⎥
⎢
⎢ 0.0 0.0 0.0 0.0 ⎥
n _ bc = ⎢ ⎥
⎢ −20.0 −20.0 −20.0 −20.0 ⎥
⎢ 0.0 0.0 0.0 0.0 ⎥
⎢ ⎥
⎢⎣ −20.0 −20.0 −20.0 −20.0 ⎥⎦
The number of columns indicates the number of edges that lie on the natural boundary
(specified by nbe). The first and second rows indicate the first and the second node that
define of an element edge. The third and fourth rows correspond to the appropriate
traction values in x and y directions at the first node, respectively, whereas rows fifth and
sixth correspond to tractions in x and y directions specified at the second node. Input files
for the 1 and 64 element meshes are given on the program website.
Input_file_16ele.m
% Input Data for Example 9.3 (16-element mesh)
% material properties
E = 30e6; % Young’s modulus
ne = 0.3; % Poisson’s ratio
D = E/(1-ne^2) * [ 1 ne 0 % Hooke’s law – Plane stress
ne 1 0
0 0 (1-ne)/2];
% mesh specifications
nsd = 2; % number of space dimensions
nnp = 25; % number of nodal nodes
nel = 16; % number of elements
nen = 4; % number of element nodes
ndof = 2; % degrees-of-freedom per node
neq = nnp*ndof; % number of equations
44
flags = zeros(neq,1); % an array to set B.C flags
e_bc = zeros(neq,1); % essential B.C array
n_bc = zeros(neq,1); % natural B.C array
% essential B.C.
ind1 = 1:10:(21-1)*ndof+1; % all x dofs along the line y=0
ind2 = 2:10:(21-1)*ndof+2; % all y dofs along the line x=0
flags(ind1) = 2; e_bc(ind1) = 0.0;
flags(ind2) = 2; e_bc(ind2) = 0.0;
% plots
compute_stress = 'yes';
plot_mesh = 'yes'; % (same as in Chapter 8)
plot_nod = 'yes';
plot_disp = 'yes';
plot_stress = 'yes';
plot_stress_xx = 'yes';
plot_mises = 'yes';
fact = 9.221e3; % factor for scaled displacements plot
% mesh generation
mesh2d;
include_flags.m
% file to include global variables
global ndof nnp nel nen nsd neq ngp nee neq
global nd e_bc P b D
global LM ID IEN flags n_bc
global x y nbe counter nodestress
global compute_stress plot_mesh plot_disp plot_nod
global plot_stress_xx plot_mises fact
45
denoted as blk to the beginning of each block and loop over all degrees-of-freedom ndof
in that block.
setup_ID_LM.m
function d=setup_ID_LM(d);
include_flags;
count = 0; count1 = 0;
for i = 1:neq
if flags(i) == 2 % check if a node on essential boundary
count = count + 1;
ID(i) = count; % arrange essential B.C nodes first
d(count)= e_bc(i); % store essential B.C in reordered form (d_bar)
else
count1 = count1 + 1;
ID(i) = nd + count1;
end
end
for i = 1:nel
n = 1;
for j = 1:nen
blk = ndof*(IEN(j,i)-1);
for k = 1:ndof
LM(n,i) = ID( blk + k ); % create the LM matrix
n = n + 1;
end
end
end
elast2Delem.m
function [ke, fe] = elast2Delem(e)
include_flags;
46
eta = gp(i);
psi = gp(j);
N = NmatElast2D(eta,psi); % shape functions
[B, detJ] = BmatElast2D(eta,psi,C); % derivative of the shape functions
ke = ke + w(i)*w(j)*B'*D*B*detJ; % element stiffness matrix
be = N*b(:,e); % interpolate body forces using shape functions
fe = fe + w(i)*w(j)*N'*be*detJ; % element body force vector
end
end
NmatElas2D.m
% Shape functions for 2D elasticity defined in parent element coordinate system
function N = NmatElast2D(eta,psi)
N1 = 0.25*(1-psi)*(1-eta);
N2 = 0.25*(1+psi)*(1-eta);
N3 = 0.25*(1+psi)*(1+eta);
N4 = 0.25*(1-psi)*(1+eta);
BmatElas2D.m
% B matrix function for 2D elasticity
function [B, detJ] = BmatElast2D(eta,psi,C)
47
The function loops over nbe edges on the essential and performs a one-dimensional
integration using Gauss quadrature. The integration is performed by transforming the
boundary edge to the parent coordinate system ξ ⊂ [0,1] . The nodal boundary force
vector is then assembled to the global force vector using ID array. Similarly, point forces
defined at the nodes are assembled into the global nodal force vector using the ID array.
point_and_trac.m
% Compute and assemble point forces and boundary force vector
function f = point_and_trac(f);
include_flags;
psi = gp(i);
N = 0.5*[1-psi 0 1+psi 0; % 1D shape functions in the parent edge
0 1-psi 0 1+psi]; % for interpolating tractions in x and y
T = N * n_bce;
ft = ft + w(i)*N' *T *J; % compute traction
end
end
Postprocessing: postprocessor.m
48
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 Figure 9.13 in Chapter 9.
To obtain the fringe or contour plots of stresses, stresses are computed at element nodes
and then averaged over elements connected to the node. Alternatively, stresses can be
computed at the Gauss points where they are most accurate and then interpolated to the
nodes. The user is often interested not only in the individual stress components, but in
some overall stress value such as Von-Mises stress. In case of plane stress, the von Mises
stress is given by σ Y = σ 12 + σ 22 − 2σ 1 σ 2 , where σ1 and σ2 are principal stresses given
2
σx +σ y ⎛ σ x −σ y ⎞
by σ 1,2 = ± ⎜ ⎟ + τ xy . Figure 9.14 in Chapter 9 plots the σxx stress
2
2 ⎝ 2 ⎠
contours for the 64-element mesh.
postprocessor.m
% deformation and stress ouput
function postprocess(d);
include_flags
displacement.m
% scale and plot the deformed configuration
function displacements(d);
include_flags;
if strcmpi(plot_disp,'yes')==1;
displacement = d(ID)*fact; % scale displacements
49
% Compute deformed coordinates
j = 1;
for i = 1:ndof:nnp*ndof
xnew(j) = x(j) + displacement(i);
ynew(j) = y(j) + displacement(i+1);
j = j + 1;
end
% Plot deformed configuration over the initial configuration
for e = 1:nel
XXnew = [xnew(IEN(1,e)) xnew(IEN(2,e)) xnew(IEN(3,e)) xnew(IEN(4,e)) xnew(IEN(1,e))];
YYnew = [ynew(IEN(1,e)) ynew(IEN(2,e)) ynew(IEN(3,e)) ynew(IEN(4,e)) ynew(IEN(1,e))];
plot(XXnew,YYnew,'k');hold on;
end
title('Initial and deformed structure'); xlabel('X'); ylabel('Y');
end
get_stress.m
% Compute strains and stresses at the gauss points
function get_stress(d,e);
include_flags;
N = NmatElast2D(eta,psi);
[B, detJ] = BmatElast2D(eta,psi,C);
50
fprintf(1,'\t%f\t\t%f\t\t%f\t\t%f\t\t%f\n',stress_gauss');
nodal_stress.m
% compute the average nodal stress values
function nodal_stress(d,e);
include_flags;
Stress_contours.m
function stress_contours;
include_flags;
if strcmpi(plot_stress_xx,'yes')==1;
figure(2);
for e=1:nel
XX = [x(IEN(1,e)) x(IEN(2,e)) x(IEN(3,e)) x(IEN(4,e)) x(IEN(1,e))];
YY = [y(IEN(1,e)) y(IEN(2,e)) y(IEN(3,e)) y(IEN(4,e)) y(IEN(1,e))];
sxx = nodestress(IEN(:,e),1)./counter(IEN(:,e));
dd = [sxx' sxx(1)];
patch(XX,YY,dd);hold on;
end
title('\sigma_x_x contours'); xlabel('X'); ylabel('Y'); colorbar
end
51
if strcmpi(plot_mises,'yes')==1;
for e=1:nel
XX = [x(IEN(1,e)) x(IEN(2,e)) x(IEN(3,e)) x(IEN(4,e)) x(IEN(1,e))];
YY = [y(IEN(1,e)) y(IEN(2,e)) y(IEN(3,e)) y(IEN(4,e)) y(IEN(1,e))];
sxx = nodestress(IEN(:,e),1)./counter(IEN(:,e));
syy = nodestress(IEN(:,e),2)./counter(IEN(:,e));
sxy = nodestress(IEN(:,e),3)./counter(IEN(:,e));
dd = [mises' mises(1)];
figure(3);
patch(XX,YY,dd);hold on;
end
title('Von Mises \sigma contours'); xlabel('X'); ylabel('Y'); colorbar
end
beam.m
The main program is given in beam.m file. It can be seen that it is almost identical to that
in Section 12.3.
%%%%%%%%%%%%%%%%%%%%%%
% Beam (Chapter 10) %
% Suleiman M. BaniHani, Rensselaer %
% Rensselaer Polytechnic Institute %
%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;
% Preprocessing
[K,f,d] = preprocessor;
52
[ke,fe] = beamelem(e);
[K, f] = assembly(K,f,e,ke,fe);
end
% Add nodal boundary force vector
f = NaturalBC(f);
% Postprocessing
postprocessor(d)
include_flags.m
% Include global variables
global nsd ndof nnp nel nen neq nd ngp
global CArea E leng phi xp P
global plot_beam plot_nod plot_stress
global LM IEN x y stress body
global flags ID xplot n_bc e_bc np nplot neqe
preprocessor.m
The preprocessor function reads input file and generates ID and LM arrays. The structure of ID
array is identical to that for the scalar field problems (see for instance program in Chapter 5); The
LM relates elements (columns) to equation numbers after renumbering. The LM array for
⎡1 3⎤
⎢2 4⎥
Example Problem 10.1 is LM = ⎢ ⎥.
⎢3 5⎥
⎢ ⎥
⎣4 6⎦
% reads input data and sets up mesh information
function [K,f,d] = preprocessor;
include_flags;
% Generate LM array
count = 0; count1 = 0;
for i = 1:neq
if flags(i) == 2 % check if essential boundary
count = count + 1;
ID(i) = count; % number first the degrees-of-freedom on essential boundary
d(count)= e_bc(i); % store the reordered values of essential B.C
else
count1 = count1 + 1;
ID(i) = nd + count1;
end
end
for e = 1:nel
for j = 1:nen
for m = 1:ndof
ind = (j-1)*ndof + m;
LM(ind,e) = ID(ndof*IEN(j,e) - ndof + m) ; % create the LM matrix
end
end
end
53
input_file_example10_1.m
The cross-sectional area is prescribed at the nodes and interpolated using linear shape functions.
The Young’s modulus and body forces are assumed to be constant within one element; they are
prescribed for each element. Essential and natural boundary conditions are prescribed for each
degree of freedom on essential and natural boundary, respectively.
% Input Data for Example 10.1
nsd = 2; % Number of spatial dimensions
ndof =2; % Number of degrees-of-freedom per node
nnp = 3; % Total number of global nodes
nel = 2; % Total number of elements
nen = 2; % Number of nodes in each element
neq = ndof*nnp; % Number of equations
neqe = ndof*nen; % Number of equations for each element
% Element properties
CArea = [1 1 1]'; % Elements cross-sectional area
leng = [8 4 ]; % Elements length
body = [-1 0 ]'; % body forces
E = [1e4 1e4]'; % Young’s Modulus
% gauss integration
ngp = 2; % number of gauss points
% output controls
plot_beam = 'yes';
plot_nod = 'yes';
% mesh generation
beam_mesh_10_1;
% number of points for plot
nplot=300;
54
beam_mesh_10_1.m
function beam_mesh_10_1
include_flags;
% connectivity array
IEN = [1 2
2 3];
% plot beam
plotbeam;
beamelem.m
% generate element stiffness matrix and element nodal body force vector
function [ke, fe] = beamelem(e)
include_flags;
for i = 1:ngp
N = NmatrixBeam(gp(i),xe); % shape functions matrix
B = BmatrixBeam(gp(i),xe) *1/J^2; % derivative of shape functions
Ae = [N(1) N(3)]*CArea(IENe); % calculate cross-sectional area at element gauss points
Ee = E(e); % extract Young's modulus
be = body(e); % extract body forces
ke = ke + w(i)*(B'*Ae*Ee*B); % calculate element stiffness matrix
fe = fe + w(i)*N'*be; % calculate element nodal force vector
end
ke = J*ke;
fe = J*fe;
NmatrixBeam.m
% Shape functions in the natural coordinate s
function N = NmatrixBeam(s,xe)
L=xe(2)-xe(1);
N(1)=1/4*(1-s)^2*(2+s);
N(2)=L/8*(1-s)^2*(1+s);
N(3)=1/4*(1+s)^2*(2-s);
N(4)=L/8*(1+s)^2*(s-1);
55
BmatrixBeam.m
% Derivative of the shape functions in the natural coordinate s
function B = BmatrixBeam(s,xe)
L=xe(2)-xe(1);
B(1)=3/2*s;
B(2)=L*(3/4*s-1/4);
B(3)=-3/2*s;
B(4)= L*(3/4*s+1/4);
SmatrixBeam.m
% Second derivative of the shape functions
function S = SmatrixBeam(s,xe)
L=xe(2)-xe(1);
S(1)=3/2;
S(2)=3/4*L;
S(3)=-3/2;
S(4)= 3/4*L;
naturalBC.m
% compute and assemble nodal boundary force vector
function f = naturalBC(f);
include_flags;
for i = 1:neq
if flags(i) == 1
dof = ID(i);
f(dof) = f(dof) + n_bc(dof);
end
end
postprocessor.m
% postprocessing function
function postprocessor(d)
include_flags;
for i = 1:nplot
xi = xplotgauss(i); % current coordinate
N = NmatrixBeam(xi,xe); % shape functions
B = BmatrixBeam(xi,xe)*1/J^2; % first derivative of shape functions
S = SmatrixBeam(xi,xe)*1/J^3; % second derivative of shape functions
Ee = E(e); % Young's modulus
displacement(i) = N*de ; % displacement output
moment(i) = Ee*B*de; % moment output
shear(i) = Ee*S*de; % Shear force output
end
56
% plot displacements, moment and shear forces
[x_plot,S_ex,M_ex,w_ex]=exact; % call exact beam solution
figure(2)
plot(xplot,displacement,'-.r'); hold on;
plot(x_plot,w_ex,'-k'); legend('FE','Exact Solution'); hold on;
ylabel('displacement'); title('Displacements: FE versus analytical beam solutions');
figure(3)
plot(xplot,moment,'-.r'); hold on;
plot(x_plot,M_ex,'-k'); legend('FE','Exact Solution'); hold on;
ylabel('moment'); xlabel('x'); title('Moments: FE versus analytical beam solutions');
figure(4)
plot(xplot,shear,'-.r'); hold on;
plot(x_plot,S_ex,'-k'); legend('FE','Exact Solution'); hold on;
ylabel('shear'); xlabel('x'); title('Shear: FE versus analytical beam solutions');
end
Functions which are identical to those in Chapter 5 are: assembly.m, solvedr.m, gauss.m
⎧-1 if i = j − 1 or i = j + 1
⎪
Aij = ⎨ 2 if i = j
⎪ 0 otherwise
⎩
−1 −1
and compute A . Then check how closely B = A A corresponds to I . Do this for
n = 5,10,1000 and a larger value you choose. The accuracy of the computed product
can be compared to the correct results by computing a norm of the error given by
1
∑∑ ( B − I ij )
n n 2
err =
n2
ij
i =1 j =1
Problem 1-2
Consider a system of linear equations
57
⎧
⎪ 8x + x 2 + 6x 3 = 7.5
⎪
⎪ 1
⎪
⎨ 3x 1 + 5x 2 + 7x 3 = 4
⎪
⎪
⎪
⎪4x + 9x 2 + 2x 3 = 12
⎪
⎩ 1
a) Write the system of equations in matrix notation Ax = b and solve for the unknown
x using MATLAB
b) Suppose we impose an additional constraint on the solution
g (x) = x 1 + x 2 + x − 1 = 0 . Using MATLAB find a new vector xnew so that it will
satisfy exactly the constraint equation g (x )
new
and will minimize the error
T
err (xnew ) = (Axnew − b) (Axnew − b)
a) Check if the above three matrices are positive definite. Recall that if for any vector
x ≠ 0 we have xT Kx > 0 then matrix K is called Symmetric Positive Definite
T
(SPD). If, on the other hand, x Kx ≥ 0 for any vector x ≠ 0 then the matrix K is
symmetric semi-positive definite. Choose one of the semi-positive definite matrices
shown above and show that for any right hand side vector, f , the system of
equations Kd = f has no unique solution.
b) Verify your results by computing the eigenvalues for the above three matrices
58