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

LAB1

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

2 FEM for parabolic equations 9


2.1 Task 2a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Task 2b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Task 2c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Task 2d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

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 :

a(xl )u′ (xl ) = κl (u(xl ) − gl ),


−a(xr )u′ (xr ) = κr (u(xr ) − gr ),

where a(x) > 0 and f are given functions.



If we choose κl = κr = 108 , we then have a(xl )u
κl
(xl )
close to 0. Then we can
approximate u(xl ) − gl by 0.
Then, we can write u(xl ) = gl , and with the same reasoning, u(xr ) = gr .

Now, we have xl = 0, xr = 1, gl = 2, gr = 3, a(x) = 0.5 − 0.06x, f (x) =


0.03(x − 6)4 .

How can we run the code?


Considérons l’équation suivante, où x ∈ I = [xl , xr ]:

−au′′ (x) = f, x ∈ I = [xl , xr ]

−au′ (xl ) = κl (u(xl ) − gl )


−au′ (xr ) = κr (u(xr ) − gr )
We can multiply the equation by a function test v,

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

Let consider V = v | ∥v∥L2 (I) < 1; ∥v ′ ∥L2 (I) < 1




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

where Vh is the discrete space of continuous piecewise linears.

n
X
uh = ξj φj .
j=0

Choosing v = ϕi , pour i = 0, 1, . . . , n, we now obtain the following linear


system:
(A + R)ξ = b + r (5)
with:

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.

Figure 1: Solution uh found by FEM

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′′ (x) = cos(x), x ∈ I, (6)

with boundary conditions :

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:

Figure 2: uh with n=10

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

With a quadrature formula, we have


Z xi+1
[cos(xi−1 ) − uh (xi−1 )]2 + [cos(xi ) − uh (xi )]2
(u − uh )2 dx ≈
xi 2

5
Now, we can implement the code, and look at the error:

Figure 3: Quadratic error between uh and cos(x) the real solution

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:

−u′′ (x) = exp −100(x − 0.5)2 ,



x ∈ I = [0, 1], (7)

with boundary conditions :

u(0) = u(1) = 1. (8)

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

Figure 5: Adaptive mesh refinement with m=15, α = 0.9

7
Figure 6: Adaptive mesh refinement with m=5, α = 0.2

Figure 7: Adaptive mesh refinement with m=20, α = 0.5

We clearly can see the impact of m and α:

• Parameter α: The parameter α ∈ [0, 1] critically determines the aggres-


siveness of the mesh refinement process.A diminution in α augments the

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 FEM for parabolic equations


Consider the following two-dimensional heat equation

ut = ∇ · a∇u + f, (x, y) ∈ Ω, t ∈ [0, T ], (9)

u(t, x, y) = 0, (x, y) ∈ ∂Ω, t ∈ [0, T ], (10)


2 2
u(0, x, y) = e−10(x +y )
, (x, y) ∈ Ω, (11)
where a(x, y) > 0 is the diffusion coefficient, f is the heat source and t ∈ [0, T ]
for some final time T . The domain Ω is the circle of the radius one centered on
(0,0).

2.1 Task 2a
Let’s start by derive the variational formulation of the parabolic problem:

ut = ∇ · a∇u + f, ut is the partial derivative with respect to t

We can multiply this equation by a test function and we can integrate on Ω


both sides. So we have:
Z Z Z
vut dx − v∇ · a∇u dx = f v dx.
Ω Ω Ω

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
Ω Ω Ω

 Vh the space of continuous piecewise linear on Ω, and Vh,0 ⊂ Vh


let’s consider
where Vh,0 = v ∈ Vh | v|δΩ = 0 .
We have to find uh ∈ Vh,0 such that
Z Z Z
(uh )t v dx + a∇uh · ∇v dx = f v dx, ∀v ∈ Vh,0
Ω Ω Ω

We can choose v = P φi ∀i ∈ 1, 2, ..., ni where ni is the number of interior nodes.


n
We have also uh = j=1 ξj (t)φj , and t ∈ [0, T ].
So, we can replace uh by his expression in the variational formulation:
Pni ni
∂( j=1 ξj (t)φj )
Z Z X Z
φi dx + a∇( ξj (t)φj ) · ∇φj dx = f φi dx
Ω ∂t Ω j=1 Ω

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 }
Ω Ω Ω

and ξ(t) = (ξi (t))i∈1,..,ni .


We also know that
2
+y 2 )
ξ( t0 ) = ξ( 0).uh (0, x, y) = e−10∗(x for every nodes Ni = (xi , yi ).
Pni 2 2
We have uh (0, xi , yi ) = j=1 ξj (0)φj (xi , yi ) = e−10(xi +yi ) ,
2 2
ξi (0) = e−10(xi +yi ) , ∀i ∈ {1, ..ni } (13)
Now, we have our system of ordinary differential equations with our initial
vector condition ξ(0)

10
Let’s prove that
Z t
∥uh (t, ·)∥Ω ≤ ∥uh (0, ·)∥Ω + ∥f (τ, ·)∥Ω dτ
0

In the variational formulation, we can replace v by uh , so we have

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

The estimate is powerful because it provides a guarantee of stability for


the numerical method: as long as the initial condition and the source term
are bounded, the solution produced by the finite element method will also be
bounded and stable over the time interval [0,T].
Let’s discretize the system in time by backward Euler. t ∈ [0, T ], 0 = t0 <
t1 < t2 < ... < tm = T and kl = tl − tl−1 , ∀l = 1, 2, .., m.
For each time tl , we want to find an approximation ξl of ξ(tl ). Using back-
ward Euler method, we have

˙ 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

For each ξl , we know ξl−1 . With a(x, y) = 0.01(x2 + y 2 + 1), T=2, f =


0.1sin(x)cos(x) + 0.2 only exists outside of the disk x2 + y 2 = 0.52 . With a
spatial grid size h=0.05, a,d a uniform time step to be 0.1h.
First, we need to create a mesh with a spatial grid size h=0.05:

Figure 8: Meshing of the unity disk centered at (0,0) with h=0.05.

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

Figure 10: Figure of the solution at T=2

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 ;

We can see that the solution is not stable:

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:

∇u · ⃗n = 0, (x, y) ∈ ∂Ω, t ∈ [0, T ]

Previously, we had this equation, where v is a test function:

Z Z Z
− v∇ · a∇u dx = a∇u · ∇v dx − v(a∇u · n) dS.
Ω Ω δΩ

We know that ∇u · ⃗n = 0, (x, y) ∈ ∂Ω


So we have the same variational formulation:
Z Z Z
ut v dx + a∇u · ∇v dx = f v dx,
Ω Ω Ω

The thing is that the space of v need to change.

15
A Appendices Part 1
A.1 Matlab codes for Task 1a

Listing 1: Code for the assembly of the stiffness matrix A + R

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

Listing 2: Code for the assembly of the load vector b + r


function b = Assemblageb ( x , f , Kl , Kr , g l , g r )
N = length ( x ) −1;
b = zeros (N+ 1 , 1 ) ;
f o r i =2:N
h = x ( i +1)−x ( i ) ;
b( i ) = f (x( i ))∗h ;
endfor
b ( 1 ) = f ( x ( 1 ) ) ∗ h/2 + Kl∗ g l ;
b ( end ) = f ( x ( end ) ) ∗ h/2 + Kr∗ g r ;
endfunction

Listing 3: Script for task1a


clear a l l ;
x l = 0 ; xr = 1 ;
g l = 2 ; gr = 3 ;
a = @( x ) 0 . 5 − 0 . 0 6 ∗ x ;
f = @( x ) 0 . 0 3 ∗ ( x −6)ˆ4;
Kl = 1 0 ˆ 8 ;
Kr = 1 0 ˆ 8 ;

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)

A.2 Matlab codes for Task 1b

Listing 4: Script to verify the second order convergence rate

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

norm quad ( j ) = sqrt (sum( e r r o r q u a d ) ) ;


j=j +1;

endfor

loglog ( h , norm quad , ’ r ’ ) ;


hold on ;
loglog ( h , h . ˆ 2 , ’ b ’ ) ;

hold on ;

A.3 Matlab codes for Task 1c

Listing 5: Script to make the Adaptive Mesh Refinement


clear a l l
x l = 0 ; xr = 1 ;
g l = 1 ; gr = 1 ;
a = @( x ) 1 ;
f = @( x ) exp( −100∗(x − 0 . 5 ) ˆ 2 ) ;
Kl = 1 0 ˆ 8 ;
Kr = 1 0 ˆ 8 ;

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

Listing 6: function to create a mesh of the unity disk according to h


function [ q , me ] = diskmesh ( h )
[ x , y ] = meshgrid ( −1:h : 1 , −1:h : 1 ) ;

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

Listing 7: function to create the Mass Matrix on 2D


function M = MassAssembler2D ( p , t )
np = s i z e ( p , 2 ) ;
nt = s i z e ( t , 2 ) ;
M = sparse ( np , np ) ;
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 ) ;
MK = [ 2 1 1 ;
1 2 1;
1 1 2]/12∗ area ;
M( l o c 2 g l b , l o c 2 g l b ) = M( l o c 2 g l b , l o c 2 g l b ) + MK;
endfor
M = sparse (M) ;
endfunction

Listing 8: function to create the Stiffness Matrix on 2D


function A = S t i f f n e s s A s s e m b l e r 2 D ( p , t , a )
np = s i z e ( p , 2 ) ;
nt = s i z e ( t , 2 ) ;
A = sparse ( np , np ) ;
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 , b , c ] = HatGradients ( x , y ) ;
xc = mean( x ) ; yc = mean( y ) ;
abar = a ( xc , yc ) ;
AK = abar ∗ ( b∗b ’ + c ∗ c ’ ) ∗ a r e a ;
A( l o c 2 g l b , l o c 2 g l b ) = A( l o c 2 g l b , l o c 2 g l b ) + AK;
endfor
A = sparse (A ) ;
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 10: function to create the Hat gradients


function [ area , b , c ] = HatGradients ( x , y )
a r e a=p o l y a r e a ( x , y ) ;
b=[y(2) −y ( 3 ) ; y(3) −y ( 1 ) ; y(1) −y ( 2 ) ] / 2 / a r e a ;
c =[x(3) −x ( 2 ) ; x(1) −x ( 3 ) ; x(2) −x ( 1 ) ] / 2 / a r e a ;
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 ;

eps = zeros (m+1,N ) ;

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

You might also like