# MatLab Programming and Finite Element Method (MP&FEM

)
PhD. Bogdan DOBRICĂ bogdan@3nanosae.org University of Bucharest – spring 2013

MP&FEM Structure

MatLab introduction. How to write basic algorithms. Matrix operations. Linear programming. Ordinary Differential Equations (ODEs). Euler and Taylor methods. Built-in functions. Partial Differential Equations (PDEs). Finite Difference Methods. Finite Element Methods. Finite Volume Methods. Built-in functions.

MP&FEM 4
Partial Differential Equations (PDEs). Finite Difference Methods. Finite Element Methods.

PDE = Partial Differential Equation

Given a natural number n, a function y:Rn → R, y((xn)n), a PDE is the following equation:

f(x1,...,xn,∂y/∂x1,...,∂y/∂xn,∂2y/∂x12,∂2y/∂x1∂x2,.. .) = 0

We will study (with general methods) the heat equation, T(t,x1,...,xn):RxRn → R : ∂T/∂t = k∆T

1-D Heat Equation. Example

T(t,x) ↑

L(m)

1-D Heat Equation. Exercise 1
Given a harmonic surface temperature variation: T(t,0) = 15 – 10 sin (2πt/12) (°C) Find the temperature as a function of depth and time in a 100m thick rock with infinite surface. The heat conduction of the rock is 10-6 m2/s and at the bottom there's no heat transfer.

1-D Heat Equation. Finite Differences
∂T/∂t = ∂2T/∂x2 (1D)

To solve the (1D) equation we will use the following approximations: t is discrete i.e. t : {1,...,n} → R, t(i):=ti↗ x is discrete i.e. x : {1,...,m} → R, x(j):=xj↗ T(t,x):=T(ti,xj):=T(i,j):=Tij, i=1,n;j=1,m ∂T(t,x)/∂t=(T(ti+1,x)-T(ti,x))/(ti+1-ti) = (Ti+1jTij)/(ti+1-ti)

1-D Heat Equation. Finite Diff. (2)

∂T/∂x = T(t,xj+1)-T(t,xj)/(xj+1-xj)=(Tij+1-Tij)/ (xj+1-xj) ∂2T/∂x2=((Tij+1-Tij)/(xj+1-xj)-(Tij-Tij-1)/(xj-xj-1))/ (xj+1-xj) The (1D) equation becomes: (Ti+1j-Tij)/(ti+1-ti) = k ((Tij+1-Tij)/(xj+1-xj)-(Tij-Tij-1)/(xj-xj-1))/(xj+1-xj) Making ti+1-ti=∆t, xj+1-xj=∆x (1D) becomes:

1-D Heat Equation. Finite Diff. (3)
bounda ry conditi ons x=x1 x→ Missing data for t = t1 Tij-1 Tij Ti+1j Tij+1 bounda ry conditi ons x=xm

t ↓

Ti1 = Tim-Timf(i) =0 1 Using known values (yellow & green) we can compute missing values (red). T1j is missing (and cannot be computed) so a good approach is to just consider the material uniformly heated (T1j = ct, for all j) and repeat the process until the differences between solutions are small enough, using for the new T1j the previously

1-D Heat Eq. MatLab (Partial) Solution
T = 15 * ones (n,m); % uniformly heated body, 15°C T(1,:) = ...; % set the 1st boundary condition. Choose your function :)! for repeat = 1:500; % try to repeat 500 times TL = T; % save the last computed temperature T(1,:)=TL(end,:); % set T1j as the previous Tnj ; end is a special variable for i = 2:m; % cycle to fill in the missing matrix values T(i,2:end-1) = T(i-1,2:end-1) + dt*k*(T(i-1,1:end-2)2*T(i-1,2:end-1)+T(i-1,3:end))/(dx^2); % (1D) T(i,end)=T(i,end-1); end % 2nd boundary condition

1-D Heat Eq. Useful MatLab functions

Plotting a heatmap:

>> imagesc ([begin_t end_t], [begin_x end_x], T)

Plotting a contoured heatmap: Drawing a color-to-temperature scale:

>> contourf (t_vector, x_vector, T)

>> colorbar();

1-D Stationary Heat Eq. Exercise 2
Consider a L ( = 2m) length metal rod with conductivity k ( = 5 W/m°C) and crosssection A ( = 0.01m2 ), in air at 0°C that is heated by a constant heat flux at one end q ( = 15 W/m2). Find the temperature T distribution on the rod.
0°C q A L x

k

1-D Stationary Heat Equation (1)

Deriving the weak formulation from the strong formulation below:
d dT  Ak Q = 0 dx dx

1-D Stationary Heat Equation (2)

The basic idea is to use the following equivalence:
d dT  Ak Q = 0 dx dx
ax = bx ∀ x ⇔ a = b

1-D Stationary Heat Equation (3)

Multiplying the strong formulation with an almost (why?) arbitrary function v : R→R: d dT  Ak Q = 0 dx dx d dT d dT  Ak Q =0 ⇔ v  x   Ak  Q =0 ∀ v dx dx dx dx

1-D Stationary Heat Equation (4)

And then integrating over the definition domain:

d dT  Ak Q = 0 dx dx β d dT d dT ( Ak )+ Q =0 ⇔∫α v ( ( Ak )+ Q ) dx = 0 ∀v dx dx dx dx

1-D Stationary Heat Equation (5)

Using integration by parts (higher dimensions requires Green's identities):

β d dT d dT ( Ak )+ Q =0 ⇔∫α v ( ( Ak )+ Q ) dx = 0 ∀v dx dx dx dx β β dv d dT dT β dT ∫α v dx ( Ak dx ) dx = v ( Ak dx )∣α – ∫α dx Ak dx dx

1-D SHE: Weak formulation

We obtain the weak formulation below:

β d dT d dT ( Ak )+ Q =0 ⇔∫α v ( ( Ak )+ Q ) dx = 0 ∀v dx dx dx dx β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

1-D SHE: Finite Element Approach (1)
A k

x0

xi

xj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

1-D SHE: Finite Element Approach (2)
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

̄n , x 0= 0, x n= L x i ∈[ 0, L ] , ∀i =0, T i =T  x i  ∀ i , T x = T  x ∀ x v i = v  x i  ∀ i , v x = v  x ∀ x

1-D SHE: FEM – Linear Approximation
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

T j–Ti T j x i T i x j T x= x– , ∀ x ∈[ x i , x j ] x j – xi x j− xi

1-D SHE: FEM - Linear Approximation
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

x j− x x − xi T x=T i  T j   , ∀ x ∈[ x i , x j ] x j− xi x j − xi

1-D SHE: FEM → Linear Algebra (1)
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

x j− x x − xi T T x = N a , N =[ ] , a =[ T i T j ] x j − xi x j − x i

1-D SHE: FEM → Linear Algebra (2)
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx x j− x x − xi T T x = N a , N =[ ] , a =[ T i T j ] x j − xi x j − x i

v x =V c , V =[ V 0 V 1 ... V n ] , c =[ c 0 c1 ... c n ]

T

1-D SHE: FEM → Linear Algebra (3)
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx
x d x d d x T T T T c V Ak N a ∣x −∫x c V Ak N a dx ∫x c V Qdx = 0 dx dx dx T T
j j j i i i

1-D SHE: FEM → Linear Algebra (4)
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

x d x d d x T T V Ak N a ∣x −∫x V Ak N a dx ∫x V Qdx =0 dx dx dx T
j j j i i i

1-D SHE: FEM → Linear Algebra (5)
A k

x0

Ti

xi

xj

Tj

xn

β dv β dT β dT v ( Ak )∣α −∫α Ak dx + ∫α vQdx = 0 dx dx dx

x d x d d x T T N Ak N a ∣x −∫x N Ak N a dx ∫x N Qdx = 0 dx dx dx T
j j j i i i

1-D SHE: FEM → Linear Algebra (6)
A k

x0

Ti
j j

xi

xj

Tj

xn

x d x d d x T T N Ak N a ∣x −∫x N Ak N a dx ∫x N Qdx = 0 dx dx dx T
j i i i

K a= f

1-D SHE: FEM → Linear Algebra (7)
A k

x0

Ti
j j

xi

xj

Tj

xn

x d x d d x T T N Ak N a ∣x −∫x N Ak N a dx ∫x N Qdx =0 dx dx dx K a= f T
j i i i

d d x j− x x− xi −1 1 1 N= [ ]=[ ]= [−1 1 ] dx dx x j − x i x j − x i x j− x i x j− xi x j− xi

1-D SHE: FEM → Linear Algebra (8)
A k

x0

Ti
j j

xi

xj

Tj

xn

x d x d d x T T N Ak N a ∣x −∫x N Ak N a dx ∫x N Qdx =0 dx dx dx K a= f T
j i i i

x Ak 1 −1 1 −1 K= = Ak ∫ x x j − xi −1 1 −1 1
j i

[

] [

]

1-D SHE: FEM → Linear Algebra (9)
A k

x0

Ti
j j

xi

xj

Tj

xn

x d x d d x T T N Ak N a ∣x −∫x N Ak N a dx ∫x N Qdx =0 dx dx dx K a= f dT d q =−k =−k Na dx dx x x T T f =− N Aq ∣x ∫x N Qdx T
j i i i

j

j

i

i

1-D SHE: FEM → Linear Algebra (10)
A k

x0

Ti
j j

xi

xj

Tj

xn

x d x d d x T T N Ak N a ∣x −∫x N Ak N a dx ∫x N Qdx =0 dx dx dx T
j i i i

K a= f

−1 Q  x j − x i  1 f =− Aq  1 2 1

[ ]

[]

1-D SHE: FEM → Linear Algebra (11)
A k

x0

Ti

xi

xj

Tj

xn

q i 0 −1 Q  x j − x i  1 1 − 1 Ak a =− A  2 − 1 1 1 1 0 q j j
i

[

]

[ ][ ]

[]

i

j

You can find the MatLab solution on 3Nano-SAE webpage: http://www.3nanosae.org/staff-and-members/bogdan-dob

Conclusions

Solving PDEs require a good mathematical background! PDEs are important tools for physicians. Solving PDE require attention to details and understanding simple discretisation techniques that reduce complex differential equations to linear algebra operations. MatLab proves to be quite useful to solve PDEs numerically.