Gibbs Phenomenon¶

Author: S.K. Cheng (GitHub)¶

Date: 2024-07-23¶

Introduction¶

Last time, we talked about Chebyshev polynomials and how they can be used to approximate functions. In this notebook, we will discuss another phenomenon that occurs when approximating functions.

To begin with, let us consider the Chebyshev approximation of a function $f(x)$ on the interval $[-1,1]$. The Chebyshev approximation of degree $n$ is given by $$ f_n(x) = \sum_{k=0}^{n} a_k T_k(x), $$ where $T_k(x)$ is the $k$-th Chebyshev polynomial of the first kind and $a_k$ are the Chebyshev coefficients. The Chebyshev coefficients can be computed using the formula $$ a_k = \frac{2}{\pi} \int_{-1}^{1} \frac{f(x) T_k(x)}{\sqrt{1-x^2}} dx. $$

The Chebyshev approximation is known to converge to $f(x)$ at a rate of $1/n$. However, the Chebyshev approximation also exhibits a phenomenon known as the Gibbs phenomenon. The Gibbs phenomenon is characterized by oscillations near the discontinuities of the function $f(x)$. In this notebook, we will explore the Gibbs phenomenon and its implications for function approximation.

In [ ]:
# First, consider the sign function. The sign function is defined as follows:
# sign(x) = -1 if x < 0, 0 if x = 0, 1 if x > 0.
import numpy as np
import matplotlib.pyplot as plt

# Define the sign function
def sign(x):
    return np.vectorize(lambda x: -1 if x < 0 else (0 if x == 0 else 1))(x)
    
# Chebyshev Nodes
def cheb_nodes(n):
    return np.cos((2*np.arange(1, n+1)-1)*np.pi/(2*n))

# Chebyshev Interpolation for different number of nodes
plt.figure(figsize=(12,8),dpi=100)
plt.title('Chebyshev Interpolation for a Custom Function')
x = np.linspace(-1, 1, 10000)
plt.plot(x, sign(x), color = 'salmon', label='Custom Function')
for n in range(5, 41, 5):
    y = sign(cheb_nodes(n))
    p2 = np.polynomial.chebyshev.chebfit(cheb_nodes(n), y, n-1)
    plt.plot(x, np.polynomial.chebyshev.Chebyshev(p2)(x), label='Chebyshev Interpolation for n = {}'.format(n), linewidth=2, linestyle='--', alpha=0.7)
plt.legend()
plt.xlabel('x', fontsize=12)
plt.ylabel('f(x)', fontsize=12)
plt.show()
No description has been provided for this image

As we can see from the figure, the interpolants exhibit oscillations near the discontinuities of the function. These oscillations are known as the Gibbs phenomenon. The Gibbs phenomenon is a common feature of function approximation and can be observed in a wide range of applications. In this notebook, we will explore the Gibbs phenomenon in more detail and discuss its implications for function approximation.

Gibbs Phenomenon¶

The Gibbs phenomenon is a common feature of function approximation and can be observed in a wide range of applications. The Gibbs phenomenon is characterized by oscillations near the discontinuities of a function. These oscillations occur when approximating a function using a finite number of terms in a series expansion.

One may note that when using Fourier series to approximate a function, the Gibbs phenomenon is particularly pronounced near the discontinuities of the function. The Gibbs phenomenon is a result of the oscillatory nature of the Fourier series and the inability of a finite number of terms to capture the sharp transitions in the function.

However, the Gibbs phenomenon is not limited to Fourier series and can occur in other types of function approximation as well. For example, the Chebyshev approximation of a function can also exhibit the Gibbs phenomenon near the discontinuities of the function.

To illustrate the Gibbs phenomenon, considering the same function $\operatorname{sign}(x)$, we also can conclude that Chebyshev polynomials oscillate near the discontinuities of the function. Let $p_n$ be the degree $n$ Chebyshev interpolant of the function $\operatorname{sign}(x)$ on the interval $[-1,1]$. Then as $n\to \infty$: $$ \begin{aligned} \lim_{n\to \infty, \mathrm{odd}} p_n(x) &= c_1 = 1.28228345577542854813\dots, \\ \lim_{n\to \infty, \mathrm{even}} p_n(x) &= c_2 = 1.06578388826644809905\dots. \end{aligned} $$

Also, if $f_n$ is the degree $n$ Chebyshev pojection of the function $\operatorname{sign}(x)$ on the interval $[-1,1]$, then as $n\to \infty$: $$ \lim_{n\to \infty} \|f_n\| = \frac{2}{\pi}\int_{0}^{\pi} \frac{\sin x}{x}dx = 1.178979744472167\dots. $$

Conclusion¶

Gibbs Phenomenon: Any polynomial and trigonometric approximations tend to oscillate near discontinuities. The oscillations decay algebraically, not exponentially, with distance from the discontinuity.