Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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 rowwise 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
3 1 5 2 B = AX = 6 5 3 7 2 3 4 x1 4 5 6 x2 4 2 5 x3 8 9 1 x4

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 C1 using the backslash operator. Line 5 solves for unknown B 2 . 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.

3 1 2 2 B = AC = 6 5 f1 7

2 3 4 x1 4 5 6 x2 B1 A11 = 4 2 5 x3 B 2 A 21 8 9 1 0.2

A12 C1 B1 = A11C1 + A12C 2 C B = A C + A C A 22 2 2 21 1 22 2

(2)

Example2.m 1
(1)

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

2 3 4 5 6 7

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 2nd column i.e. a column vector. [2;4;4;8]

3. Advanced partitioning
In the above problem it 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.

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, tic, X2=A\B, toc X2-X1 D1=1:3 D2=0.2:0.1:0.9 A(:,2) toc

f1 1 2 2 2 4 B = AC = 6 5 4 f 4 7 8 B A 1 = 11 B 2 A 21

3 4 0.1 2 4 5 2 6 x2 5 6 x2 6 4 2 5 5 x3 = 2 5 x3 f1 2 3 1 4 0.1 9 1 0.2 f 4 8 9 7 1 0.2 A12 C1 B = A11C1 + A12C2 1 A 22 C2 B 2 = A 21C1 + A 22C 2

(3)

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 C1 . Line 3 define the submatrices of
Last printed 31/01/2006 13:05:00 1 Dr N.A.Alexander

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 A11 , A12 , A 21 and A 22 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 A11 , A12 , A 21 and A 22 . 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 2 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 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 % solution consistency check

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

cos 0 0

0 0 cos sin

0 0 sin cos

(5)

Note that in line 4, the data theta is copied into variable t in the function and on returning from the function R is copied into R1.
Example4.m 1 2 3 4 5 function main clc; clear all; theta=pi/4 [R1]=CalcR(theta)
Input (data sent to function) Output (data returned by function) Calling function

10 B-A*C

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

6 7 8 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;


(4)
Function definition

f1 5 f 1 F = KU 2 = f3 0 f 4 1

1 0 1 u1 6 2 0 u 2 2 7 2 u 3 0 2 12 u 4

Question (4)
Create a Matlab function [K]=BarElemMatrix(u,EA) where u is a vector [x1,z1,x2,z2] that contains the nodal coordinates ( x1 , z1 ) and ( x 2 , z 2 ) for the two ends of the bar element. From these 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

Question (2)
Create a Matlab program that is able to solve system (4) for the unknowns when F = [ f1, f 2 ,4,5]T and

U = [0.1,0.2, u3 , u4 ]T . Code a consistency check.

Question (3)
Create a Matlab program that is able to solve system (4) for the unknowns when F = [1, f 2 ,4, f 4 ]T and

K = RkR

U = [u1 ,0.2, u3 ,2]T . Code a consistency check.

1 EA 0 k= L 1 0

0 1 0 0 0 0 0 1 0 0 0 0

(6)

5. Plotting graphs
In example4.m an 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
Last printed 31/01/2006 13:05:00 4 Dr N.A.Alexander

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 3 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));

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

Bits you will need to add

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.

6. Suggested data structures for defining a truss framework


In order to develop a general FE program it 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. Number of Nodes 3 4 4 Nodal Coordinates NN = 4

end

Question (6)
The next stage in a FE program is 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). Fs = K s U s (7)

1m

1 1 z 2m x global axes

0 0 2 0 N xy = 0 1 2 1 Number of Elements NE = 5 Element Nodal 1 2 1 3 Connections ENC = 1 4 3 4 2 4 Element EA = [1 1 1 1 1] Axial rigidities

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 (2i 1, 2i ) e.g. node 3 contains dofs 5 and 6.
Number of Nodes NNL = 2 Loaded Loads 2 LD = 4 Number of degree of NR = 3 freedom restraints (Supports) Displacement at 1 restraint dofs (0 is a RD = 5 fully rigid support) 6

1kN

2 3 1 0

Question (5)
Create a Matlab program that 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.

2kN

3kN

0 0 0

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
Last printed 31/01/2006 13:05:00 5 Dr N.A.Alexander Last printed 31/01/2006 13:05:00 6 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.

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

(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

Last printed 31/01/2006 13:05:00

Dr N.A.Alexander

You might also like