Leibniz Formula from a Fourier Series

The Leibniz series

$$ \frac{\pi}{4}=1-\frac13+\frac15-\frac17+\cdots $$

follows by evaluating the Fourier series of the sawtooth $f(x)=x$ at $x=\pi/2$. This notebook derives the coefficients symbolically before making that substitution.

The sawtooth and its coefficients

Since $f$ is odd, only sine terms occur:

$$ x=\sum_{k=1}^{\infty}b_k\sin(kx),\qquad b_k=\frac1\pi\int_{-\pi}^{\pi}x\sin(kx)\,dx. $$

declare symbol x, n : MathValue

def f (x : MathValue) : MathValue := x

def cosinePrimitive (k : MathValue) : MathValue :=
  x * sin (k * x) / k + cos (k * x) / k^2

def sinePrimitive (k : MathValue) : MathValue :=
  (- x) * cos (k * x) / k + sin (k * x) / k^2
def cosineCoefficients : [MathValue] :=
  map
    (\k ->
      let primitive := cosinePrimitive k
       in (substitute [(x, π)] primitive - substitute [(x, - π)] primitive) / π)
    nats

def sineCoefficients : [MathValue] :=
  map
    (\k ->
      let primitive := sinePrimitive k
       in (substitute [(x, π)] primitive - substitute [(x, - π)] primitive) / π)
    nats
take 10 sineCoefficients
$\{2, -1, \frac{2}{3}, \frac{-1}{2}, \frac{2}{5}, \frac{-1}{3}, \frac{2}{7}, \frac{-1}{4}, \frac{2}{9}, \frac{-1}{5}\}$

Fourier terms

Egison now combines each coefficient with its basis function.

def fourierTerms : [MathValue] :=
  map (\(k, b) -> b * sin (k * x)) (zip nats sineCoefficients)
take 10 fourierTerms
$\{2 \sin(x), -\sin(2 x), \frac{2}{3} \sin(3 x), \frac{-1}{2} \sin(4 x), \frac{2}{5} \sin(5 x), \frac{-1}{3} \sin(6 x), \frac{2}{7} \sin(7 x), \frac{-1}{4} \sin(8 x), \frac{2}{9} \sin(9 x), \frac{-1}{5} \sin(10 x)\}$

Evaluate at $x=\pi/2$

Even harmonics vanish, while successive odd harmonics alternate in sign. We encode the exact four-step pattern

$$ \sin(k\pi/2)=1,0,-1,0,\ldots $$

before dividing the Fourier terms by two. Thus the identity $\pi/2=2(1-1/3+1/5-\cdots)$ gives the desired series.

def sinAtHalfPi (k : Integer) : MathValue :=
  if isEven k
    then 0
    else (-1) ^ (i.quotient (k - 1) 2)

def leibnizTerms : [MathValue] :=
  map
    (\(k, b) -> b * sinAtHalfPi k / 2)
    (zip nats sineCoefficients)
take 10 leibnizTerms
$\{1, 0, \frac{-1}{3}, 0, \frac{1}{5}, 0, \frac{-1}{7}, 0, \frac{1}{9}, 0\}$

Reading the nonzero entries yields $1,-1/3,1/5,-1/7,\ldots$. Their infinite sum is $\pi/4$; the zeros record the even Fourier modes that vanish at $\pi/2$.

Links

Back to the Table of Contents