Anda di halaman 1dari 4

11305 - The Finite Element Method

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.

Exercise M1.1: The for loop


Finite elements are normally dened by their end or corner nodes. Nodes are points in the
domain space in which a global coordinate system is dened, and each node is identied
by a node number. The position of a node is given by a set of coordinates. Here we will
only consider a plane domain space. A matrix named X is applied to hold the information
about the node coordinates:
X(no, :) = [x1, x2]

x2
e

x1

Figure 1: Nodes shown as heavy dots. Node numbers in circles.

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

Figure 2: Element topology. Element numbers in squares.

DTU Byg

1/4

Exercise M1

11305 - The Finite Element Method

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] ,

Ex. : T (2, : ) = [2, 3] ,

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] ,

Ex. : T (6, : ) = [7, 8, 12, 11]

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

DTU Byg - 2015

11305 - The Finite Element Method

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.

Exercise M1.2: Functions


In FEM programmes many operations are repetitions, e.g. the element stiness matrix
is established for all elements in the same way. Therefore it is convenient to structure
the FEM programme using sub-programs which can perform these repetitions based on
element specic data. Although all the element stiness matrices in a mesh are established
in the same way, each stiness matrix does depend on the characteristics of the specic
element.
The programme code listed on the next page illustrates the use of functions in Matlab.
The code is available on CampusNet. Three functions are listed. The rst function has a
call at line 14 to the second function which has a call at line 29 to the third function.
The functions are commented and will not be further explained here. Study the ow
of data by looking at what is input to the functions and what is output (returned) by the
functions.
Here all three functions are collected in the same le. Another possibility is to have
each le in a separate le. The le names should be the same as the function name followed
by the extension .m, e.g. the lename of the second function would be calc len.m.
Question 8*: Modify the code such that the user may specify a list of elements for
which the total length is calculated. Apply the code to your mesh denitions under Question 2.
Question 9: Write a similar Matlab script which calculates the total area of a domain
meshed by quadrilateral elements and test it on your mesh denitions under Questions 5
and 6 or 7.

DTU Byg

3/4

Exercise M1

11305 - The Finite Element Method

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

% s : v e c t o r from node 1 t o node 2


% l e l : l e n g t h o f s , r e t u r n e d by t h e f u n c t i o n

len.m

4/4

DTU Byg - 2015

Anda mungkin juga menyukai