You are on page 1of 4

Last printed 31/01/2006 13:05:00 1 Dr N.A.

Alexander

University of Bristol, Department of Civil Engineering
Computer Modelling II, Dr Nick A. Alexander 2006

A Tutorial on Finite Element Programming in MATLAB

1. Matrices, solutions of equations
Create the following m-file. In Line 1 Matlab function clc clears the command window, clear all
deletes all data in workspace memory. Line 2 defines the 4-by-4 matrix A, Elements in A are define row-
wise with commas (,) between elements and a semicolon (;) at the end of a row. Line 3 defines a column
vector B. In Matlab the apostrophe ()symbol is a transpose operator, i.e. in line 3 the vector is defined as
row vector then transposed to produce a column vector. We can solve the following matrix system

=
4
3
2
1
1 9 8 7
5 2 4 5
6 5 4 2
4 3 2 1
3
6
5
3
x
x
x
x
AX B (1)

Line 4 inverses matrix A and post multiples vector B to determine unknowns. The tic function starts a
stopwatch and tic stop the stop watch. This enables us to determine how long it takes to solve these
equations with matrix inversion and multiplication. Line 5 employs a backslash (\) operator to solve the
algebraic system. This method is more efficient than line 4 and should give identical results. Line 6 shows
the difference in the two solution methods. Line 7 employs the colon (:) operator, this defines a row vector
with an implicit loop starting from 1 to 3 in steps of 1. i.e. D1=[1,2,3]. Line 8 shows a more general case
where a row vectors is defined by the implicit loop starting from 0.2 to 0.9 , i.e.
D2=[0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]. Line 9 demonstrates the use of the colon operator in
matrix subscripts. A(:,2) returns the elements in every row and the 2
nd
column i.e. a column vector.
[2;4;4;8]

Example1.m
1
2
3
4
5
6
7
8
9
clc; clear all;
A=[1,2,3,4; 2,4,5,6; 5,4,2,5; 7,8,9,1]
B=[3,5,6,-3]
tic, X1=(A^-1)*B, toc
tic, X2=A\B, toc
X2-X1
D1=1:3
D2=0.2:0.1:0.9
A(:,2)

2. Matrix partitioning
The solution of system defined in equation (2) requires partitioning of the matrix A. In line 2 define the
known matrices. The (;) acts as a separator between Matlab statements that also suppress any output to the
command window of the command that precedes it. First we solve for
1
C . Line 3 define the submatrices of
Last printed 31/01/2006 13:05:00 2 Dr N.A.Alexander

A. The (,) also acts as a separator between Matlab statements but it does not suppress any output to the
command window. Line 4 solve for vector
1
C using the backslash operator. Line 5 solves for unknown
2
B .
Line 6 creates the complete B vector and C vector. Line 7 is a consistency check that should produce a
vector of zeros. The percentage (%) operator allows you to place remarks in the code, i.e. anything to the
right of a % on that line is ignored by the Matlab Interpreter/Compiler.

2 22 1 21 2
2 12 1 11 1
2
1
22 21
12 11
2
1
C A C A B
C A C A B
C
C
A A
A A
B
B
AC B
+ =
+ =

=
2 . 0 1 9 8 7
5 2 4 5
6 5 4 2
4 3 2 1
6
2
3
3
2
1
1
x
x
x
f
(2)


Example2.m
1
2
3
4
5
6
7
clc; clear all;
A=[1,2,3,4; 2,4,5,6; 5,4,2,5; 7,8,9,1]; B1=[3,5,6]; C2=[0.2];
A11=A(1:3,1:3), A12=A(1:3,4), A21=A(4,1:3), A22=A(4,4)
C1=A11\(B1-A12*C2)
B2=A21*C1+A22*C2
B=[B1; B2], C=[C1; C2]
B-A*C % solution consistency check


3. Advanced partitioning
In the above problemit was quite simple to partition the system as no shuffling of rows and columns were
required. In the system, equation (3), shuffling must proceed partitioning.

2 22 1 21 2
2 12 1 11 1
2
1
22 21
12 11
2
1
C A C A B
C A C A B
C
C
A A
A A
B
B
AC B
+ =
+ =

=
2 . 0
1 . 0
1 7 9 8
4 1 3 2
5 5 2 4
6 2 5 4
6
2
2 . 0
1 . 0
1 9 8 7
5 2 4 5
6 5 4 2
4 3 2 1
6
2
3
2
4
1 3
2
4
1
x
x
f
f x
x
f
f
(3)

Matlab provides a very useful extension to matrix subscript operations that is invaluable here. The Matlab
subscript notation, A(f,g) will give an element at row f column g if f and g are scalars. In the case
where f and g are vectors Matlab includes an extension to subscript notation; A(f,g) returns the
submatrix of A that contains all elements that lie in both rows defines by f and columns defined by g. Thus
it is possible to abstract matrices
11
A ,
12
A ,
21
A and
22
A without shuffling A. Line 3 defines f as a vector
containing the rows in which the unknowns in C occur. Initially g=[1,2,3,4]; operation g(f)=[]
deletes (nulls) the elements in g that have lie in position defined by f resulting in g=[1,4]. This is
rather convoluted way of defining g but it does show some how the null operator [] can delete elements in
a vector. Lines 4 and 5 define the matrices
11
A ,
12
A ,
21
A and
22
A . Lines 6 and 7 solve as before. Lines 8
and 9 reassembles the complete B vector and C vectors. Matlab function zeros(4,1) create a vector (4
rows by 1 column) of zeros. i.e. [0;0;0;0]. B(f)=B1 copies elements in B1 into vector B at positions
Last printed 31/01/2006 13:05:00 3 Dr N.A.Alexander

in B that are defined by vector f. Thus Matlabs advanced matrix subscript notation can be used to
abstract submatrices from a larger matrix and assign submatrices within a larger matrix (as is the case here).

Example3.m
1
2
3
4
5
6
7
8
9
10
clc; clear all;
A=[1,2,3,4; 2,4,5,6; 5,4,2,5; 7,8,9,1]; B1=[2;6]; C2=[0.1; 0.2];
f=[2,3], g=1:4, g(f)=[]
A11=A(f,f), A12=A(f,g)
A21=A(g,f), A22=A(g,g)
C1=A11\(B1-A12*C2)
B2=A21*C1+A22*C2
B=zeros(4,1); B(f)=B1, B(g)=B2
C=zeros(4,1); C(f)=C1, C(g)=C2
B-A*C % solution consistency check

Question (1)
Create a Matlab program that is able to solve system(4) for the unknowns when [ ]
T
5 , 4 , 3 , 2 = F . Code a
consistency check.

= KU F

4
3
2
1
4
3
2
1
12 2 0 1
2 7 2 0
0 2 6 1
1 0 1 5
u
u
u
u
f
f
f
f
(4)

Question (2)
Create a Matlab programthat is able to solve system (4) for the unknowns when [ ]
T
f f 5 , 4 , ,
2 1
= F and
[ ]
T
u u
4 3
, , 2 . 0 , 1 . 0 = U . Code a consistency check.


Question (3)
Create a Matlab programthat is able to solve system (4) for the unknowns when [ ]
T
f f
4 2
, 4 , , 1 = F and
[ ]
T
u u 2 , , 2 . 0 ,
3 1
= U . Code a consistency check.



4. Matlab Functions
Matlab functions can be stored in separate Matlab m-files, however in these notes it is suggested that all the
code is contained in one m-file. Matlab requires a principle function (like in the case of VB) which is
conventionally named main in these notes but can be given any name (starting with a letter). Example4.m
Last printed 31/01/2006 13:05:00 4 Dr N.A.Alexander

contain a main function that calls a sub-function CalcR that returns the rotational transformation matrix R,
equation (5).

=




cos sin 0 0
sin cos 0 0
0 0 cos sin
0 0 sin cos
R (5)

Note that in line 4, the data theta is copied into variable t in the function and on returning fromthe
function R is copied into R1.

Example4.m
1
2
3
4
5
6
7
8
function main
clc; clear all;
theta=pi/4
[R1]=CalcR(theta)


function [R]=CalcR(t)
R1=[cos(t),-sin(t); sin(t), cos(t)];
R=zeros(4,4); R(1:2,1:2)=R1; R(3:4,3:4)=R1;


Question (4)
Create a Matlab function [K]=BarElemMatrix(u,EA) where u is a vector [x1,z1,x2,z2] that
contains the nodal coordinates ( )
1 1
, z x and ( )
2 2
, z x for the two ends of the bar element. Fromthese
coordinates you can compute its length L and angle theta. Hence, using the function CalcR in the
previous section, write the function that returns the bar element stiffness matrix using the following matrix
triple product. You must also supply the axial rigidity EA

= RkR K ,

=
0 0 0 0
0 1 0 1
0 0 0 0
0 1 0 1
L
EA
k (6)

5. Plotting graphs

In example4.man element is displayed using the Matlab plot function. In line 3 vector U contains the
nodal coordinates (x,z); node 1 is (0,0) and node 2 is (2,3). The plot expect to receive all the x coordinates
in one vector and all the z coordinate is another vector so x and z are defined in line 3 by abstracting the
appropriate elements of U. In line 4, the plot function string -bo define a continous blue line with
circle markers at each data point. See Matlab help for a complete list of options. Line 5 displays a title, label
for the x axis and y axis (which is our z axis). Line 6 show how to add a piece of text to the centre of this
Input (data sent to function)
Output (data returned by function)
Function
definition
Calling function
Last printed 31/01/2006 13:05:00 5 Dr N.A.Alexander

line (bar element). Matlab num2str(a) function converts the data a into a string. This is required by the
text function.

Example4.m
1
2
3
4
5
6
clc; clear all;
ElemNum=3;
U=[0,0,2,3], x=U([1,3]), z=U([2,4])
plot(x,z,'-bo','lineWidth',3);
title('Element plot'); xlabel('x [m]'); ylabel('z [m]');
text(mean(x),mean(z),num2str(ElemNum));


6. Suggested data structures for defining a truss framework

In order to develop a general FE programit is important to organise the way you supply data to the program.
It is suggested that you employ the following matrices NXY,ENC and EA to define any truss structure. For
example consider the 5 bar truss below. On the ith row of NXY there are the (x,z) coordinates of ith node.
On the ith row of ENC are the node numbers of (end 1, end 2) of the ith bar element. The ith column of EA
contain the axial rigidity of the ith bar element.


Question (5)
Create a Matlab programthat plots the entire structure. You must create a function TestStructure that
returns the data NN,NXY,NE,ENC,EA for the above structure. Also it should call the function you have
already written to calculate the bar element stiffness matrices.




Number of Nodes NN = 4

Nodal Coordinates

=
1 2
1 0
0 2
0 0
xy
N
Number of Elements NE = 5
Element Nodal
Connections

=
4 2
4 3
4 1
3 1
2 1
NC
E
Element
Axial rigidities
[ ] 1 1 1 1 1 = EA
1
2
3
4
1
2
3
4
5
2m
1m
x

global axes
z
Last printed 31/01/2006 13:05:00 6 Dr N.A.Alexander

Example5.m
1
2
3
4
function FEAmain
clc; clear all;
[NN,NXY,NE,ENC,EA]=TestStructure
for i=1:NE

You will need to abstract the nodal coordinate of the ith element
Create a function PlotElement(U,ElemNum) that plot a single element defined by a
coordinate vector U (see example4.m) and element number ElemNum
Use the hold on Matlab function after every plot function. (what does this do?)
Calc your [K{i}]=BarElemMatrix(u,EA) function to calculate the element
stiffness matrices. K{i} is a cell array, e.g. a vector where each element in the vector is
a matrix. This is a useful way of storing all the element stiffness matrices.

end

Question (6)
The next stage in a FE programis to take all the element stiffness matrices K{i} and assemble the structure
stiffness matrix KS. In order to do this you should make use of extend matrix subscript notation shown in
example3.m. This is the first part of assembling the matrix equation (7).

s s s
U K F = (7)
7. Loading and supports
Loading vector FS in equation (7) needs to be defined. It is suggested that matrix LD is employed. Each row
of LD contains, in the first column, the node number at which loading is applied, then in second and third
columns to force in the x and z directions respectively. Definition of support restraints requires the
introduction of degrees of freedom (dofs). In a truss framework each node has two dofs; the x and z
displacements. The numbering system for dofs suggested in these notes is the ith node contain dofs
numbered ( ) i i 2 , 1 2 e.g. node 3 contains dofs 5 and 6.

The figure above has two nodal supports but only three dofs are restrained. At node 1 only dof 1 is
restrained, dof 2 is free. At node 3 both dofs 5 and 6 are restrained. Matrix RD contains the displacement at
the restrained dofs. The ith row of RD contains in the first column the dof number and in the second column
Number of Nodes
Loaded
NNL = 2

Loads


=
0 1 4
3 2 2
LD
Number of degree of
freedom restraints
(Supports)
NR = 3
Displacement at
restraint dofs (0 is a
fully rigid support)

=
0 6
0 5
0 1
RD
1
2
4
3
1kN
2kN
3kN
Bits you
will need
to add
Last printed 31/01/2006 13:05:00 7 Dr N.A.Alexander

the displacement at this dof. Zero displacement means a completely rigid support restraint that allows no
movement.

Question (6)
The next stage in a FE program

Modify [NN,NXY,NE,ENC,EA,NL,LD,NR,RD]=TestStructure function to include nodal
loads and support restraints data
Create another function [FS,US]=CalcLoadRestraintVector(NL,LD,NR,RD) use the
matrices LD and RD to define the global load vector FS and global displacement vector US.
Solve system equation (7) or (8), but remember it must be partitioned. You will have to employ the
technique you used in question (3)
Reassemble complete FS and US, perform consistency check.

( )
F F 22 R 21 F
R F F 12 R 11 R
F
R
22 21
12 11
F
R
S S S
U U K U K F
F U U K U K F
U
U
K K
K K
F
F
U K F
for solve
find sub
=
+ =

= (8)


Question (7)
Create a function that will display the deflected shape of the truss.

Write function PlotDefShape(US,NE,NXY,ENC). It should use function that you have
already written PlotElement.
You will have to scale displacements by multiplying them by a scale factor sf so that they are
visible. Can this be assigned automatically by you program?
Use the Matlab figure function creates a new figure.
Can you display the resultant nodal displacements as text at the nodes?

Question (8)
Finish off by computing the element axial forces, strains and stress.

8. Formatted Printing to command window
Formatted printing to the command window is useful. You can modify the following code to output the
results from your FEA program in a tabular form.

Example6.m
1
2
3
4
5
6
clc; clear all; disp(' ');
N=15; A=rand(N,2); B=ones(N,1);
t=' i A(i,1) A(i,2) B(i)'; disp(t);
for i=1:N
t=sprintf('%5g %14g %14g %14g',i,A(i,1:2),B(i)); disp(t);
end

You might also like