FEM Matlab Program
FEM Matlab Program
Exercise M1
Matlab exercises
The following exercises are intended for brushing up your programming skills in general
and your Matlab skills in particular. Besides focusing on programming, the exercises will
touch upon nite element specic topics. Start by answering the questions which are
marked with an asterisk (*), i.e. Questions 1, 2, 3, 4 and 8.
x2
e
x1
Bar elements
Question 1*: Write a Matlab script which calculates and stores the coordinates of the
nodes shown in Figure 1. Assume that e = 3.0. Use a for-loop which runs through the
ve node numbers one-by-one (for no=1:5 . . . end).
Question 2*: Now the total number of nodes is a variable, nno. The rst node has the coordinates (x1 , x2 ) = (0.0, 0.0) and the last node has the coordinates (x1 , x2 ) = (12.0, 0.0).
Write a Matlab script which calculates and stores the coordinates of the nodes such that
the node distance, e , is constant. Run the script with dierent values of nno and make
sure that the result is correct.
1
1
2
2
3
3
4
4
DTU Byg
1/4
Exercise M1
Finite elements are arranged from node to node as shown in Figure 2. The element
topology is specied by the nodes at each end of it. For each element this information is
stored in a 1 row matrix: [start node, end node]. The topology information on the entire
system or mesh may be stored in a matrix named T :
T (el, : ) = [no1, no2] ,
or T (2, : ) = [2 3]
Question 3*: Write a Matlab script which stores the topology of the elements shown
in Figure 2. Use a for-loop which runs through the four element numbers one-by-one
(for el=1 : 4 . . . end).
Question 4*: Now write a Matlab script which generates elements between a variable
number of nodes as specied in Question 2.
Quadrilateral elements
Now we turn to quadrilateral elements. We want to mesh a rectangular domain as shown
in Figure 3.
Question 5: Write a Matlab script which calculates and stores the coordinates of the
nodes shown in Figure 3. Use two for-loops one inside the other, which run through the
four rows of nodes and the four columns of nodes:
for row=1:4
..
.
for col=1:4
..
.
end
end
The quadrilateral elements have 4 nodes, one at each corner. The element topology is
given by the list of node numbers dening the element in a predened order. Normally
this order is counter-clockwise. The topology information may be stored in the matrix T :
T (el, : ) = [no1, no2, no3, no4] ,
Question 6: Write a Matlab script which stores the topology of the elements shown in
Figure 3.
Question 7: In Figure 3, let the number of elements in the x1 and x2 directions be
variables called nelx1 and nelx2, respectively. Modify your Matlab scripts for Questions
5 and 6 to accommodate this change.
2/4
Exercise M1
x2
13
14
7
L2
15
8
10
4
11
5
6
1
16
9
12
6
7
2
8
3
x1
L1
Figure 3: Square domain meshed by quadrilateral elements.
DTU Byg
3/4
Exercise M1
function len
% Main program ( f u n c t i o n )
11
13
15
% coordinates
X1=[0 ,
0;
1,
1;
3/2 , 0 ] ;
% node 1
% node 2
% node 3
% topology
T1 ( 1 , : ) =[1 2 ] ;
T1 ( 2 , : ) =[2 3 ] ;
% element 1
% element 2
% c a l c u l a t e t o t a l length of a l l elements
L1=c a l c l e n (X1 , T1)
% c a l l f u n c t i o n c a l c l e n which r e t u r n s t o t a l l e n g t h
% of a l l elements
end
17
%
19
33
f u n c t i o n L = c a l c l e n (X,T)
% c a l c u l a t e s the t o t a l length of a l l elements
% i n p u t : X, c o o r d i n a t e matrix
%
T, t o p o l o y c e l l a r r a y
% output : L , t o t a l l e n g t h o f a l l e l e m e n t s
L=0;
f o r e l =1: s i z e (T, 1 )
% run through a l l e l e m e n t s , onebyone
nos=T( e l , : ) ;
% nos : node numbers o f e l e m e n t e l
Xel=X( nos , : ) ;
% Xel : c o o r d i n a t e s o f t h e e l e m e n t nodes
Le=l e n e l ( Xel ) ;
% c a l l f u n c t i o n l e n e l which r e t u r n s e l e m e n t l e n g t h
L=L+Le ;
% add e l e m e n t l e n g t h t o sum o f e l e m e n t l e n g t h s
end
% L i s r e t u r n e d by t h e f u n c t i o n
end
35
21
23
25
27
29
31
37
39
function l e l = l e n e l (x)
% c a l c u l a t e s t h e l e n g t h o f t h e v e c t o r g i v e n by x ( l e n g t h o f e l e m e n t )
% input : x ,
2 x2 matrix o f e l e m e n t c o o r d i n a t e s
% output : l e l , l e n g t h o f e l e m e n t
41
43
s=x ( 2 , : )x ( 1 , : ) ;
l e l =s q r t ( dot ( s , s ) ) ;
45
end
len.m
4/4