# Finding Derivatives using Fourier Spectral Methods

Spectral methods are a class of numerical techniques that often utilize the FFT. Spectral methods can be implemented easily in Matlab, but there are some conventions to note. First note that Matlab’s “fft” and “ifft” functions store wave numbers in a different order than has been used so far. The wave numbers in Matlab and in most other FFT packages are ordered, $0,1,...,{\frac {n}{2}},-{\frac {n}{2}}+1,-{\frac {n}{2}}+2,...,-1$. Secondly, Matlab does not take full advantage of real input data. The DFT of real data $u$ satisfies the symmetry property ${\hat {u}}(-k)={\hat {u}}(k)$, so it is only necessary to compute half of the wave numbers. Matlab’s ``fft" command does not take full advantage of this property and wastes memory storing both the positive and negative wave numbers. Third, spectral accuracy (exponential decay of the magnitude of the Fourier coefficients) is better for smooth functions, so where possible be sure your initial conditions are smooth – When using a Fourier spectral method this requires that your initial conditions are periodic.

## Taking a Derivative in Fourier Space

Let $u_{j}$ be the discrete approximation of a function $u(x)$ which is sampled at the $n$ discrete points $x_{j}\in {h,2h,...,ih,..,2\pi -h,2\pi }$ where $h=2\pi /n$. Now take the Fourier Transform of $u_{j}$ so that ${\text{FFT}}(u_{j})\equiv {\hat {u}}_{k}\qquad {\text{where}}\quad k\in {{\frac {-n}{2}}+1,...{\frac {n}{2}}}.$ The Fourier transform of ${\frac {\partial ^{\nu }u_{j}}{\partial x^{\nu }}}$ can then be computed from ${\hat {u}}_{k}$ as shown below:
    ${\text{FFT}}({\frac {\partial ^{\nu }u_{j}}{\partial x^{\nu }}})\equiv (ik)^{\nu }{\hat {u}}_{k}$         ( 1)
where ${\hat {u}}_{n/2}=0$ if $\nu$ is odd. More details can be found in Trefethen.
Thus, differentiation in real space becomes multiplication in Fourier space. We can then take the inverse fast Fourier Transform (IFFT) to yield a solution in real space. In the next section we will use this technique to implement forward Euler and backward Euler timestepping schemes to compute solutions for several PDEs.
$f(x)=\sin ^{2}\left(wx\right)$
$(1,1+2\pi ]$

### Exercises

* Let $u(x)=\sum _{k}{\hat {u}}_{k}\exp(ikx)$ be the Fourier series representation of a function $u(x)$. Explain why ${\frac {\mathrm {d} ^{\nu }u}{\mathrm {d} x^{\nu }}}=\sum (ik)^{\nu }{\hat {u}}_{k}\exp(ikx),$ provided the series converges.
* Consider the linear KdV equation $u_{t}+u_{xxx}=0$ with periodic boundary conditions for $x\in (0,2\pi ]$ and the initial data $u(x,0)=0\quad {\text{ if }}\quad 0<x\leq \pi$ and $u(x,0)=1\quad {\text{ if }}\quad \pi <x\leq 2\pi$
* Using separation of variables, show that the “solution” is $u(t,x)={\frac {1}{2}}-{\frac {2}{\pi }}\sum _{j=0}^{\infty }{\frac {\sin((2j+1)x-(2j+1)^{3}t)}{2j+1}}.$ Quotation marks are used because the expression for the solution that is given does not converge when differentiated either once in time or twice in space.
* As explained by Olver, this solution has a fractal structure at times that are an irrational multiple of $\pi$ and a quantized structure at times that are rational multiples of $\pi$. The Matlab program in listing B uses the Fast Fourier transform to find a solution to the linearized KdV equation. Explain how this program finds a solution to the linearized KdV equation.
* Compare the numerical solution produced by the Matlab program with the analytical solution. Try to determine which is more accurate and see if you can find evidence or an explanation to support your suggestions.

## Notes

* ↑ Trefethen (2000)
* ↑ This question was prompted by an REU and UROP project due to Sudarshan Balakrishan which is available at http://www.lsa.umich.edu/math/undergrad/researchandcareeropportunities/infinresearch/pastreuprojects.
* ↑ Olver (2010)

## References

Olver, P.J. (2010). "Dispersive Quantization". American Mathematical Monthly. 117: 599–610. {{cite journal}}: Cite has empty unknown parameter: |coauthors= (help)
Trefethen, L.N. (2000). Spectral Methods in Matlab. SIAM.