The Weierstrass function

(2 comments)

The Weierstrass function, named after the German mathematician Karl Weierstrass (1815 – 1897) is a real-valued function that is continuous everywhere but nowhere differentiable. It is usually expressed as a Fourier series:

$$ f(x) = \sum_{k=0}^\infty a^k \cos \left( b^k\pi x\right), $$

where $0 < a < 1$ and $b$ is a positive, odd integer satisfying $ab > 1 + 3\pi / 2$.

The code below plots the Weierstrass function for $a=0.3, b =23$ and indicates, in inset Axes, a zoomed-in region.

Weierstrass function

import numpy as np
import matplotlib.pyplot as plt

# Number of points to plot.
N = 1000
# Parameters of this particular Weierstrass function, f(x).
# b must be an odd integer and 0 < a < 1.
a, b = 0.3, 23
# The condition for f(x) to be continuous but nowhere differentiable:
assert a*b > 1 + 3 * np.pi / 2

def get_W(x, f=0, nterms=50):
    """Return the Weierstrass function at x."""
    for k in range(nterms):
        term = a**k * np.cos(b**k * np.pi * x)
        f += term
    return f

def get_f(Dx, cx=0, nterms=50):
    """
    Return the Weierstrass function on the interval [-Dx/2, Dx/2], centered
    at cx, using nterms to approximate the infinite sum. x can be an array.

    """

    x = np.linspace(cx - Dx / 2, cx + Dx / 2, N)
    f = np.zeros(N)
    f = get_W(x, f, nterms)
    return x, f

# The "outside" plot.
Dx = 4
x, y = get_f(Dx)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.axis('square')

# The inset plot.
axin = ax.inset_axes([0.6, 0.6, 0.4, 0.4])
Dx_in, cx_in = 0.1, -0.5
xin, yin = get_f(Dx_in, cx_in)
axin.plot(xin, yin)
axin.set_xticks([])
axin.set_yticks([])
ax.indicate_inset_zoom(axin, edgecolor="black")

plt.show()
Current rating: 2.3

Comments

Comments are pre-moderated. Please be patient and your comment will appear soon.

Doug Brown 1 year, 11 months ago

The animation is not needed

Link | Reply
Currently unrated

christian 1 year, 11 months ago

True! I have removed this statement.

Link | Reply
Currently unrated

New Comment

required

required (not published)

optional

required