1dfem implementation example: % example_1d.m files clear n = 7; % no interior points h = 1/(n+1); a...
TRANSCRIPT
1DFEM Implementation
0)1()0(
')''(
uu
fcubuau
2)( xxa
0)( xb1)( xc
33)( 2 xxxf
Example:
MM
MM
ff
ff
ff
ff
hb
1
12
32
21
2
4100
1
00
41
0014
6
hM
121
1
211
110
00
00
00
1K
MMM
M
aaa
a
aaa
aaa
h
% example_1D.m filesclearn = 7; % no interior pointsh = 1/(n+1);a = inline('x+2','x');c = inline('1','x');f = inline('-x^2+5*x+3','x');uexact = inline('x*(1-x)','x'); K = sparse(n,n);M1 = sparse(n,n);L = sparse(n,1);x = h:h:1; i=1;K(i,i)=(a((x(i)+x(i+1))/2)+a(x(i)/2))/h;i=n;K(i,i)=(a((x(i)+x(i+1))/2)+a((x(i)+x(i-1))/2))/h;for i=2:n-1 K(i,i)=(a((x(i)+x(i+1))/2)+a((x(i)+x(i-1))/2))/h;end for i=1:n-1 K(i,i+1) = -a( (x(i)+x(i+1))/2 )/h; K(i+1,i) = K(i,i+1);end
v = ones(n, 1) ;M1 = diag(4*v,0)+diag(v(1:n-1),-1)+diag(v(1:n-1),1);M = (h/6)*M1; A = K + M; L(1) = h*(f((x(1))/2)+f((x(1)+x(2))/2))/2;L(n) = h*(f((x(n)+x(n-1))/2)+f((x(n)+1)/2))/2;for i=2:n-1 L(i) = f((x(i)+x(i-1))/2)+f((x(i)+x(i+1))/2); L(i) = h*L(i)/2;endU = A\Luexact(h)Usol = [0;U;0];xnodes = [0;x'];ezplot('x*(1-x)',[0,1]);hold onplot(xnodes,Usol,'r-o')grid onhold off
Local Basis function
ix 1ix1ix
i
iIf we restrict any global basis functionon an interval Kj then it becomes eitherthe zero function or one of these local basis functions
1jxjx
Kj,1
1jxjx
Kj,2
1)( 1,1 jK xj
0)(,1 jK xj
21,1 )( cxcxjK
1211 cxc j
021 cxc j
jhc 1
1
j
j
h
xc 2
)()( 1,1 j
jhK xxxj
)()( 11
,2 jjhK xxx
j
1DFEM Implementation
5.0 55.0 6.0 8.0 0.12.0 4.0 45.00.0Partition of the interval [0,1]
]0.1 ,8.0 ,6.0 ,55.0 ,5.0 ,45.0 ,4.0 ,2.0 ,0[p Node Coordinate vector
Global labeling (nodes)
5.0 55.0 6.0 8.0 0.12.0 4.0 45.00.0
1 2 3 4 5 6 7 8 9
Element labeling (interval)1 2 3 4 5 6 7 8
Local labeling (nodes)
5.0 55.0 6.0 8.0 0.12.0 4.0 45.00.01 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
2 4 6 8
1 3 5 7
98765432
87654321tG
loba
l-loc
al
labe
ling
mat
rix
1st e
lem
2ed
elem
3ed
elem
8th e
lem
Global label of the first node
Global label of the second node
Example: Find the coordinate of the 2ed node in the fifth element
))5,2((tp
Three Matrices
5.0 55.0 6.0 8.0 0.12.0 4.0 45.00.0Partition of the interval [0,1]
]0.1 ,8.0 ,6.0 ,55.0 ,5.0 ,45.0 ,4.0 ,2.0 ,0[pNodes Coordinate vector
98765432
87654321tGlobal local labeling Matrix
Boundary nodes vector ]9,1[e
dxaa jijiji )''(),(
dxdxa jiji ''
A K MMass Matrixstiffness Matrix
Assemble the Mass Matrix
dxjijiM ,
Mass Matrix
9999939291
39333231
19232221
19131211
),(),(),(),(
),(),(),(),(
),(),(),(),(
),(),(),(),(
dxuu ),(
5.0 55.0 6.0 8.0 0.12.0 4.0 45.00.0n=9 # nodesM=8 # elements
xdxdxd 45.0
4.0
32
4.0
2.0
32
2.0
0
32
xd1
0
32
Contribution from K1
Contribution from K2
Contribution from K3
xdxdxd KKKK 45.0
4.0
3,1
4.0
2.0
2,22,1
2.0
0
1,2 00
xdxd 6.0
55.0
66
55.0
5.0
66 xd1
0
66
Contribution from K5
Contribution from K6
xdxd KKKK 6.0
55.0
6,26,2
55.0
5.0
5,15,1
Each entries of the matrix is a sum of several Contribution from elements. These contributions are integrals of two local basis functions in the same element
Element labeling (interval)1 2 3 4 5 6 7 8
1,2 K1,1 K
2,2 K2,1 K
8,2 K8,1 K
We generate all possible contributions in each element (this is called local mass matrix)
1
11
1
11
1
11
1
11
,2,2,1,2
,2,1,1,1
K
KK
K
KK
K
KK
K
KK
dxdx
dxdx
9
99
9
99
9
99
9
99
,2,2,1,2
,2,1,1,1
K
KK
K
KK
K
KK
K
KK
dxdx
dxdx
How many integrations ??
32 24
Assemble the Mass Matrix
Local Basis function
1jxjx
)()( ,11 xxx
jKjjh
)()( 11
,2 jjhK xxx
j
j
j
jj
j
j
jj
j
j
jj
j
j
jj
x
x
KK
x
x
KK
x
x
KK
x
x
KK
dxdx
dxdx
11
11
,2,2,1,2
,2,1,1,1
j
j
jj
x
x
KK dx1
,1,2
jh6
1
j
x
x
KK hdxj
j
jj 3
1,1,1
1
jh
x
x
KK
j
j
jjdx
3
1,2,2
1
21
12
6jh
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
5.0 55.0 6.0 8.0 0.12.0 4.0 45.00.0
15
1
30
1
30
1
15
1
2.01 h
1 2
1
2
1 2 3 4 5 6 7 8 9
1
2
3
4
5
6
7
8
9
60
1
120
1
120
1
60
1
05.03 h
347
36
4
15/1
15/130/1
30/1
60/160/1120/1
120/1
15
1
30
1
30
1
15
1
2.02 h
21
32
2
3
15/1
15/130/1
30/1
60
1
120
1
120
1
60
1
05.03 h
457
46
5
60/160/1120/1
120/1
21
12
6jh
1DFEM Implementation
% main.m file
p=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1];t=[1:8; 2:9];e=[1,9];[M] = createM(p,e,t)
21
12
6jh
% createM.m file function [M] = createM(p,e,t)n = length(p); % number of nodesm = size(t,2); % number of elementsM = sparse(n,n); for j=1:m loc2glb = t(1:2,j); % global labeling for the nodes in Kj xcoord = p(loc2glb); % coordinates of the nodes hj = xcoord(2)-xcoord(1); l_mass = (hj/6)*[2, 1; ... 1, 2]; % local mass matrix M(loc2glb,loc2glb) = M(loc2glb,loc2glb) + l_mass; % Add contributionsend
Stiffness Matrix
1jxjx
j
j
jj
j
j
jj
j
j
jj
j
j
jj
x
x
KK
x
x
KK
x
x
KK
x
x
KK
dxadxa
dxadxa
11
11
''''
''''
,2,2,1,2
,2,1,1,1
j
j
jj
x
x
KK dxa1
'' ,1,2
j
j
x
xjh
dxxa1
2 )(1)
2(
1,1,1
1
1
''
jj
j
j
j
jj
xxa
h
x
x
KK dxa
11
11
)2
(1 1jj
j
xxa
h
dxa ji ''jiK ,
Stiff Matrix
)2
(1 1
jj xxa
jh)
2(
1,2,2
1
1
''
jj
j
j
j
jj
xxa
h
x
x
KK dxa
)()( ,11 xxx
jKjjh
)()( 11
,2 jjhK xxx
j
% main.m file
p=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1];t=[1:8; 2:9];e=[1,9];[K] = createK(p,e,t)
Stiffness Matrix
11
11)
2(
1 1jj
j
xxa
h
% createK.m filefunction [K] = createK(p,e,t)n = length(p); % number of nodesm = size(t,2); % number of elementsa = inline('x+2','x');K = sparse(n,n); for j=1:m loc2glb = t(1:2,j); % global labeling for the nodes in Kj xcoord = p(loc2glb); % coordinates of the nodes hj = xcoord(2)-xcoord(1); xb = (xcoord(1)+xcoord(2))/2; ab = a(xb); l_stif = (ab/hj)*[1, -1; ... -1, 1]; % local stiffness matrix K(loc2glb,loc2glb) = K(loc2glb,loc2glb) + l_stif; % Add contributionsend
Load vector
1jxjx
j
j
j
j
j
j
x
x
K
x
x
K
dxf
dxf
1
1
,2
,1
j
j
j
x
x
K dxf1
,1
dxf i
ibStiff Matrix
)2
(,1)2
(11 jj
j
jj xx
K
xx
j fh
)2
(2
1 jj xxjhf
j
j
j
x
x
K dxf1
,1
)2
(2
1 jj xxjhf
1
1)
2(
21jj xxjh
f
Local Load vector
)()( ,11 xxx
jKjjh
)()( 11
,2 jjhK xxx
j
% createL.m file
function [L] = createL(p,e,t)n = length(p); % number of nodesm = size(t,2); % number of elementsf = inline('-x^2+5*x+3','x');L = sparse(n,1); for j=1:m loc2glb = t(1:2,j); % global labeling for the nodes in Kj xcoord = p(loc2glb); % coordinates of the nodes hj = xcoord(2)-xcoord(1); xb = (xcoord(1)+xcoord(2))/2; fb = f(xb); l_L = (hj/2)*fb*[1; 1]; % local load vector L(loc2glb) = L(loc2glb) + l_L; % Add contributionsend
% main.m file
p=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1];t=[1:8; 2:9];e=[1,9];[L] = createL(p,e,t)
1
1)
2(
21jj xxjh
f
Load vector
0)1()0(
)''(
uu
fuau
Wea
k
)( )(),(
such that )( Find10
10
HLua
Hu
Boundary Conditions
}0)1()0(:)({)( 110 vvHvH
0)1(')0(
)''(
uu
fuau
Wea
k
HLua
Hu
)(),(
such that Find
}0)0(:)({ 1 vHvH)(1
0 HSh HSh
Example:1
0
Consider the partition
13
1
3
2},{span 21 hS
2
},,{span 321 hS
12 3
]4,1[e]1[e
1 2 3 4
Boundary Conditions
A K MMass Matrixstiffness Matrix
LAU
impose boundary conditions
5.0 55.0 6.0 8.0 0.12.0 4.0 45.00.0
1 2 3 4 5 6 7 8 9
0)0( u 0)1( u
We need to remove contributions coming from these two red local functions
for i=1:length(e) ib = e(i); A(ib,:)=sparse(1,n); A(:,ib)=sparse(n,1); A(ib,ib)=1; L(ib)=0;end
LAU
9
8
2
1
9
8
2
1
99
32
232221
1211
98
898887
78
0
0
L
L
L
L
U
U
U
U
aa
aaa
a
a
aaa
aa
1 0
0
10
0 0
0
Boundary Conditions% non uniform meshp=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1]; t=[1:8; 2:9];e=[1,9]; % % uniform mesh% p=0:0.125:1; n = length(p); t=[1:n-1;2:n];% e=[1,n]; n = length(p); % number of nodesm = size(t,2); % number of elementsuexact = inline('x*(1-x)','x'); [M] = createM(p,e,t)[K] = createK(p,e,t)[L] = createL(p,e,t)A = K + M; for i=1:length(e) ib = e(i); A(ib,:)=sparse(1,n); A(:,ib)=sparse(n,1); A(ib,ib)=1; L(ib)=0;endU = A\L;ezplot('x*(1-x)',[0,1]);hold on; plot(p,U,'r-o'); grid on; hold off
Impose boundary conditions
Uniform Mesh
Uniform mesh (using 8 elements)
Non-uniform mesh (using 8 elements)
Higher Order Polynomial
a more general finite element space, consisting of piecewise polynomials of degree r − 1, where r is an integer ≥ 2, with the above piecewise linear case included for r = 2, thus
010)( 1 )v(),v( on each K : vCv S jr-h
Πk denotes the space of polynomials of degree ≤ k.
Example:
r = 2 Sh space of cont pw-linear
Example: (HW)
r = 3 Sh space of cont pw-quadratic
1 2 3 4 5
322
1)( cxcxcx 322
1)( cxcxcx
Example: r = 4 Sh space of cont pw-cubic
432
23
1)( cxcxcxcx
1jx jxjj hx3
11
jj hx3
21
0)(
0)(
0)(
1)(
1
3
211
3
111
11
j
jj
jj
j
x
hx
hx
x
0)(
0)(
1)(
0)(
2
3
212
3
112
12
j
jj
jj
j
x
hx
hx
x
0)(
1)(
0)(
0)(
3
3
213
3
113
13
j
jj
jj
j
x
hx
hx
x
1)(
0)(
0)(
0)(
4
3
214
3
114
14
j
jj
jj
j
x
hx
hx
x
),(),(),(),(
),(),(),(),(
),(),(),(),(
),(),(),(),(
44434241
34333231
24232221
14131211
Local mass matrix
)','()','()','()','(
)','()','()','()','(
)','()','()','()','(
)','()','()','()','(
44434241
34333231
24232221
14131211
Local stiffness matrix
432
23
1)( cxcxcxcx
Higher Order Polynomial
1 2 3
1 2 3 4 5 6 7 8 9
****
****
****
****
1 2
1
2
10
3 4
3
4
****
****
****
****
4 5
4
5
6 7
6
7
****
****
****
****
7 8
7
8
910
93
10
Remark: The matrix is not tridiagonal
Theorem
r
rh vChvvI
0
r
rh vChvvI 1
1
)(for rHv
Approximation properties of Sh
r
rh uChuu 1
1
Theorem 5.1+5.2:
r
rh uChuu
0
)( rHu
432
23
1~~~~)(
~ctctctcx
1jx jxjj hx3
11
jj hx3
21
0)(
0)(
0)(
1)(
1
3
211
3
111
11
j
jj
jj
j
x
hx
hx
x
0)(
0)(
1)(
0)(
2
3
212
3
112
12
j
jj
jj
j
x
hx
hx
x
0)(
1)(
0)(
0)(
3
3
213
3
113
13
j
jj
jj
j
x
hx
hx
x
1)(
0)(
0)(
0)(
4
3
214
3
114
14
j
jj
jj
j
x
hx
hx
x
Reference element
1jx jxjj hx3
11
jj hx3
21
0 13
1
3
2
Reference element
mapping )( 11
jhxxt
j
0)1(~
0)(~
0)(~
1)0(~
1
3
21
3
11
1
0)1(~
0)(~
1)(~
0)0(~
2
3
22
3
12
2
0)1(~
1)(~
0)(~
0)0(~
3
3
23
3
13
3
1)1(~
0)(~
0)(~
0)0(~
4
3
24
3
14
4
0
0
0
1
~
~
~
~
1111
1
1
0000
4
3
2
1
3
2
9
4
27
83
1
9
1
27
1
c
c
c
c
0
0
0
1
~
~
~
~
0001
12
99
2
112
918
2
459
2
9
2
27
2
27
2
9
4
3
2
1
c
c
c
c
1
9
~
~
~
~
2
11
2
9
4
3
2
1
c
c
c
c
092
452
27
02
918
2
27
012
92
9
1
~2
~ 3
~ 4
~
Example: r = 4 Sh space of cont pw-cubic
432
23
1)( cxcxcxcx
1jx jx
0)('
0)(
0)('
1)(
1
1
11
11
j
j
j
j
x
x
x
x
),(),(),(),(
),(),(),(),(
),(),(),(),(
),(),(),(),(
44434241
34333231
24232221
14131211
Local mass matrix
)','()','()','()','(
)','()','()','()','(
)','()','()','()','(
)','()','()','()','(
44434241
34333231
24232221
14131211
Local stiffness matrix
432
23
1)( cxcxcxcx
C element1
0)('
0)(
1)('
0)(
2
2
12
12
j
j
j
j
x
x
x
x
0)('
1)(
0)('
0)(
3
3
13
13
j
j
j
j
x
x
x
x
1)('
0)(
0)('
0)(
4
4
14
14
j
j
j
j
x
x
x
x
Example:
432
23
1)( cxcxcxcx
1jx jx
0)('
0)(
0)('
1)(
1
1
11
11
j
j
j
j
x
x
x
x
C element .vs. C element1
0)('
0)(
1)('
0)(
2
2
12
12
j
j
j
j
x
x
x
x
0)('
1)(
0)('
0)(
3
3
13
13
j
j
j
j
x
x
x
x
1)('
0)(
0)('
0)(
4
4
14
14
j
j
j
j
x
x
x
x
432
23
1)( cxcxcxcx
1jx jxjj hx3
11
jj hx3
21
0)(
0)(
0)(
1)(
1
3
211
3
111
11
j
jj
jj
j
x
hx
hx
x
0)(
0)(
1)(
0)(
2
3
212
3
112
12
j
jj
jj
j
x
hx
hx
x
0)(
1)(
0)(
0)(
3
3
213
3
113
13
j
jj
jj
j
x
hx
hx
x
1)(
0)(
0)(
0)(
4
3
214
3
114
14
j
jj
jj
j
x
hx
hx
x
0 2
11
1C
0C
All global basis functions are continuous (only)
All global basis functions and their first derivatives are continuous
',
0)1()0(
''
uu
fu
Wea
k
)( )(),(
such that )( Find10
10
HLua
Hu
4th order differential equation
}0)1()0(:)({)( 110 vvHvH
0)1(')1()0(')0(
''''
uuuu
fu
Wea
k
)( )(),(
such that )( Find20
20
HLua
Hu
}0)1()0(:)({ 220 vvHvH
)(10 HSh
20HSh
1C
0C
It is required to use
It is required to use
element element