Professional Documents
Culture Documents
LAB1 Report OD
LAB1 Report OD
Report
Demont Oscar
1
Contents
1 FEM for Elliptic equations 3
1.1 Task 1a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Task 1b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Task 1c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
A Appendices Part 1 16
A.1 Matlab codes for Task 1a . . . . . . . . . . . . . . . . . . . . . . 16
A.2 Matlab codes for Task 1b . . . . . . . . . . . . . . . . . . . . . . 17
A.3 Matlab codes for Task 1c . . . . . . . . . . . . . . . . . . . . . . 18
B Appendices Part 1 19
B.1 Matlab codes for task 2a . . . . . . . . . . . . . . . . . . . . . . . 19
2
1 FEM for Elliptic equations
1.1 Task 1a
−(a(x)u′ (x))′ = f (x), x ∈ [xl , xr ] (1)
with Robin boundary conditions :
Z xr Z xr
f v dx = (−(au′ (x))′ ) v dx (2)
xl xl
Z xr
= au′ (x)v ′ (x) dx + a(xr )u′ (xr )v(xr ) − a(xl )u′ (xl )v(xl ) (3)
xl
Z xr
= au′ (x)v ′ (x) dx + κr (u(xr ) − gr )v(xr ) + κl (u(xl ) − gl )v(xl ) (4)
xl
Variational formulation :
We have to find u ∈ V such that
Z Z
au′ (x)v ′ (x) dx + κL u(L)v(L) + κ0 u(0)v(0) = f v dx + κL gL v(L) + κ0 g0 v(0), ∀v ∈ V.
I I
3
Find uh ∈ Vh such that for all v ∈ Vh , we have :
Z Z
au′h (x)v ′ (x) dx + κxr uh (xr )v(xr ) + κxl uh (xl )v(xl ) = f (x)v(x) dx + κxr gxr v(xr ) + κxl gxl v(xl );
I I
n
X
uh = ξj φj .
j=0
Z
Aij = aϕ′j ϕ′i dx,
I
Rij = κr ϕj (xr )ϕi (xr ) + κl ϕj (xl )ϕi (xl ),
Z
bi = f ϕi dx,
I
ri = κr gr ϕi (xr ) + κl gl ϕi (xl ), ∀i, j ∈ {0, n} .
You can find the matlab code at the end of the report.
We can see that the Robin boundary conditions are respected, in fact uh (xl ) =
2 and uh (xr ) = 3. The code took 0.19 seconds to run.
4
1.2 Task 1b
Considering this equation
u(0) = u(2π) = 1.
We want to compute the code a couple of times to see the L2 − norm of the
error. First, we need to find the real solution. u(x) = cos(x) is a solution. In
fact u′′ (x) = −cos(x) and u(0) = u(2π) = 1. So we have our real solution.
Now, let’s find uh . With n=10, we have this solution:
We can see that the error between uh and the real solution cos(x) is visible.
Let’s check for N=20,40,80,160. For this, we can create an ”error-vector”, which
will have as its component the error between the real solution and the approx-
imate solution according to the number N of intervals. Then, we can compare
with the second convergence rate. But how can we calculate ∥u(x)−uh (x)∥L2 (I) ?
s
Z 2π
∥u(x) − uh (x)∥L2 (I) = (u − uh )2 dx
0
But, we have
Z 2π n Z
X xi
(u − uh )2 dx = (u − uh )2 dx
0 i=1 xi−1
5
Now, we can implement the code, and look at the error:
We can see thanks to the figure that the L2 -norm of the error verify the
second convergence rate. I compared logarithmically the error vector, where
each component corresponds to the error between the real solution and the
approximate solution for a different step h, that is to say an approximation of
the L2 -norm, with the vector h2 .
1.3 Task 1c
Considering the following equation:
We want to do an adaptive mesh refinement. You can find the code at the
end of the report.
We can look at severals solutions to see what are the consequences of α and m.
6
Figure 4: Adaptive mesh refinement with m=5, α = 0.9
7
Figure 6: Adaptive mesh refinement with m=5, α = 0.2
8
propensity for subinterval division, thereby instituting a more rigorous
refinement strategy. This leads to an increase in the number of subinter-
vals that satisfy the refinement criterion. Conversely, elevating α yields
a conservative refinement approach, resulting in fewer subdivisions. This
parameter effectively balances computational efficiency against the gran-
ularity of the mesh and, by extension, the potential resolution of the nu-
merical solution.
• Parameter m: The variable m, representing the number of adaptive refine-
ment cycles, directly influences the fidelity of the computed solution. In-
cremental iterations facilitate the mesh’s adaptation to regions exhibiting
pronounced error, ostensibly enhancing solution accuracy. However, there
exists a threshold beyond which additional refinement does not propor-
tionately improve accuracy but incurs significant computational overhead.
Thus, an optimal balance must be sought to avoid the diminishing returns
associated with excessive mesh refinement.
2.1 Task 2a
Let’s start by derive the variational formulation of the parabolic problem:
Now, we can use the Green’s Formula to simplify the second term on the left-
hand size of the previous equation.
Z Z Z
− v∇ · a∇u dx = a∇u · ∇v dx − v(a∇u · n) dS.
Ω Ω δΩ
9
But we know that v=0 on δΩ. In fact v ∈ V0 , where
V0 = v ∈ V | v|δΩ = 0
And
V = v | ∥v∥L2 (Ω) + ∥∇v∥L2 (Ω) < ∞
So, at the end we have: Find u ∈ V0 such that
Z Z Z
ut v dx + a∇u · ∇v dx = f v dx, ∀v ∈ V0
Ω Ω Ω
ni Z ni Z Z
˙
X X
⇔ ξj (t) φi φj dx + ξj (t) a∇φi φj dx = f φi dx, ∀i ∈ {1, 2..., ni }
j=1 Ω j=1 Ω Ω
Now, we have
˙ + Aξ(t) = b(t), t ∈ [0, T ]
M ξ(t) (12)
Z Z Z
with Mij = φj φi dx; Aij = a∇φi ∇φi dx; bi = f φi dx, ∀i, j ∈ {1, ..ni }
Ω Ω Ω
10
Let’s prove that
Z t
∥uh (t, ·)∥Ω ≤ ∥uh (0, ·)∥Ω + ∥f (τ, ·)∥Ω dτ
0
Z Z Z
(uh )t uh dx + a∇uh ∇uh dx = f uh dx ≤ ∥f (τ, ·)∥Ω ∥uh (τ, ·)∥Ω
Ω Ω Ω
Z Z Z
∂uh 1 ∂ ∂ ∂ ∂
but uh dx = (uh )2 dx = u2h dx = ∥uh (τ, ·)∥2Ω = ∥uh (τ, ·)∥ ∥uh (τ, ·)∥
Ω ∂t 2 Ω ∂t ∂t Ω ∂t ∂t
Z
We also have a(x, y) > 0, so a∇uh ∇uh dx >= 0.
Ω
Then, we have
Z
∂
∥uh (τ, ·)∥ ∥uh (τ, ·)∥ + a∇uh ∇uh dx ≤ ∥f (τ, ·)∥Ω ∥uh (τ, ·)∥Ω .
∂t Ω
Then,
∂
∥uh (τ, ·)∥ ≤ ∥f (τ, ·)∥.
∂t
We can integrate on [0, t], t ∈]0, T ]
Z t Z t
∂
∥uh (τ, ·)∥ dτ ≤ ∥f (τ, ·)∥ dτ.
0 ∂t 0
Immediately, we have
Z t
∥uh (t, ·)∥ ≤ ∥uh (0, ·)∥ + ∥f (τ, ·)∥ dτ. (14)
0
˙ l ) ≃ ξl − ξl−1
ξ(t
kl
11
And with the intitial condition ξ0 = ξ(t0 ) = ξ(0) Now, we can create a linear
system:
ξl − ξl−1 M M
M + Aξl = bl , ⇔ ( + A)ξl = bl + ξξ .
kl kl kl l−1
You can find the code to resolve the solution at the end of the report.
We can see (thanks to function tic and toc on matlab) that the time loop
thanks approximatively 600seconds to be solved. It’s huge.
12
Figure 9: Figure of the solution at tl with l=174
2.2 Task 2b
In the previous method, I implemented the matrices before the time loop, be-
cause there is no need to recreate them. We just need to change the right-hand
side to solve the linear system. What can we do to win time and reduce cost
calculs?
The first idea is to factorise M
ht + A by LU factorisation. In fact, doing this:
M = MassAssembler2D ( q , me ) ;
A = S t i f f n e s s A s s e m b l e r 2 D ( q , me , a ) ;
C = A + M/ h t ;
[ L , U] = l u (C ) ;
tic ;
f o r l =2:m+1
13
b = LoadAssembler2D ( q , me , f , t , l ) ;
b r = b + M∗ e p s ( l − 1 , : ) ’ / h t ;
y=L\ b r ;
x r=U\y ;
eps ( l , : ) = x r ;
endfor
toc ;
could be more efficient. However, the time loop takes approximately 550seconds,
it’s better but it’s not what we want. We can use sparse matrices to resolve
thoses linears system. We can see that the matrices A and M are sparse matrices,
they have a huge amount of zeros elements. We can use sparse matrices to solve
thoses systemes, it could be more efficient.
2.3 Task 2c
The forward Euler method is an explicit time-stepping scheme, where the future
state of the solution is directly computed from the current state. While simple
and straightforward to implement, the forward Euler method has a significant
limitation in terms of stability, especially for parabolic PDEs, such as the heat
equation. For parabolic PDEs, the forward Euler method is conditionally stable,
meaning that it requires the time step ∆t to be sufficiently small relative to the
spatial discretization step ∆x to maintain stability.
For linear parabolic PDEs, the backward Euler method is unconditionally
stable, meaning that it does not require the time step to be below a certain
threshold to ensure stability. This allows for larger time steps to be used with-
out sacrificing the stability of the solution, making it more efficient for solving
parabolic PDEs.
For the forward method, we need to do this:
˙ l ) ≃ ξl+1 − ξl , ∀l = 0, ...m − 1
ξ(t
ht
Int he matlab code we just have to replace the time loop by
tic ;
f o r l =1:m
b = LoadAssembler2D ( q , me , f , t , l ) ;
b r = b + (M/ h t − A) ∗ e p s ( l , : ) ’ ;
x r =(M/ h t ) \ b r ;
eps ( l +1 ,:) = x r ;
endfor
toc ;
14
Figure 11: Solution at time tl with l=47 with the forward Euler method
The computer was not able to display the solution after T=0.5. We can
reduce the time-step to see the conditional stability specificity of the forward
method. We can choose ∆t = 0.01.
The ability to use larger time steps without encountering stability issues
makes it more efficient and reliable for long-time simulations of such phenom-
ena. So that’s I would prefer to use backward Euler, it’s better in this kind of
parabolic problems.
2.4 Task 2d
We have to replace the boundary condition by the Neumann condition:
Z Z Z
− v∇ · a∇u dx = a∇u · ∇v dx − v(a∇u · n) dS.
Ω Ω δΩ
15
A Appendices Part 1
A.1 Matlab codes for Task 1a
function A = AssemblageA ( x , a , Kl , Kr )
N = length ( x ) −1;
A = zeros (N+1,N+1);
f o r i =1:N
h = x ( i +1)−x ( i ) ;
xmid = ( x ( i +1)−x ( i ) ) / 2 ;
A( i , i ) = A( i , i ) + a ( xmid ) / h ;
A( i , i +1) = A( i , i +1) − a ( xmid ) / h ;
A( i +1, i ) = A( i +1, i )− a ( xmid ) / h ;
A( i +1, i +1) = A( i +1, i +1) + a ( xmid ) / h ;
end
A( 1 , 1 ) = A( 1 , 1 ) + Kl ;
A(N+1,N+1) = A(N+1,N+1) + Kr ;
endfunction
N=1000;
h = ( xr−x l ) /N;
16
x = x l : h : xr ;
A=AssemblageA ( x , a , Kl , Kr ) ;
b=Assemblageb ( x , f , Kl , Kr , g l , g r )
U = A\b ;
plot ( x ,U)
clear a l l
x l = 0 ; xr = 2∗ pi ;
g l = 1 ; gr = 1 ;
a = @( x ) 1 ;
f = @( x ) cos ( x ) ;
u r e a l = @( x ) cos ( x ) ;
norm quad = zeros ( 1 , 8 ) ;
Kl = 1 0 ˆ 8 ;
Kr = 1 0 ˆ 8 ;
j =1;
f o r N= 2 0 : 2 0 : 1 6 0
h ( j ) = ( xr−x l ) /N;
x = x l : h ( j ) : xr ;
A = AssemblageA ( x , a , Kl , Kr ) ;
b = Assemblageb ( x , f , Kl , Kr , g l , g r ) ;
U = A\b ;
plot (U, x ) ;
U real = ( u real (x ) ) ’ ;
error = abs (U − U r e a l ) ;
e r r o r q u a d = zeros ( 1 ,N+1);
f o r i =2:N
e r r o r q u a d ( i ) = ( error ( i +1)ˆ2+ error ( i ) ˆ 2 ) ∗ h ( j ) / 2 ;
17
endfor
endfor
hold on ;
alp = 0.5;
m= 20;
h = 1/6;
x = x l : h : xr ;
A = AssemblageA ( x , a , Kl , Kr ) ;
b = Assemblageb ( x , f , Kl , Kr , g l , g r ) ;
U=A\b ;
plot ( x , U, ’ b ’ ) ;
hold on ;
xlabel ( ’ x ’ ) ;
ylabel ( ’U ’ ) ;
f o r j =1:m
n=length ( x ) −1;
e r r = zeros ( n , 1 ) ;
f o r i =1:n
h = x ( i +1)−x ( i ) ;
c = f ( x ( i ) ) ; % temporary v a r i a b l e s
18
b = f (x( i +1));
t = ( cˆ2+b ˆ 2 ) ∗ h / 2 ;
e r r ( i ) = h∗ sqrt ( t ) ;
endfor
f o r i =1: length ( e r r )
i f e r r ( i ) > a l p ∗max( e r r )
x = [ x ( x ( i +1)+x ( i ) ) / 2 ] ;
endif
endfor
x = sort ( x ) ;
A = AssemblageA ( x , a , Kl , Kr ) ;
b = Assemblageb ( x , f , Kl , Kr , g l , g r ) ;
U = A\b ;
plot ( x , U, ’ r o ’ ) ;
endfor
plot ( x ,U)
B Appendices Part 1
B.1 Matlab codes for task 2a
mask = x . ˆ 2 + y . ˆ 2 <= 1 ;
x = x ( mask ) ;
y = y ( mask ) ;
q = [x(:) , y ( : ) ] ;
TRI = delaunay ( q ( : , 1 ) , q ( : , 2 ) ) ;
me = TRI ;
figure ;
t r i p l o t (me , q ( : , 1 ) , q ( : , 2 ) ) ;
axis e q u a l ;
t i t l e ( ’ Meshing o f t h e u n i t y d i s k on ( 0 , 0 ) ’ ) ;
xlabel ( ’X ’ ) ;
ylabel ( ’Y ’ ) ;
19
q=q ’ ;
me=me ’ ;
endfunction
20
Listing 9: function to create the Load vector on 2D
function b = LoadAssembler2D ( p , t , f )
np = s i z e ( p , 2 ) ;
nt = s i z e ( t , 2 ) ;
b = zeros ( np , 1 ) ;
f o r K = 1 : nt
l o c 2 g l b = t ( 1 : 3 ,K) ;
x = p(1 , loc2glb ) ;
y = p(2 , loc2glb ) ;
area = polyarea (x , y ) ;
bK = [ f ( x ( 1 ) , y ( 1 ) ) ;
f (x(2) ,y (2));
f (x (3) , y ( 3 ) ) ] / 3 ∗ area ;
b ( l o c 2 g l b ) = b ( l o c 2 g l b ) + bK ;
endfor
endfunction
Listing 11: Script to solve the linear system and to approch the solution
clear a l l ;
u 0 = @( x , y ) exp( −10∗( x ˆ2 + y ˆ 2 ) ) ;
a = @( x , y ) 0 . 0 1 ∗ ( x ˆ2 + y ˆ2 + 1 ) ;
f = @( x , y ) ( x ˆ2 + y ˆ2 >= 0 . 5 ˆ 2 ) ∗ ( 0 . 1 ∗ sin ( x ) ∗ cos ( x ) + 0 . 2 ) ;
T = 2;
h s =0.05 ;
h t = 0.1∗ h s ;
m=c e i l (T/ h t ) ;
[ q , me ] = diskmesh ( h s ) ;
N = size (q , 2 ) ;
t =0: h t : T ;
f o r i =1:N
x i = q(1 , i ) ;
y i = q(2 , i ) ;
21
eps ( 1 , i ) = u 0 ( x i , y i ) ;
endfor
M = MassAssembler2D ( q , me ) ;
A = S t i f f n e s s A s s e m b l e r 2 D ( q , me , a ) ;
A r = sparse ( h t ∗A + M) ; %c r e a t e t h e m a t r i x b e f o r e t h e time l o o p
tic ;
f o r l =2:m+1
b r = h t ∗ LoadAssembler2D ( q , me , f ) + M∗eps ( l − 1 , : ) ’ ;
x r = A r\ b r ;
eps ( l , : ) = x r ;
endfor
toc ;
figure ;
f o r t = 1 : s i z e ( eps , 1 )
clf ;
x = q(1 , : ) ;
y = q(2 , : ) ;
z = eps ( t , : ) ;
s c a t t e r 3 (x , y , z , 25 , z , ’ f i l l e d ’ ) ;
t i t l e ( s p r i n t f ( ’ I n s t a n t t %d ’ , t ) ) ;
xlabel ( ’X ’ ) ;
ylabel ( ’Y ’ ) ;
z l a b e l ( ’ Valeur ’ ) ;
colorbar ;
view ( 3 ) ;
pause ( 0 . 1 ) ;
end
22