You are on page 1of 30

Mathematical Models - DN2266

Homework 5 - DFT and spectral methods


Teachers
Anna-Karin TORNBERG
Rikard OJALA
Authors
Pierre-Alexandre BEAUFORT
Hadrien VAN LIERDE
December 2013
Contents
1 Selected theoretical problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 First problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Second problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 DFT and frequency content of functions . . . . . . . . . . . . . . . . . . . . . . 4
3 Spectral interpolation and dierentiation . . . . . . . . . . . . . . . . . . . . . . 9
3.1 Decay of coecients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.3 Approximation of derivative . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Spectral methods for dierential equations . . . . . . . . . . . . . . . . . . . . . 14
4.1 Expansion with a nite centered Fourier series . . . . . . . . . . . . . . . 14
4.2 The heat equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.3 Burgers equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
A Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
A.1 DN2221_lab5_Q2.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
A.2 DN2221_lab5_Q3a.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
A.3 DN2221_lab5_Q3b.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
A.4 DN2221_lab5_Q3c.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
A.5 DN2221_lab5_Spectral_Derivative.m . . . . . . . . . . . . . . . . . . . 25
A.6 DN2221_lab5_Q4b.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
A.7 DN2221_lab5_Q4d.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
0
1 Selected theoretical problems
1.1 First problem
Let u(x, t) =

n=1
a
n
(t) sin(nx) on the interval x [0, ]. If
u
x
=

n=1
a
(1)
n
(t) sin(nx) then
(i)
a
(1)
n
(t) =
4

m=1
m+n odd
nm
m
2
n
2
a
m
(t)
Proof : Dierentiating the expression of u, we have:
u
x
=

n=1
na
n
(t) cos(nx)
We thus have

n=1
a
(1)
n
(t) sin(nx) =
u
x
=

n=1
na
n
(t) cos(nx) (1)
We are going to multiply both side of the equation by a function of the form sin(mx) and integrate
the result over [0, ]. To that end, we calculate the following expressions:
Left hand terms:
_

0
sin(nx) sin(mx)dx =
_

0
1
2
(cos((n m)x) cos((n + m)x))dx
1. If n ,= m, this expression gives the following result
_

0
1
2
(cos((n m)x) cos((n + m)x))dx =
1
2
_
sin((nm)x)
nm

sin((n+m)x)
n+m
_

0
= 0
2. If n = m, the expression reduces to
_

0
1
2
(1 cos(2mx))dx =

2
Right hand terms
_

0
cos(nx) sin(mx)dx =
_

0
1
2
(sin((mn)x) + sin((m + n)x))dx
1. If n ,= m, this expression gives the following result
_

0
1
2
[sin((mn)x) + sin((m + n)x)] dx =
1
2
_

cos((mn)x)
mn

cos((m + n)x)
m + n
_

0
(a) If m + n is odd (hence mn is odd too), this expression is equal to
1
mn
+
1
m + n
=
2m
m
2
n
2
(b) If m + n is even (hence mn is even too), this expression is equal to zero.
2. If m = n, this expression is equal to
_

0
1
2
sin(2mx)dx = 0
(i)
The indices are not the same as in the question 1 of the homework: m and n are switched.
1
Hence, multiplying both sides of equation (1) by sin(mx) for m N, we have:

2
a
(1)
m
(t) =

n=1
m+n odd
2mn
m
2
n
2
a
n
(t)
Hence,
a
(1)
m
(t) =
4

n=1
m+n odd
mn
m
2
n
2
a
n
(t)
1.2 Second problem
We consider the equation
u
t
+ c
u
x
= d

3
u
x
3
(2)
dened on the interval x [0, L], with periodic boundary conditions u(0, t) = u(L, t), where c
and d are constants. We let
u
N
(x, t) =
N/2

k=N/2
u
k
(t)e
2ikx/L
be a spectral expansion of u.
We rst dene a weak formulation of the problem above. We dene the following trial space
V = v : C C such that v is 2 times dierentiable,
_
L
0
[v[
2
+

dv
dx

2
+

d
2
v
dx
2

2
dx < , v(0) = v(L)
Then, for a certain v in V , we multiply the equation (2) by v and integrate the result on [0, L].
We obtain
_
L
0
u
t
vdx +
_
L
0
c
u
x
vdx =
_
L
0
d

3
u
x
3
vdx (3)
Integrating the right hand term by parts, we have
_
L
0
d

3
u
x
3
vdx =
_
L
0
d

2
u
x
2
v
x
dx + d
_

2
u
x
2
v
_
L
0
The last term of this expression vanishes as v(0) = v(L) (and assuming

2
u
x
2
(0, t) =

2
u
x
2
(L, t),
this will be the case for the Galerkin method used). Thus, equation (3) becomes
_
L
0
u
t
vdx +
_
L
0
c
u
x
vdx +
_
L
0
d

2
u
x
2
v
x
dx = 0
The weak formulation of the problem 2 is thus
Find u C
1
([0, ], V ) such that
_
L
0
u
t
vdx +
_
L
0
c
u
x
vdx +
_
L
0
d

2
u
x
2
v
x
dx = 0 v V
To formulate Galerkins method, we reduce the space V to a nite dimensional space spanned
by (e
2ikx/L
)
N/2kN/2
and we seek a solution of the form
u
N
(x, t) =
N/2

k=N/2
u
k
(t)e
2ikx/L
2
We conrm that

2
u
x
2
(0, t) =

2
u
x
2
(L, t). Thus, for all l N and v
l
= e
2ilx/L
, we must verify
_
L
0
u
N
t
v
l
dx +
_
L
0
c
u
N
x
v
l
dx +
_
L
0
d

2
u
N
x
2
v
l
x
dx = 0 (4)
Calculating each term of this equation, we obtain
_
L
0
c
u
N
t
v
l
dx =
_
L
0
N/2

k=N/2
u
k
t
e
2i(kl)x/L
dx
= L
u
l
t
_
L
0
c
u
N
x
v
l
dx = c
_
L
0
N/2

k=N/2
u
k
2ik
L
e
2i(kl)x/L
dx
= c u
l
2il
L
L
= 2ilc u
l
_
L
0
d

2
u
x
2
v
l
x
dx = d
_
L
0
N/2

k=N/2
u
k
(
2ik
L
)
2
2il
L
e
2i(kl)x/L
dx
= u
l
8
3
il
3
L
3
L
= u
l
8
3
il
3
L
2
Thus, the equation (4) becomes
L
u
l
t
+ c2il u
l
+
8
3
il
3
L
2
u
l
= 0
Hence, for given initial conditions,
u
l
(t) = u
l
(0)e
i
t
L
(2cl+
8
3
l
3
L
2
)
3
2 DFT and frequency content of functions
We consider a 1-periodic function f. We introduce the grid x
j
=
j
N
, j = 0, 1, 2, ..., N 1 and
denote f
j
= f(x
j
). We dene the DFT coecients

f
k
by

f
k
=
1
N
N1

j=0
f
j
e
2ikx
j
/L
, k = 0, ..., N 1
For a few dierent functions f, we use MATLAB function fft.m, examine the real and imaginary
part of the DFT coecients and compare the results to theory. The MATLAB function used is
in appendix A.1.
First function: f(x) = 1
We clearly have

f
0
=
1
N

N1
i=0
1 = 1

f
k
=
1
N

N1
j=0
e
2ikx
j
/L
= 0, k = 1, ..., N 1
(5)
The result obtained with fft is similar, as shown in graph 1 (we used function fftshift.m to
relate all coecients to a frequency in N/2, ..., N/2 1):
25 20 15 10 5 0 5 10 15 20 25
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Frequence (Hz)
M
a
g
n
i
t
u
d
e

o
f

t
h
e

F
o
u
r
i
e
r

c
o
e
f
f
i
c
i
e
n
t


real
imaginary
peak
Figure 1: DFT of the function f(x) = 1
Second function: f(x) = sin(2x)
We clearly have
f(x) =
1
2i
(e
i2x
e
i2x
) (6)
which gives directly the Fourier expansion of f (zero real parts and for imaginary parts, peaks
in frequencies 1Hz and 1Hz). The result obtained with fft is similar as shown in graph 2.
4
25 20 15 10 5 0 5 10 15 20 25
0.5
0.4
0.3
0.2
0.1
0
0.1
0.2
0.3
0.4
0.5
Frequence (Hz)
M
a
g
n
i
t
u
d
e

o
f

t
h
e

F
o
u
r
i
e
r

c
o
e
f
f
i
c
i
e
n
t


real
imaginary
peaks
Figure 2: DFT of the function f(x) = sin(2x)
Third function: f(x) = cos(2x)
We clearly have
f(x) =
1
2
(e
i2x
+ e
i2x
) (7)
which gives directly the Fourier expansion of f (zero imaginary part and for real part, peaks
in frequencies 1Hz and 1Hz). The result obtained with fft is similar as shown in graph 3.
25 20 15 10 5 0 5 10 15 20 25
0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
Frequence (Hz)
M
a
g
n
i
t
u
d
e

o
f

t
h
e

F
o
u
r
i
e
r

c
o
e
f
f
i
c
i
e
n
t


real
imaginary
peaks
Figure 3: DFT of the function f(x) = cos(2x)
Fourth function: f(x) = sin(4x) + cos(6x)
We clearly have
f(x) =
1
2i
(e
i4x
e
i4x
) +
1
2
(e
i6x
+ e
i6x
) (8)
5
which gives directly the Fourier expansion of f (peaks in frequencies 2Hz and 2Hz for imag-
inary part, peaks in frequencies 3Hz and 3Hz for real part). The result obtained with fft is
similar as shown in graph 4.
25 20 15 10 5 0 5 10 15 20 25
0.5
0.4
0.3
0.2
0.1
0
0.1
0.2
0.3
0.4
0.5
Frequence (Hz)
M
a
g
n
i
t
u
d
e

o
f

t
h
e

F
o
u
r
i
e
r

c
o
e
f
f
i
c
i
e
n
t


real
imaginary
peaks
Figure 4: DFT of the function f(x) = sin(4x) + cos(6x)
Fifth function: f(x) = sin
3
(2x)
Using trigonometric identities, one can show that f(x) = sin
3
(2x) =
3
4
sin(2x)
1
4
sin(6x).
Thus, we have
f(x) =
3
8i
(e
i2x
e
i2x
)
1
4i
(e
i6x
e
i6x
) (9)
which gives directly the Fourier expansion of f (peaks in frequencies 1Hz and 1Hz, 3Hz and
3Hz for imaginary part, zero real part). The result obtained with fft is similar as shown in
graph 5.
6
25 20 15 10 5 0 5 10 15 20 25
0.4
0.3
0.2
0.1
0
0.1
0.2
0.3
0.4
Frequence (Hz)
M
a
g
n
i
t
u
d
e

o
f

t
h
e

F
o
u
r
i
e
r

c
o
e
f
f
i
c
i
e
n
t


real
imaginary
peaks
Figure 5: DFT of the function f(x) = sin
3
(x)
From this graph, one can thus deduce that the function f can be written as
f(x) =
3
8i
(e
i2x
e
i2x
)
1
4i
(e
i6x
e
i6x
) =
3
4
sin(2x)
1
4
sin(6x)
Sixth function: f(x) = sin
2
(2x) cos
2
(2x)
Using trigonometric identities, one can show that
f(x) = sin
2
(2x)(1 sin
2
(2x))
= sin
2
(2x) sin
4
(2x)
=
1 cos(4x)
2
(
1 cos(4x)
2
)
2
=
1
8

1
8
cos(8x)
Thus, we have
f(x) =
1
8

1
16
(e
i8x
+ e
i8x
) (10)
which gives directly the Fourier expansion of f (peaks in frequencies 0Hz, 4Hz and 4Hz for
real part, zero imaginary part). The result obtained with fft is similar as shown in graph 6).
7
25 20 15 10 5 0 5 10 15 20 25
0.1
0.05
0
0.05
0.1
0.15
Frequence (Hz)
M
a
g
n
i
t
u
d
e

o
f

t
h
e

F
o
u
r
i
e
r

c
o
e
f
f
i
c
i
e
n
t


real
imaginary
peaks
Figure 6: DFT of the function f(x) = sin
2
(2x) cos
2
(2x)
From this graph, one can thus deduce that the function f can be written as
f(x) =
1
8

1
16
(e
i8x
+ e
i8x
) =
1
8

1
8
cos(8x)
8
3 Spectral interpolation and dierentiation
3.1 Decay of coecients
We consider a function f dened by
f(x) = e
M(x/L0.3)
2
, 0 x < L
f(x + L) = f(x), x R
The DFT coecients

f
k
are dened by

f
k
=
1
N
N1

j=0
f
j
e
2ikx
j
/L
, k = 0, ..., N 1
where x
j
= jL/N, j = 0, 1, 2, ..., N 1 and f
j
= f(x
j
).
We consider N = 2
8
= 256 and L = 1. We compute the DFT coecients versus k for
k = 0, 1, 2, ..., N/2 1 and for M = 10, 100, 200 and 400. The DFT coecients are displayed
in gure 7. The MATLAB function used is in appendix A.2.
0 50 100 150
10
2
10
0
10
2
10
4
k index
D
F
T

c
o
e
f
f
i
c
i
e
n
t
10
0 50 100 150
10
5
10
0
10
5
k index
D
F
T

c
o
e
f
f
i
c
i
e
n
t
100
0 50 100 150
10
10
10
5
10
0
10
5
k index
D
F
T

c
o
e
f
f
i
c
i
e
n
t
200
0 50 100 150
10
20
10
10
10
0
10
10
k index
D
F
T

c
o
e
f
f
i
c
i
e
n
t
400
Figure 7: DFT of the function f for k = 0, 1, 2, ..., N/2 1
Theoretically, for an analytic function, the decay should be exponential. On the graphs, we see
that for small values of M, the decay is not exponential (an exponential decay would appear as
a straight line on a semilog-graph). For N = 100 and N = 200, the error is rst at then the
decay seems to be exponential and then, after a certain limit, the decay is not exponential any-
more but the coecients are already negligible. Finally, for N = 400, the error is rst at then
the decay seems to be exponential up to k 40 and then the coecients vanish. The base of the
convergence is approximately 0.5 in all cases (if y = r
x
, we dene r as the base). How can we
explain this behavior ? f is not analytic as it is not continuous in points of kL, k Z and
as we cannot dene a continuous prolongation of f. Yet, when M gets larger, the curve becomes
smoother. This could explain why we observe an exponential convergence for large values of M.
9
3.2 Interpolation
In this subsection, f denotes the same function as in the preceding subsection, with M = 100.
We compute the DFT coecients

f
k
for N = 2
4
. Then, we evaluate f on a much denser grid
(with 400 points) and plot the data and the interpolant versus x. We do this for two dierent
ways of calculating the DFT coecients :
1. First way:

f
k
=
1
N
N1

j=0
f
j
e
2ikx
j
/L
, k = 0, ..., N 1
f(x) =
N1

k=0

f
k
e
2ikx/L
, 0 x < L
2. Second way:

f
k
=
1
N
N1

j=0
f
j
e
2ikx
j
/L
, k = N/2, ..., N/2 1
f(x) =
N/21

k=N/2

f
k
e
2ikx/L
, 0 x < L
The results are displayed in gure 8 for the rst method of calculation and in gure 9 for the
second method of calculation. In each case, the real part of the result is taken. The MATLAB
function used is in appendix A.3.
0 0.2 0.4 0.6 0.8 1
0.2
0
0.2
0.4
0.6
0.8
1
1.2
xcoordinate
f
u
n
c
t
i
o
n

a
n
d

i
n
t
e
r
p
o
l
a
n
t

(
u
n
c
e
n
t
e
r
e
d
)


data
interpolation
fft points
Figure 8: Spectral interpolation in 400 points using rst method of calculation
10
0 0.2 0.4 0.6 0.8 1
0.2
0
0.2
0.4
0.6
0.8
1
1.2
xcoordinate
f
u
n
c
t
i
o
n

a
n
d

i
n
t
e
r
p
o
l
a
n
t

(
c
e
n
t
e
r
e
d
)


data
interpolation
fft points
Figure 9: Spectral interpolation in 400 points using second method of calculation
To understand the dierence between the two results above, one can compute the vector of
imaginary parts of the values obtained with spectral interpolation, and compare the norm of
this vector for the two methods of calculation. The rst method gives a norm of 4.33, the
second gives a norm of 0.0073. Thus the values calculated with the rst method still contain
complex values with imaginary part that are much greater than with the second method. How
can we explain this phenomenon?
Let us analyze the second method. For all k N/2 + 1, ..., N/2 1,

f
k
=

f

k
and

f
k
e
2ikx/L
= (

f
k
e
2ikx/L
)

and

f
k
e
2ikx/L
+ (

f
k
e
2ikx/L
)

= [

f
k
[
2
R. Thus, when summing
all the terms to compute f(x), the result is necessarily real, except for the contribution of

f
N/2
and

f
0
that can contain a little imaginary part.
Let us now analyze the rst method. For all k 1, ..., N/2 1, N/2 + 1, ..., N 1. We may
observe that for all l 1, ..., N/2 1,

f
N/2l
=
1
N
N1

j=0
f
j
e
2i(
N
2
l)x
j
/L
=
1
N
N1

j=0
f
j
e
ji
e
2ilx
j
/L
=
1
N
N1

j=0
f
j
(1)
j
e
2ilx
j
/L
=
1
N
N1

j=0
f
j
(1)
j
(e
2ilx
j
/L
)

= (
1
N
N1

j=0
f
j
(1)
j
e
2ilx
j
/L
)

= (
1
N
N1

j=0
f
j
e
2i(
N
2
+l)x
j
/L
)

=

f

N/2+l
11
Nevertheless, where these two terms appear in the expression of f(x), we have

f
N/2l
e
2i(
N
2
l)
x
L
+

f
N/2+l
e
2i(
N
2
+l)
x
L
As we cannot ensure that e
il
x
L
is real (except when x = x
j
= jL/N for some j = 0, 1, ..., N1),
we cannot ensure that the two terms in the expression above are complex conjugate of each
other. Thus, for method 1, we cannot ensure that the value will be (almost) real.
3.3 Approximation of derivative
One can use the spectral interpolation to compute approximations of f

(x
k
) and f

(x
k
) where
x
k
= kL/N, k = N/2, ..., N/2 1. These approximations are computed in the following way:

f
k
=
1
N
N1

j=0
f
j
e
2ikx
j
/L
, k = 0, ..., N 1
Df(x
l
) =
N/21

k=N/2

f
k
(2ik/L)e
2ikx
l
/L
, l =
N
2
, ...,
N
2
1
D
2
f(x
l
) =
N/21

k=N/2

f
k
(2ik/L)
2
e
2ikx
l
/L
, l =
N
2
, ...,
N
2
1
Let us rst analyze the results obtained for Df. We compute the error |Df f

|
2
=
_
1
N

N1
j=0
[Df(x
l
) f

(x
l
)[
2
. Then we do a loglog-plot of this error vs N for N = 2
m
and
m = 4, 5, 6, 7, 8. We repeat the experience for M = 100, 200, 400 and 800. The MATLAB function
used is in appendix A.4. The result is displayed on the gure 10.
10
1
10
2
10
3
10
4
10
3
10
2
10
1
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
100
10
1
10
2
10
3
10
10
10
5
10
0
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
200
10
1
10
2
10
3
10
20
10
10
10
0
10
10
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
400
10
1
10
2
10
3
10
20
10
10
10
0
10
10
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
800
Figure 10: For the approximation of rst derivative, 2-norm of the error vs N for N = 2
m
,
m = 4, 5, 6, 7, 8 and for M = 100, 200, 400 and 800
In all cases, the graph can be decomposed in three parts. The error curve is rather at for
small values of N. Then, the error decreases exponentially. Finally, the errors atten out due
to round o errors. Yet, for dierent values of M the results are slightly dierent. For small
values of M (M = 100 and M = 200), the error decreases exponentially. Then, it increases a
12
little, owing to round o errors. For larger values of M (M = 400 and M = 800), the error is
rst at, then it decreases exponentially, then it attens (due to round o errors). The order
of the exponential decay is always r 0.5 (where q is the base of the exponential function for
an error of the form r
N
). Thus, if we consider these results only, we may say that
1. the larger M is, the smaller the value the error reaches before roundo errors occur,
2. the increase of error due to roundo errors is more signicant when M is large,
3. the larger M is, the greater the value of N for which roundo errors occur.
So, when the function becomes steeper, the approximation of the derivative is sharper.
Let us now analyze the second derivative approximation. The loglog-plot of the 2-norm of the
error is plotted in graph 11.
10
1
10
2
10
3
10
2
10
1
10
0
10
1
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
100
10
1
10
2
10
3
10
10
10
5
10
0
10
5
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
200
10
1
10
2
10
3
10
20
10
10
10
0
10
10
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
400
10
1
10
2
10
3
10
20
10
10
10
0
10
10
N
D
i
s
c
r
e
t
e

2

n
o
r
m

o
f

e
r
r
o
r
800
Figure 11: For the approximation of second derivative, 2-norm of the error vs N for N = 2
m
,
m = 4, 5, 6, 7, 8 and for M = 100, 200, 400 and 800
We observe a similar behavior.
1. the larger M is, the smaller the value the error reaches before roundo errors occur,
2. the increase of error due to roundo errors is more signicant when M is large,
3. the larger M is, the greater the value of N for which roundo errors occur.
The order is also q 5 in all cases.
As a conclusion, we have to pay attention to the two following points when evaluating the rst
or second derivative with spectral interpolation. Firstly, even when the error is supposed to
decrease exponentially, roundo errors can deteriorate the result signicantly, in that case, the
error cannot be reduced indenitely (the error reaches a minimum that is strictly positive).
Secondly, if the initial error (for small values of N) is rather large and the roundo error occurs
for small values of N, one cannot reach a very high precizion (as we have seen for M = 100).
13
4 Spectral methods for dierential equations
In this section, we consider the following model equation:
u
t
+ A(u
2
)
x
+ Bu
x
=
2
u
xx
+
3
u
xxx
, on x [0; L)
with initial condition:
u(x; t = 0) = f(x)
where:
A, B,
2
> 0,
3
are constant
f(x) = f(x + L)
u(x; t) = u(x + L; t)
_

_
(11)
Indeed, (11) is a model equation as - for certain values A, B,
2
,
3
- we can model the
heat, convection-diusion, Schrdinger, Burger or Korteweg-deVries equations. Here are the
particular parameter-values for these dierent dierential equations:
Equation A B
2

3
Heat 0 0 0
Convection-diusion 0 1 0
Schrdinger 0 0 i 0
Burger 0.5 0 0
Korteweg-deVries 0.5 0 0 1
Table 1: Parameter-values of (11) according to the heat, convection-diusion, Schrdinger,
Burger or Korteweg-deVries equations.
We are going to solve (11) by using a nite centered Fourier series of u(x; t). First, we will
derive the corresponding dierential equations, with a reference to a MATLAB implementation
that solves it. Then, we will consider the heat equation and especially the error performed
by the implementation. Finally, we will consider the Burger equation in order to study the
behavior of a solution of a nonlinear dierential equation.
4.1 Expansion with a nite centered Fourier series
Let us approximate u(x; t) u
N
(x; t), with u
N
a nite centered Fourier series:
u
N
(x; t) =
N
2
1

k=
N
2
u
k
(t) exp
_
2k
x
L
i
_
(12)
We use a collocation approach, i.e. we wish that (11) is veried with u := u
N
in x =
jL
N
, for j =
0, 1, ..., N 1. We thus dene:
U(t) = [u
N
(x
0
; t), u
N
(x
1
; t); ; u
N
(x
N1
; t)]
T
where x
j
=
jL
N
. Therefore, (11) becomes:
U
t
+ AD
N
(U U) + BD
N
U =
2
D
2
N
U +
3
D
3
N
U (13)
where represents a pointwise multiplication and D
N
is the Fourier collocation dierential
matrix
(ii)
.
From (13), we are able to compute numerically U by using an integrator factor technique. We
choose here to use the fourth order Runge-Kutta method, i.e.:
(ii)
which the operator T
N
is such that (T
N
u)
l
=

N
2
1
k=
N
2
_
ik
N
exp(2ik
l
N
)

N1
j=0
_
exp(2ik
j
N
)u
j
_
_
14
U
0
= f(x);
t
i
=
iT
n
;
i = 1;
while i < n+1 do
K
1
:= F(t
i
, U
i
)
K
2
:= F(t
i
+
t
2
; U
i
+
t
2
K
1
)
K
3
:= F(t
i
+
t
2
; U
i
+
t
2
K
2
)
K
4
:= F(t
i
+ t; U
i
+ tK
3
)
U
i+1
= U
i
+
t
6
(K
1
+ 2K
2
+ 2K
3
+ K
4
)
i:=i+1;
end
function F(t,u)
return(
2
D(D(u)) +
3
D(D(D(u))) 2AuD(u) BD(u);)
end
where D( ) applied the Fourier collocation dierential operation (see section 3 and appendix
A.5). DN2266_lab5_Q4b.m performs such a computation (see appendix A.6). Observe that we
have rewritten the nonlinear term (u
2
)
x
= 2uu
x
. Analytically, this is equivalent but this is not
the case about the approximate results from the discretization.
Let us write the ODEs for the Fourier coecients by applying a Galerkins method. In other
words, we expand u with the nite centered Fourier series u
N
in (11)
(iii)
, then we multiply by
the test function exp(2il
x
L
)in order to use the orthogonality of the complex exponentials
(iv)
by integrating over x [0; L]. We nally get after dividing by L:
d u
l
dt
+
2A
L
_
L
0
u
N
u
N
x
exp(2il
x
L
)dx + B u
l
2il
L
=
2
u
l
4
2
l
2
L
2
+
3
u
l
8
3
il
3
L
3
(14)
We let:
w
l
:=
1
2
_
2
0
u
N
u
N
x
exp(2ilx)dx
= (

u
N
u
N
x
)
l
Unfortunately, the nonlinear term needs a special treatment since
_

x
N
y
N
_
k
,= x
k
y
k
.
Therefore, we use the pseudo-spectral treatment, which consists in transforming u
l
and
u
l
x
to
physical space
(v)
, then performing a multiplication in this space and nally transforming the
product to Fourier space
(vi)
.
We notice that there is a main dierence between the collocation method and the pseudo-
spectral Galerkin method: the rst method does not apply a special treatment for the non
linear term, while the second one does. Actually, it is due to the fact that the collocation
method uses a space dierential operator while the Galerkin method does the dierential in the
traditional way. Besides, the collocation method always does the computation in the physical
domain, while the pseudo-spectral Galerkin method does the computations in the spectral
domain, except for the nonlinear term.
(iii)
We rewrite the nonlinear term (u
2
)
x
= 2uu
x
(iv)
_
L
0
exp(2k
x
L
i) exp(2l
x
L
i)dx = L
kl
(v)
e.g. using ifft.m
(vi)
e.g. using fft.m
15
4.2 The heat equation
We consider (11) that describes the heat equation
(vii)
, with L = 1,
2
= 1 and f(x) = sin(2x):
u
t
= u
xx
, on x [0; 1)
with initial condition:
u(x; t = 0) = sin(2x)
_

_
(15)
Straightaway - from the initial condition - we know that the analytic solution is
u

(x; t) = exp(4
2
t) sin(2x)
We want to study the numerical solution of (15) until the nal time T = 0.1; 1 and for
N = 2
m
, with m = 3; 4; 5; 6; 7, by using DN2266_lab5_Q4b.m
(viii)
.
However, we have to take care of the choice of the time stepsize in order to keep a stable scheme.
We know that the stability of a fourth order Runge-Kutta method
(ix)
is about[2.8; 0] on the
real axis. In other words, the computation is stable if and only if t'() [2.8; 0], where
is an eigenvalue of the jacobian of (15). We are going to compute approximately.
First, we let u := u
k
in (15), where u
k
is the k-th Fourier coecient:
f(t; u
k
(t)) =
d u
k
dt
(t)
f =
4k
2
L
2
u
k
(16)
Then, we compute the jacobien:
J =
f
u
k
=
4
L
2
diag(k
2
)
Since the jacobian is diagonal, its entries are its eigenvalues. We observe that all its eigenvalues
are real. Now,we are able to compute the maximum time stepsize t
max
:
t[[ 2.8
t
max

2.8
[[
max
We use [[
max
in order to take the worst situation for the stability. As k [
N
2
;
N
2
1],
[[
max
=

2
N
2
L
2
:
t
max

2.8L
2

2
N
2
If we write t
max

C
N

, then C =
2.8L
2

2
and = 2.
(vii)
see table 1
(viii)
see appendix A.6
(ix)
Remind: this is the scheme that DN2266_lab5_Q4b.m uses.
16
Let us use a time stepsize that is small enough in order that the error
(x)
is dominated by the
spatial error
(xi)
. Figure 12 displays the norm-2 of the error according to N, according to a
logarithmic scale. We observe that the error does not decrease exponentially, while it should
do. It decreases about N
6.5
. It is better than the fourth order Runge-Kutta method, it does
not seem exponential. Maybe it is due to the fact that we used few points for our loglog-plot.
Moreover, even if we have assumed that the time error is negligible, it is still there.
10
0
10
1
10
2
10
3
10
16
10
14
10
12
10
10
10
8
10
6
N
n
o
r
m
(
u
h
(
x
,
t
=
0
.
1
)

u
e
x
a
c
t
(
x
,
t
=
0
.
1
)
Figure 12: loglog-plot of the norm-2 of the norm of the error [[u(x; t = 0.1) u

(x; t = 0.1)[[
2
according to N = 2
m
, with m = 3; 4; 5; 6; 7.
Actually, it is possible to compute exactly the Fourier coecients owing to (16), by using an
integrating factor:
d u
k
dt
+
4k
2
L
2
u
k
= 0
exp
_
4k
2
L
2
t
_
d u
k
dt
+ exp
_
4k
2
L
2
t
_
4k
2
L
2
u
k
= 0
d
dt
_
exp
_
4k
2
L
2
t
_
u
k
(t)
_
= 0
= u
k
(t) =
k
exp
_
4k
2
L
2
t
_
(x)
e = [[u(x; t = 0.1) u

(x; t = 0.1)[[
2
(xi)
We use t := 2T floor
_
t
max
T
_
17
We determine
k
from the initial condition, which is:
sin(2x) =
N
2
1

k=
N
2

k
exp
_
2k
x
L
i
_
and then we use the orthogonality of the complex exponentials
(xii)
:

k
=
1
L
_
L
0
_
exp
_
2k
x
L
i
_
sin(2x)
_
dx
4.3 Burgers equation
We consider (11) that describes Burgers equation
(xiii)
, with L = 1 and f(x) = sin(2x) and for
N = 2
m
with m = 4; 5; 6; 7.
First, we work with
2
= 0:
u
t
+ uu
x
= 0
with initial condition:
u(x; t = 0) = sin(2x)
_

_
(17)
This dierential equation is nonlinear and there does not exist smooth solution
(xiv)
, because of
the nonlinear term. With DN2266_lab5_Q4b.m
(xv)
, the solution breaks around t = 0.165 (see
gure 13).
0.5 0 0.5 1 1.5
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
x
U
n
(
x
;
t
)
A=0.5, B=0, epsilon2=0, epsilon3=0, L=1, m=7,T=0.165
dt=6.9262e06, dt
max
=1.7316e05
t = 0.165[s]
Figure 13: Computation of DN2266_lab5_Q4b.m for u(x; t = 0.165), which is the solution of
(17). t 7 10
6
and m = 7.
(xii)
_
L
0
exp(2k
x
L
i) exp(2l
x
L
i)dx = L
kl
(xiii)
see table 1
(xiv)
The solutions have jumps, i.e. they tend to be discontinuous.
(xv)
See appendix A.6
18
Now, we are working with
2
= 0.02:
u
t
+ uu
x
= 0.02
2
with initial condition:
u(x; t = 0) = sin(2x)
_

_
(18)
This dierential equation is nonlinear, but its solutions are smooths, thanks to the term u
xx
(xvi)
.
Unfortunately, we cannot compute the exact solution; we thus use a consecutive renements to
estimate the error
(xvii)
.
Before computing that, we study roughly the stability of a fourth order Runge-Kutta method
that is applied to (18). Unfortunately, the nonlinear term avoids the previous study we did.
Therefore, we derive a linear ODE from (18) wich we reformulate as:
u
t
+
1
2
( u
x
)
2
= 0.02 u
xx
where u =
u
x
. If we dierentiate the previous equation according to x, we get (18). We let
u := 0.04 log w and get then the following linear ODE:
w
t
= w
xx
which is the heat equation ! We already know the stability of the fourth order Runge-Kutta
for this dierential equation. We can then assume that the stability of Runge-Kutta for (18) is
roughly related to this for (15). We work again with t = 2T floor
_
t
max
T
_
. Normally, we
should only observe the spatial error, by using such a time stepsize.
Figure 14 displays the loglog-plot of the norm-2 of the error
(xviii)
according to N. We observe
that this error seems decreasing exponentially, as it is expected if the time error is negligible.
The error seems to decrease faster for this ODE than for the heat equation. However, we have to
keep in mind this last computed error is approximated by assuming that the next computation
is exact and thus that the method is converging to the exact solution
(xix)
.
(xvi)
Actually, for
2
> 0, the corresponding dierential has smooth solutions.
(xvii)
i.e. e = [[u
2N
(x; t = 0.1) u
N
(x; t = 0.1)[[
2
(xviii)
e = [[u
2N
u
N
[[
2
(xix)
which implies that the method is stable.
19
10
1.3
10
1.4
10
1.5
10
1.6
10
1.7
10
1.8
10
8
10
7
10
6
10
5
10
4
10
3
10
2
10
1
N
n
o
r
m
(
u
N
(
x
,
t
=
0
.
2
)

u
2
N
(
x
,
t
=
0
.
2
)
Figure 14: loglog-plot of the norm-2 of the error u
2N
u
N
according to N = 2
m
, with m =
4; 5; 6; 7.
We focus on the pseudo-spectral Galerkin method we developed in the point 4.1. In the case
of the Burgers equation, (14) becomes:
d u
k
dt
=
_
(

u
N
u
N
x
)
k
+
4
2
k
2

2
L
2
u
k
_
(19)
We integrate (19) by using a forward Euler method. DN2266_lab5_Q4d.m performs this inte-
gration for the model equation (11) (see appendix A.7). From the previous discussion about
the stability, we do the same rough approximation. However, the stability of a forward Euler
method is about [2; 0] on the real axis. Therefore, we approximately compute t
max
in the
same way as we did in the point 4.2 and we get:
t
max

2L
2

2
N
2
We are going to check this bound with DN2266_lab5_Q4d.m, for
2
= 0.05. We will compare
the results with DN2266_lab5_Q4b.m. From our assumptions about the stability, we should get
dierent values of t
max
for both methods.
We check the stability only for t [0; 0.2] and m = 5; 6. Figure 15 displays the computations
with the largest possible n such that the computation is unstable for t =
T
n
. First, we notice
that our rough approximation about the stability are really far the results. However, the time
stepsize of the forward Euler method is smaller than this of the fourth order Runge-Kutta, as
expected. But, their sizes are of the same order for a same value of N. Observe that we have
studied the stability of the methods only for t [0; 0.2]; it is thus possible that the methods are
20
already unstable for smallest time stepsizes, but their computation explodes only for t > 0.2;
this could leads to our expected t
max
, even if it is unlikely owing to the rough approximation.
0.5 0 0.5 1 1.5
0.6
0.4
0.2
0
0.2
0.4
0.6
x
U
n
(
x
;t)
A=0.5, B=0, epsilon2=0.05, epsilon3=0, L=1, m=5,T=0.2
dt=0.0074074, dt
max
=0.00027705
t = 0.2[s]
0.6 0.4 0.2 0 0.2 0.4 0.6 0.8 1 1.2 1.4
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
x
U
n
(
x
;t)
A=0.5, B=0, epsilon2=0.05, epsilon3=0, L=1, m=5,T=0.2
dt=0.0054054, dt
max
=0.00019789
t = 0.2[s]
0.4 0.2 0 0.2 0.4 0.6 0.8 1 1.2 1.4
0.6
0.4
0.2
0
0.2
0.4
0.6
x
U
n
(
x
;t)
A=0.5, B=0, epsilon2=0.05, epsilon3=0, L=1, m=6,T=0.2
dt=0.0014599, dt
max
=6.9263e05
t = 0.2[s]
0.5 0 0.5 1 1.5
0.6
0.4
0.2
0
0.2
0.4
0.6
x
U
n
(
x
;t)
A=0.5, B=0, epsilon2=0.05, epsilon3=0, L=1, m=6,T=0.2
dt=0.0010989, dt
max
=4.9473e05
t = 0.2[s]
Figure 15: Computations for T = 0.2, with the smallest possible t such that the compu-
tation is unstable. Left column, fourth order Runge-Kutta method and right column, for-
ward Euler method. First row, m = 5 and second one, m = 6 such that N = 2
m
. First
row, t = 7.4 10
3
, 5.4 10
3
versus expected t
max
= 2.7 10
4
, 1.9 10
4
. Second row,
t = 1.4 10
3
, 1.1 10
3
versus expected t
max
= 6.9 10
5
, 4.9 10
5
.
21
A Appendix
A.1 DN2221_lab5_Q2.m
1 %% QUESTION 2: DFT of particular functions
2 c l o s e a l l
3 N=50;
4
5 %% Definition of the functions
6 f = {@(x)1;
7 @(x) s i n (2* pi *x);
8 @(x) cos (2* pi *x);
9 @(x) s i n (4* pi *x)+ cos (6* pi *x);
10 @(x)( s i n (2* pi *x)).^3;
11 @(x) s i n (2* pi *x).^2.* cos (2* pi *x).^2
12 };
13 x=(0:N-1)/N;
14
15 %% DFT of the first function
16 f i g ur e
17 F=ones(1,N);
18 Fourier =(1/N)* f f t (F) ;
19 Fourier= f f t s h i f t (Fourier);
20 k=-N/2:N/2-1;
21 freq=k;
22 pl ot (freq , r e a l (Fourier));
23 hol d on
24 gr i d on
25 pl ot (freq ,imag(Fourier),r);
26 pl ot (0,0,go)
27
28 xl abe l (Frequence (Hz))
29 yl abe l (Magnitude of the Fourier coefficient )
30
31 %% DFT of the functions 2 to 6
32 f o r l=2: l engt h (f)
33 f i g ur e
34 F=f{l}(x);
35 l engt h (F)
36 Fourier =(1/N)* f f t (F) ;
37 Fourier= f f t s h i f t (Fourier);
38 k=-N/2:N/2-1;
39 freq=k;
40 pl ot (freq , r e a l (Fourier));
41 hol d on
42 gr i d on
43 pl ot (freq ,imag(Fourier),r);
44
45 i f l==2 || l==3
46 pl ot ([-1 1],[0 0],go)
47 e l s e i f l==4
48 pl ot ([-3 -2 2 3],[0 0 0 0],go)
49 e l s e i f l==5
50 pl ot ([-3 -1 1 3],[0 0 0 0],go)
51 e l s e i f l==6
52 pl ot ([-4 0 4],[0 0 0],go)
53 end
54 xl abe l (Frequence (Hz))
55 yl abe l (Magnitude of the Fourier coefficient )
56
57 end
A.2 DN2221_lab5_Q3a.m
1 %% QUESTION 3a: decay of DFT coefficients
22
2 L=1;
3 Mvect =[10 100 200 400];
4 c l o s e a l l
5 f i g ur e
6
7 f o r l=1: l engt h (Mvect)
8 M=Mvect(l);
9 N=2^8;
10 k=0:(N-1);
11 x=k*L/N;
12 f=exp(-M*(x/L-0.3) .^2);
13 Fourier= f f t (f);
14 result=abs (Fourier);
15 s ubpl ot (2,2,l)
16 semi l ogy (k(1:N/2),result (1:N/2),+);
17 xl abe l (k index)
18 yl abe l (DFT coefficient )
19 gr i d on
20 t i t l e (M)
21 p= p o l y f i t (k(2:24) , l og (result (2:24)) ,1);
22 10^p(1)
23 end
A.3 DN2221_lab5_Q3b.m
1 %% QUESTION 3b: comparing two methods of interpolation
2 c l o s e a l l
3
4 %% First Method of calculation
5
6 % DFT coefficients
7 L=1;
8 M=100;
9 N=2^4;
10 k=0:(N-1);
11 x=k*L/N;
12 fk=exp(-M*(x/L-0.3) .^2);
13 Fourier =(1/N)* f f t (fk);
14
15 % Interpolation
16 X= l i ns pac e (0,L ,400);
17 Pif= z e r os ( s i z e (X));
18 f o r l=1: l engt h (Pif)
19 Pif(l)=sum(Fourier .*exp(2* pi *1i*k.*X(l)/L));
20 end
21 norm(imag(Pif))
22 Pif= r e a l (Pif);
23 f=exp(-M*(X/L-0.3) .^2);
24
25 % Graph
26 f i g ur e
27 pl ot (X,f)
28 hol d on
29 pl ot (X,Pif ,r)
30 pl ot (x,fk,o);
31 gr i d on
32 xl abe l (x-coordinate )
33 yl abe l (function and interpolant (uncentered))
34
35 %% Second Method of calculation
36
37 % DFT coefficients
38 k=-N/2:(N/2-1);
39 fk=exp(-M*(x/L-0.3) .^2);
40 Fourier =(1/N)* f f t (fk);
41 Fourier= f f t s h i f t (Fourier);
42
23
43 % Interpolation
44 X= l i ns pac e (0,L ,400);
45 Pif= z e r os ( s i z e (X));
46 f o r l=1: l engt h (Pif)
47 Pif(l)=sum(Fourier .*exp(2* pi *1i*k.*X(l)/L));
48 end
49 norm(imag(Pif))
50 Pif= r e a l (Pif);
51 f=exp(-M*(X/L-0.3) .^2);
52
53 % Graph
54 f i g ur e
55 pl ot (X,f)
56 hol d on
57 pl ot (X,Pif ,r)
58 pl ot (x,fk,o);
59 gr i d on
60 xl abe l (x-coordinate )
61 yl abe l (function and interpolant (centered))
A.4 DN2221_lab5_Q3c.m
1 %% QUESTION 3c: spectral approximation of first and second derivative
2
3 c l o s e a l l
4 c l e a r a l l
5 L=1;
6 Mvect =100*2.^[0 1 2 3];
7 Nvect =2.^(4:8);
8 pts =[2 3 4 4];
9
10 f i g ur e
11
12 %% First derivative
13 f o r s=1: l engt h (Mvect)
14 M=Mvect(s);
15 err= z e r os (1, l engt h (Nvect));
16
17 % Fourier coefficients and approximation
18 f o r t=1: l engt h (Nvect);
19 N=Nvect(t);
20 x=(0:(N-1))*L/N;
21 k=-N/2:(N/2-1);
22 fk=exp(-M*(x/L-0.3) .^2);
23
24 Fourier= f f t (fk);
25 Fourier= f f t s h i f t (Fourier);
26 DFourier=Fourier .*k*1i*2* pi /L;
27 DFourier=ifftshift(DFourier);
28 Dfapr= i f f t (DFourier);
29
30 Df=exp(-M*(x/L-0.3) .^2) .*(x/L-0.3) .*(-2*M/L);
31 err(t)=1/ s qr t (N)*norm(Dfapr -Df);
32 end
33
34 % Graph of the error
35 s ubpl ot (2,2,s)
36 l ogl og (Nvect ,err ,o-);
37 gr i d on
38 xl abe l (N);
39 yl abe l (Discrete 2-norm of error)
40 t i t l e (M)
41
42 % "Order"
43 p= p o l y f i t (Nvect (1:pts(s)), l og (err(1:pts(s))) ,1);
44 M
45 10^p(1)
24
46 end
47
48 %% Second derivative
49 f i g ur e
50 f o r s=1: l engt h (Mvect)
51 M=Mvect(s);
52 err= z e r os (1, l engt h (Nvect));
53
54 % Fourier coefficients and approximation
55 f o r t=1: l engt h (Nvect);
56 N=Nvect(t);
57 x=(0:(N-1))*L/N;
58 k=-N/2:(N/2-1);
59 fk=exp(-M*(x/L-0.3) .^2);
60
61 Fourier= f f t (fk);
62 Fourier= f f t s h i f t (Fourier);
63 D2Fourier=Fourier .*(k*1i*2* pi /L).^2;
64 D2Fourier=ifftshift(D2Fourier);
65 D2fapr= i f f t (D2Fourier);
66
67 D2f=exp(-M*(x/L-0.3) .^2) .*( -2*M/L^2)+exp(-M*(x/L-0.3) .^2) .*((x/L-0.3) .*(-2*M/L)).^2;
68 err(t)=1/ s qr t (N)*norm(D2fapr -D2f);
69 end
70
71 % Graph of the error
72 s ubpl ot (2,2,s)
73 l ogl og (Nvect ,err ,o-);
74 gr i d on
75 xl abe l (N);
76 yl abe l (Discrete 2-norm of error)
77 t i t l e (M)
78
79 % "Order"
80 p= p o l y f i t (Nvect (1:pts(s)), l og (err(1:pts(s))) ,1);
81 M
82 10^p(1)
83 end
A.5 DN2221_lab5_Spectral_Derivative.m
1 f unc t i on Dfapr= DN2266_lab5_Spectral_Derivative(fk)
2 %SPECTRAL_DERIVATIVE: computes an approximation of the derivative of a periodic
3 %function using spectral interpolation.
4 % @INPUT: fk is the value of the function in points evenly distributed
5 % over a period of the function f.
6
7 % @OUTPUT: Dfapr is the approximation of the derivative in points where
8 % the value of f was given (in vector fk).
9
10 N= l engt h (fk);
11 k=-N/2:(N/2-1);
12 Fourier= f f t (fk);
13 Fourier= f f t s h i f t (Fourier);
14 DFourier=Fourier .*k*1i*2* pi /L;
15 DFourier=ifftshift(DFourier);
16 Dfapr= i f f t (DFourier);
17 end
A.6 DN2221_lab5_Q4b.m
1 %%
2 %DN2266_lab5_Q4b () computes the solution of the differential equation
25
3 %----
4 %u_t + 2*A*u*u_x + B*u_x = epsilon2*u_xx + epsilon3*u_xxx
5 %----
6 %on x in [0;L) and with initial condition u(x;t=0)=f(x), f fuch that
7 %f(x)=f(x+L)
8 %The numerical computation is performed by using a pseudo -spectral Gelerkin
9 %method with a finite centered Fourier series of 2^m coefficients and
10 %integrate over the time [0;T] with a fourth order Runge -Kutta method ,
11 %by using n time steps.
12 %@POST:
13 %- U is a (2^m)x(n+1) matrix that contains the numerical solution U(x;t)
14 %- N is a scalar that is 2^m
15 %- x is a Nx1 vector which contains the space points of computation
16 %%
17 f unc t i on [U,N,x] = DN2266_lab5_Q4b(A,B,epsilon2 ,epsilon3 ,L,m,T,n,f)
18
19 N = 2^m;
20 x = (0:N-1)*L/N;
21 xh = l i ns pac e (0 ,1 ,1000);
22 k = ((-N/2):(N/2-1));
23
24
25 dt_max= (2.8*L^2)/( pi ^2 * N^2);
26 %n = 2*ceil(T/dt_max);
27 dt= T/n;
28
29 t = l i ns pac e (0,T,n+1);
30
31 % Fourier coefficients
32 U = z e r os (N,n+1); %U(x;t)
33
34 %initial condition
35 U(:,1) = f(x);
36
37 % figure ()
38 % plot(x,U(:,1) ,.-);hold on
39 % %plot(xh,sin (2*pi*xh),g)
40 % xlabel(x)
41 % ylabel(Un(x;t))
42 % title({[A= num2str(A) , B= num2str(B) , epsilon2= num2str(epsilon2) , epsilon3=
num2str(epsilon3) , L= num2str(L) , m= num2str(m) , T= num2str(T)]; [dt= num2str(dt)
, dt_{max}= num2str(dt_max)];[t = num2str (0) [s]]});
43 % grid on
44 % axis equal
45
46 % RK4
47 f o r i = 1 : n
48
49 K1 = F(t(i),U(:,i));
50 K2 = F(t(i)+dt/2,U(:,i)+dt/2*K1);
51 K3 = F(t(i)+dt/2,U(:,i)+dt/2*K2);
52 K4 = F(t(i)+dt ,U(:,i)+dt*K3);
53
54 U(:,i+1) = U(:,i) + dt/6*(K1 + 2*K2 + 2*K3 + K4);
55
56
57 % drawnow
58 % clf
59 % plot(x,U(:,i+1) ,.-); hold on
60 % % plot(xh,exp(-4*pi*pi*t(i+1))*sin (2*pi*xh),g)
61 % xlabel(x)
62 % ylabel(Un(x;t))
63 % title({[A= num2str(A) , B= num2str(B) , epsilon2= num2str(epsilon2) , epsilon3=
num2str(epsilon3) , L= num2str(L) , m= num2str(m) ,T= num2str(T)]; [dt= num2str(dt) ,
dt_{max}= num2str(dt_max)];[t = num2str(t(i+1)) [s]]});
64 % grid on
65 % axis equal
66
67 end
68
69 f i g ur e ()
26
70 pl ot (x,U(:,end),.-); hol d on
71 %plot(xh ,exp(-4*pi*pi*t(end))*sin(2*pi*xh),g)
72 xl abe l (x)
73 yl abe l (Un(x;t))
74 t i t l e ({[A= num2str (A) , B= num2str (B) , epsilon2= num2str (epsilon2) , epsilon3=
num2str (epsilon3) , L= num2str (L) , m= num2str (m) , T= num2str (T)]; [dt= num2str (dt)
, dt_{max}= num2str (dt_max)];[t = num2str (t(end)) [s]]});
75 gr i d on
76 axi s equal
77
78
79 %%%%%%%%%%
80
81 % vector field
82 f unc t i on f = F(t,u)
83
84 f = epsilon2*D(D(u))+epsilon3*D(D(D(u))) -2*A*u.*D(u)-B*D(u);
85
86 end
87
88 %differential operator
89 f unc t i on d = D(u)
90
91 fourier = f f t (u);
92 fourier = f f t s h i f t (fourier);
93 df = fourier .*k*1i*2* pi /L;
94 df = ifftshift(df);
95 d = i f f t (df);
96
97 end
98
99 end
A.7 DN2221_lab5_Q4d.m
1 %%
2 %DN2266_lab5_Q4b () computes the solution of the differential equation
3 %----
4 %u_t + 2*A*u*u_x + B*u_x = epsilon2*u_xx + epsilon3*u_xxx
5 %----
6 %on x in [0;L) and with initial condition u(x;t=0)=f(x), f fuch that
7 %f(x)=f(x+L)
8 %The numerical computation is performed by using a collocation method with
9 %a finite centered Fourier series of 2^m coefficients and integrate over
10 %the time [0;T] with a fourth order Runge -Kutta method , by using n time steps.
11 %@POST:
12 %- U is a (2^m)x(n+1) matrix that contains the numerical solution U(x;t)
13 %- N is a scalar that is 2^m
14 %- x is a Nx1 vector which contains the space points of computation
15 %%
16 f unc t i on [U,N,x] = DN2266_lab5_Q4d(A,B,epsilon2 ,epsilon3 ,L,m,T,n,f)
17
18
19
20 N = 2^m;
21 x = (0:N-1)*L/N;
22 xh = l i ns pac e (0 ,1 ,1000);
23 k = ((-N/2):(N/2-1));
24
25
26 dt_max= (2*L^2)/( pi ^2 * N^2);
27
28 %n = 2*ceil(T/dt_max);
29 dt= T/n;
30
31 t = l i ns pac e (0,T,n+1);
32
27
33 % Fourier coefficients
34 U = z e r os (N,n+1); %U(x;t)
35
36 %initial condition
37 U(:,1) = f f t (f(x));
38
39 f i g ur e ()
40 pl ot (x, i f f t (U(:,1)),.-); hol d on
41 %plot(xh ,sin(2*pi*xh),g)
42 xl abe l (x)
43 yl abe l (Un(x;t))
44 t i t l e ({[A= num2str (A) , B= num2str (B) , epsilon2= num2str (epsilon2) , epsilon3=
num2str (epsilon3) , L= num2str (L) , m= num2str (m) , T= num2str (T)]; [dt= num2str (dt)
, dt_{max}= num2str (dt_max)];[t = num2str (0) [s]]});
45 gr i d on
46 axi s equal
47
48 % Euler Forward
49 f o r i = 1 : n
50
51 U(:,i+1) = U(:,i) + dt* f f t s h i f t (F(t(i), f f t s h i f t (U(:,i))));
52
53 drawnow
54 c l f
55 pl ot (x, i f f t (U(:,i+1)),.-); hol d on
56 %plot(xh ,exp(-4*pi*pi*t(i+1))*sin(2*pi*xh),g)
57 xl abe l (x)
58 yl abe l (Un(x;t))
59 t i t l e ({[A= num2str (A) , B= num2str (B) , epsilon2= num2str (epsilon2) , epsilon3=
num2str (epsilon3) , L= num2str (L) , m= num2str (m) ,T= num2str (T)]; [dt= num2str (dt
) , dt_{max}= num2str (dt_max)];[t = num2str (t(i+1)) [s]]});
60 gr i d on
61 axi s equal
62
63 end
64
65
66 % figure ()
67 % plot(x,ifft(U(:,end)) ,.-);hold on
68 % %plot(xh,exp(-4*pi*pi*t(end))*sin(2*pi*xh),g)
69 % xlabel(x)
70 % ylabel(Un(x;t))
71 % title({[A= num2str(A) , B= num2str(B) , epsilon2= num2str(epsilon2) , epsilon3=
num2str(epsilon3) , L= num2str(L) , m= num2str(m) , T= num2str(T)]; [dt= num2str(dt)
, dt_{max}= num2str(dt_max)];[t = num2str(t(end)) [s]]});
72 % grid on
73 % axis equal
74
75 %%%%%%%
76
77 % vector field
78 f unc t i on f = F(t,u)
79
80 f = -2*A*pseudoSpectral(u) - B*u*2* pi *1i.*k/L ...
81 - epsilon2*u*4*( pi ^2) .*(k.^2)/(L^2)...
82 - epsilon3*u*8*( pi ^3) .*(k.^3)*1i/(L^3);
83
84 end
85
86 %pseudo spectral treatment
87 f unc t i on product = pseudoSpectral(uk)
88
89 product = f f t ( i f f t (uk).* i f f t (2* pi *1i.*k/L.*uk));
90
91 end
92
93
94
95 end
28

You might also like