0% found this document useful (0 votes)
140 views5 pages

A Pseudospectral Method For Periodic Functions

This document describes a pseudospectral method for solving periodic functions and partial differential equations (PDEs) using a Fourier basis. It presents the discrete Fourier transform and inverse discrete Fourier transform used to approximate functions and their derivatives at grid points. Problems are provided to apply this method to approximate the derivative of a sample function and solve an initial value problem for the advection equation with a variable wave speed. Code is given as an example to numerically solve the problems.

Uploaded by

Josue Cofee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views5 pages

A Pseudospectral Method For Periodic Functions

This document describes a pseudospectral method for solving periodic functions and partial differential equations (PDEs) using a Fourier basis. It presents the discrete Fourier transform and inverse discrete Fourier transform used to approximate functions and their derivatives at grid points. Problems are provided to apply this method to approximate the derivative of a sample function and solve an initial value problem for the advection equation with a variable wave speed. Code is given as an example to numerically solve the problems.

Uploaded by

Josue Cofee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 1

A Pseudospectral method
for periodic functions

Lab Objective: We look at a pseudospectral method with a Fourier basis, and


numerically solve the advection equation using a pseudospectral discretization in
space and a Runge-Kutta integration scheme in time.

Let f be a periodic function on [0, 2π]. Let x1 , . . . , xN be N evenly spaced grid


points on [0, 2π]. Since f is periodic on [0, 2π], we can ignore the grid point xN = 2π.
We will further assume that N is even; similar formulas can be derived for N odd.
Let h = 2π/N ; then {x0 , . . . , xN −1 } = {0, h, 2h, . . . , 2π − h}.
The discrete Fourier transform (DFT) of f , denoted by fˆ or F(f ), is given by
N
X −1
fˆ(k) = h e−ikxj f (xj ) where k = −N/2 + 1, . . . , 0, 1, . . . , N/2.
j=0

The inverse DFT is then given by


N/2
1 X eikxj ˆ
f (xj ) = f (k), j = 0, . . . , N − 1, (1.1)
2π ck
k=−N/2

where
(
2 if k = −N/2 or k = N/2,
ck = (1.2)
1 otherwise.

The inverse DFT can then be used to define a natural interpolant (sometimes called
a band-limited interpolant) by evaluating (1.1) at any x rather than xj :
N/2
1 X
p(x) = eikx fˆ(k). (1.3)

k=−N/2

The interpolant for f 0 is then given by


N/2−1
1 X
p0 (x) = ikeikx fˆ(k). (1.4)

k=−N/2+1

1
2 Lab 1. A Pseudospectral method for periodic functions

Consider the function u(x) = sin2 (x) cos(x) + e2 sin(x+1) . Using (1.4), the deriva-
tive u0 may be approximated with the following code. 1 We note that although we
only approximate u0 at the Fourier grid points, (1.4) provides an analytic approxi-
mation of u0 in the form of a trigonometric polynomial.
import numpy as np
from scipy.fftpack import fft, ifft
import matplotlib.pyplot as plt

N=24
x1 = (2.*np.pi/N)*np.arange(1,N+1)
f = np.sin(x1)**2.*np.cos(x1) + np.exp(2.*np.sin(x1+1))

# This array is reordered in Python to


# accomodate the ordering inside the fft function in scipy.
k = np.concatenate(( np.arange(0,N/2) ,
np.array([0]) , # Because hat{f}'(k) at k = N/2 is zero.
np.arange(-N/2+1,0,1) ))

# Approximates the derivative using the pseudospectral method


f_hat = fft(f)
fp_hat = ((1j*k)*f_hat)
fp = np.real(ifft(fp_hat))

# Calculates the derivative analytically


x2 = np.linspace(0,2*np.pi,200)
derivative = (2.*np.sin(x2)*np.cos(x2)**2. -
np.sin(x2)**3. +
2*np.cos(x2+1)*np.exp(2*np.sin(x2+1))
)

plt.plot(x2,derivative,'-k',linewidth=2.)
plt.plot(x1,fp,'*b')
plt.savefig('spectral2_derivative.pdf')
plt.show()

Problem 1. Consider again the function u(x) = sin2 (x) cos(x) + e2 sin(x+1) .
Create a function that approximates 12 u00 − u0 on the Fourier grid points for
N = 24.

The advection equation


Recall that the advection equation is given by
ut + cux = 0 (1.5)
where c is the speed of the wave (the wave travels to the right for c > 0). We
will consider the solution of the advection equation on the circle; this essentially
amounts to solving the advection equation on [0, 2π] and assuming periodic bound-
ary conditions.
1 See Spectral Methods in MATLAB by Lloyd N. Trefethen. Another good reference is Cheby-

shev and Fourier Spectral Methods by John P. Boyd.


3

8
0 1 2 3 4 5 6 7

Figure 1.1: The derivative of u(x) = sin2 (x) cos(x) + e2 sin(x+1) .

A common method for solving time-dependent PDEs is called the method of


lines. To apply the method of lines to our problem, we use our Fourier grid points in
[0, π]: given an even N , let h = 2π/N , so that {x0 , . . . , xN −1 } = {0, h, 2h, . . . , 2π −
h}. By using these grid points we obtain the collection of equations

ut (xj , t) + cux (xj , t) = 0, t > 0, j = 0, . . . N − 1. (1.6)

−1
Let U (t) be the vector valued function given by U (t) = (u(xj , t))N j=0 . Let
F(U )(t) denote the discrete Fourier transform of u(x, t) (in space), so that
N/2
F(U )(t) = (û(k, t))k=−N/2+1 .

Define F −1 similarly. Using the pseudospectral approximation in space leads to the


system of ODEs
 
Ut + ~cF −1 i~kF(U ) = 0 (1.7)

where ~k is a vector, and ~kF(U ) denotes element-wise multiplication. Similarly ~c


could also be a vector, if the wave speed c is allowed to vary.
4 Lab 1. A Pseudospectral method for periodic functions

Problem 2. Using a fourth order Runge-Kutta method (RK4), solve the


initial value problem

ut + c(x)ux = 0, (1.8)
2
where c(x) = .2 + sin2 (x − 1), and u(x, t = 0) = e−100(x−1) . Plot your
numerical solution from t = 0 to t = 8 over 150 time steps and 100 x steps.
Note that the initial data is nearly zero near x = 0 and 2π, and so we can
use the pseudospectral method. a Use the following code to help graph.
t_steps = 150 # Time steps
x_steps = 100 # x steps

'''
Your code here to set things up
'''

sol = # RK4 method. Should return a t_steps by x_steps array

X,Y = np.meshgrid(x_domain, t_domain)


fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.plot_wireframe(X,Y,sol)
plt.show()

a This problem is solved in Spectral Methods in MATLAB using a leapfrog discretization

in time.
5

Figure 1.2: The solution of the variable speed advection equation; see Problem 2.

You might also like