Vous êtes sur la page 1sur 4

# University of Bristol, Department of Civil Engineering A.

A. The (,) also acts as a separator between Matlab statements but it does not suppress any output to the
Computer Modelling II, Dr Nick A. Alexander © 2006 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
A Tutorial on Finite Element Programming in MATLAB right of a % on that line is ignored by the Matlab Interpreter/Compiler.

⎡ 3 ⎤ ⎡1 2 3 4⎤ ⎡ x1 ⎤
1. Matrices, solutions of equations ⎢ 2 ⎥ ⎢2 4 5 6⎥⎥ ⎢⎢ x2 ⎥⎥ ⎡ B1 ⎤ ⎡ A11 A12 ⎤ ⎡C1 ⎤ B = A11C1 + A12C 2
B = AC → ⎢ ⎥ = ⎢ → = → 1
4 2 5⎥ ⎢ x3 ⎥ ⎢⎣B 2 ⎥⎦ ⎢⎣ A 21 ⎥ ⎢ ⎥
Create the following m-file. In Line 1 Matlab function clc clears the command window, clear all (2)
⎢ 6 ⎥ ⎢5 A 22 ⎦ ⎣C2 ⎦ B 2 = A 21C1 + A 22C2
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 ⎣ f1 ⎦ ⎣7 8 9 1 ⎦ ⎣0.2⎦
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
Example2.m
⎡ 3 ⎤ ⎡1 2 3 4⎤ ⎡ x1 ⎤ 1 clc; clear all;
⎢ 5 ⎥ ⎢2 4 5 6⎥⎥ ⎢⎢ x2 ⎥⎥
B = AX → ⎢ ⎥ = ⎢ (1) 2 A=[1,2,3,4; 2,4,5,6; 5,4,2,5; 7,8,9,1]; B1=[3,5,6]’; C2=[0.2];
⎢ 6 ⎥ ⎢5 4 2 5⎥ ⎢ x3 ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ 3 A11=A(1:3,1:3), A12=A(1:3,4), A21=A(4,1:3), A22=A(4,4)
⎣− 3⎦ ⎣7 8 9 1 ⎦ ⎣ x4 ⎦
4 C1=A11\(B1-A12*C2)
Line 4 inverses matrix A and post multiples vector B to determine unknowns. The tic function starts a 5 B2=A21*C1+A22*C2
stopwatch and tic stop the stop watch. This enables us to determine how long it takes to solve these 6 B=[B1; B2], C=[C1; C2]
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 7 B-A*C % solution consistency check
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 3. Advanced partitioning
matrix subscripts. A(:,2) returns the elements in every row and the 2nd column i.e. a column vector. In the above problem it was quite simple to partition the system as no shuffling of rows and columns were
[2;4;4;8] required. In the system, equation (3), shuffling must proceed partitioning.

Example1.m
⎡ f1 ⎤ ⎡1 2 3 4⎤ ⎡ 0.1⎤ ⎡ 2 ⎤ ⎡4 5 2 6⎤ ⎡ x2 ⎤
1 clc; clear all; ⎢ 2 ⎥ ⎢2 4 5 6⎥⎥ ⎢⎢ x2 ⎥⎥ ⎢⎢ 6 ⎥⎥ ⎢⎢4 2 5 5⎥⎥ ⎢⎢ x3 ⎥⎥
B = AC → ⎢ ⎥ = ⎢ → =
2 A=[1,2,3,4; 2,4,5,6; 5,4,2,5; 7,8,9,1] ⎢ 6 ⎥ ⎢5 4 2 5⎥ ⎢ x3 ⎥ ⎢ f1 ⎥ ⎢2 3 1 4⎥ ⎢ 0.1⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ (3)
3 B=[3,5,6,-3]’ ⎣ f 4 ⎦ ⎣7 8 9 1 ⎦ ⎣0.2⎦ ⎣ f 4 ⎦ ⎣8 9 7 1 ⎦ ⎣0.2⎦
4 tic, X1=(A^-1)*B, toc ⎡B ⎤ ⎡A A12 ⎤ ⎡C1 ⎤ B = A11C1 + A12C2
→ ⎢ 1 ⎥ = ⎢ 11 → 1
5 tic, X2=A\B, toc ⎣B 2 ⎦ ⎣ A 21 A 22 ⎥⎦ ⎢⎣C2 ⎥⎦ B 2 = A 21C1 + A 22C 2

6 X2-X1
Matlab provides a very useful extension to matrix subscript operations that is invaluable here. The Matlab
7 D1=1:3 subscript notation, A(f,g) will give an element at row f column g if f and g are scalars. In the case
8 D2=0.2:0.1:0.9 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
9 A(:,2) 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
2. Matrix partitioning rather convoluted way of defining g but it does show some how the null operator [] can delete elements in
The solution of system defined in equation (2) requires partitioning of the matrix A. In line 2 define the 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
known matrices. The (;) acts as a separator between Matlab statements that also suppress any output to the and 9 reassembles the complete B vector and C vectors. Matlab function zeros(4,1) create a vector (4
command window of the command that precedes it. First we solve for C1 . Line 3 define the submatrices of 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 1 Dr N.A.Alexander Last printed 31/01/2006 13:05:00 2 Dr N.A.Alexander
in B that are defined by vector f. Thus Matlab’s advanced matrix subscript notation can be used to contain a main function that calls a sub-function CalcR that returns the rotational transformation matrix R,
abstract submatrices from a larger matrix and assign submatrices within a larger matrix (as is the case here). equation (5).

## Example3.m ⎡cos θ − sin θ 0 0 ⎤

⎢ sin θ cos θ 0 0 ⎥⎥
1 clc; clear all;
R=⎢ (5)
2 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]; ⎢ 0 0 cos θ − sin θ ⎥
⎢ ⎥
3 f=[2,3], g=1:4, g(f)=[] ⎣ 0 0 sin θ cos θ ⎦

4 A11=A(f,f), A12=A(f,g) Note that in line 4, the data theta is copied into variable t in the function and on returning from the
5 A21=A(g,f), A22=A(g,g) function R is copied into R1.
6 C1=A11\(B1-A12*C2)
Example4.m
7 B2=A21*C1+A22*C2
1 function main
8 B=zeros(4,1); B(f)=B1, B(g)=B2
2 clc; clear all; Calling function
9 C=zeros(4,1); C(f)=C1, C(g)=C2
3 theta=pi/4
10 B-A*C % solution consistency check
4 [R1]=CalcR(theta) Input (data sent to function)
5 Output (data returned by function)

Question (1) 6
Create a Matlab program that is able to solve system (4) for the unknowns when F = [2,3,4,5] . Code a
T 7 function [R]=CalcR(t) Function
definition
consistency check. 8 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;
⎡ f1 ⎤ ⎡ 5 1 0 − 1⎤ ⎡ u1 ⎤
⎢f ⎥ ⎢ 1 6 2 0 ⎥⎥ ⎢⎢u 2 ⎥⎥
F = KU → ⎢ 2 ⎥ = ⎢ (4)
⎢ f3 ⎥ ⎢ 0 2 7 2 ⎥ ⎢u 3 ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥
⎣ f 4 ⎦ ⎣− 1 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
Question (2) coordinates you can compute its length L and angle theta. Hence, using the function CalcR in the
Create a Matlab program that is able to solve system (4) for the unknowns when F = [ f1, f 2 ,4,5]T and previous section, write the function that returns the bar element stiffness matrix using the following matrix
U = [0.1,0.2, u3 , u4 ]T . Code a consistency check.
triple product. You must also supply the axial rigidity EA

⎡1 0 − 1 0⎤
⎢ 0 0 0⎥⎥
EA ⎢ 0
K = RkR Τ , k= (6)
Question (3) L ⎢− 1 0 1 0⎥
⎢ ⎥
Create a Matlab program that is able to solve system (4) for the unknowns when F = [1, f 2 ,4, f 4 ]T and ⎣0 0 0 0⎦
U = [u1 ,0.2, u3 ,2]T . Code a consistency check.
5. Plotting graphs

In example4.m an element is displayed using the Matlab plot function. In line 3 vector U contains the
4. Matlab Functions nodal coordinates (x,z); node 1 is (0,0) and node 2 is (2,3). The plot expect to receive all the x coordinates
Matlab functions can be stored in separate Matlab m-files, however in these notes it is suggested that all the in one vector and all the z coordinate is another vector so x and z are defined in line 3 by abstracting the
code is contained in one m-file. Matlab requires a principle function (like in the case of VB) which is appropriate elements of U. In line 4, the plot function string ‘-bo’ define a continous blue line with
conventionally named main in these notes but can be given any name (starting with a letter). Example4.m 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 3 Dr N.A.Alexander Last printed 31/01/2006 13:05:00 4 Dr N.A.Alexander
line (bar element). Matlab num2str(a) function converts the data a into a string. This is required by the Example5.m
text function. 1 function FEAmain
Example4.m 2 clc; clear all;
1 clc; clear all; 3 [NN,NXY,NE,ENC,EA]=TestStructure
2 ElemNum=3; 4 for i=1:NE
3 U=[0,0,2,3], x=U([1,3]), z=U([2,4])
Bits you • You will need to abstract the nodal coordinate of the ith element
4 plot(x,z,'-bo','lineWidth',3);
will need • Create a function PlotElement(U,ElemNum) that plot a single element defined by a
5 title('Element plot'); xlabel('x [m]'); ylabel('z [m]'); to add coordinate vector U (see example4.m) and element number ElemNum
6 text(mean(x),mean(z),num2str(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
end
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. Question (6)
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 The next stage in a FE program is to take all the element stiffness matrices K{i} and assemble the structure
contain the axial rigidity of the ith bar element. 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).
Number of Nodes NN = 4 Fs = K s U s (7)
3
⎢ 2 0⎥
N xy = ⎢ ⎥ Loading vector FS in equation (7) needs to be defined. It is suggested that matrix LD is employed. Each row
⎢0 1 ⎥ of LD contains, in the first column, the node number at which loading is applied, then in second and third
1m 2 ⎢ ⎥ columns to force in the x and z directions respectively. Definition of support restraints requires the
3 5 ⎣2 1⎦
Number of Elements NE = 5 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
Element Nodal ⎡1 2 ⎤
Connections ⎢1 3 ⎥ numbered (2i − 1, 2i ) e.g. node 3 contains dofs 5 and 6.
1 2 ⎢ ⎥
1 ENC = ⎢1 4⎥ Number of Nodes NNL = 2
z ⎢3 4⎥ 3
1kN Loads ⎡2 − 2 − 3⎤
⎢⎣2 4⎥⎦ LD = ⎢
2m ⎣4 1 0 ⎥⎦
x Element EA = [1 1 1 1 1]
Axial rigidities Number of degree of NR = 3
global axes freedom restraints
(Supports)
2
2kN Displacement at ⎡1 0⎤
RD = ⎢⎢5 0⎥⎥
1 restraint dofs (0 is a
Question (5) 3kN fully rigid support)
Create a Matlab program that plots the entire structure. You must create a function TestStructure that ⎢⎣6 0⎦⎥
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.

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

• 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 ⎤ ⎡ K K 12 ⎤ ⎡U R ⎤ FR = K 11U R + K 12 U F → sub U F find FR

FS = K S U S → ⎢ R ⎥ = ⎢ 11 ⎥ ⎢ U ⎥ → (F − K U ) = K U → solve for U (8)
⎣ F ⎦ ⎣ 21
F K K 22 ⎦ ⎣ F ⎦ F 21 R 22 F F

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

• 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 clc; clear all; disp(' ');
2 N=15; A=rand(N,2); B=ones(N,1);
3 t=' i A(i,1) A(i,2) B(i)'; disp(t);
4 for i=1:N
5 t=sprintf('%5g %14g %14g %14g',i,A(i,1:2),B(i)); disp(t);
6 end