Visualizing Functional Analysis with Python
I’ll provide an example problem in functional analysis and solve it using $Python$.
I’ll include mathematical notations in $TeX$, source code, and visualize the results with graphs.
Functional Analysis Example: Approximating Functions with Fourier Series
Let’s consider a fundamental problem in functional analysis: approximating a function using a Fourier series.
We’ll examine how well we can approximate a square wave function using different numbers of terms in its Fourier series.
Mathematical Background
A square wave function $f(x)$ on the interval $[-\pi, \pi]$ can be defined as:
$$
f(x) = \begin{cases}
1, & \text{if } 0 < x < \pi \
-1, & \text{if } -\pi < x < 0
\end{cases}
$$
The Fourier series of this function is:
$$
f(x) \approx \frac{4}{\pi} \sum_{n=1,3,5,…}^{\infty} \frac{1}{n} \sin(nx)
$$
This is a perfect example to demonstrate how increasing the number of terms improves approximation, a key concept in functional spaces.
Let’s implement this in $Python$ and visualize the results:
1 | import numpy as np |
Code Explanation
The code implements a Fourier series approximation of a square wave function.
Here’s a breakdown of what it does:
Square Wave Definition:
We define a square wave function that returns $1$ for positive $x$ and $-1$ for negative x.Fourier Approximation Function:
This implements the Fourier series formula I mentioned earlier, summing only the odd terms as per the mathematical derivation.Multiple Approximations:
We calculate several approximations with different numbers of terms $(1, 3, 5, 11, 21, 51, 101)$.Error Calculation:
For each approximation, we compute the L2 error (root mean square error) compared to the true function.Visualizations:
- The original square wave
- Different approximations overlaid on the original function
- L2 error vs. number of terms on a log-log scale
- A heatmap showing how the approximation evolves with increasing terms
Animation:
Shows how the approximation improves as we add more terms.Convergence Analysis:
We fit a power law to the error values to determine the convergence rate of the Fourier series.
Results and Analysis
Convergence rate: O(n^-0.400)
The visualizations show several important aspects of functional approximation:
Approximation Quality:
As we increase the number of terms in the Fourier series, the approximation gets closer to the true square wave. With just $1$ term, we have a sine wave.
With $101$ terms, we have a reasonable approximation of the square wave, though with visible Gibbs phenomenon (oscillations near the discontinuities).L2 Error Decay:
The log-log plot of error vs. number of terms shows that the error decreases approximately as $O(n^{(-0.5)})$, which matches theoretical expectations for Fourier series approximation of discontinuous functions.Function Space Visualization:
The heatmap shows how the approximation evolves across the entire domain as we add more terms, providing insight into the convergence properties in function space.Gibbs Phenomenon:
Even with many terms, there’s oscillation near the discontinuities (at $x = 0$), which is the famous Gibbs phenomenon. This illustrates a fundamental limitation in Fourier approximation of discontinuous functions.
Functional Analysis Insights
This example demonstrates several key concepts in functional analysis:
Approximation in Function Spaces:
How functions can be approximated using orthogonal bases (in this case, the Fourier basis).Convergence Properties:
The rate of convergence depends on the smoothness of the function being approximated.L2 Norm:
We used the L2 norm (square root of the mean squared error) to measure the distance between functions, which is fundamental in Hilbert spaces.Completeness of Basis:
The Fourier basis is complete in L2$[-π,π]$, meaning any square-integrable function can be approximated to arbitrary precision.
The visualization clearly shows that even though we need infinitely many terms for perfect representation, we can achieve practical approximations with a finite number of terms.
This demonstrates the power of functional analysis in providing theoretical frameworks for practical computational methods.