Fourier series
fourier(f, [a, b]) computes the coefficients of the Fourier series of f on [a, b], each as a certified interval. f may be a formula or a piecewise function, so square waves and sawtooths are first-class.
const F = fourier("-1 if -1:x:!0; 1 if 0:x:1", [-1, 1], { terms: 9 }).exact();
F.b[1]; // 4/π, enclosed
F.partial(5); // the partial sum as a function, to plot
F.toString(3, 4); // "1.2732*sin(pi*x) + 0.4244*sin(3*pi*x)"
F.meanSquareError(5); // (1/L)·∫ (f − S₅)², proven
With L = b − a,
kind: "sine" or "cosine" gives the half-range series in πn(x − a)/L instead, which extends f as an odd or an even function of period 2L. These are the series that solve the heat and wave equations on an interval.
How the coefficients are enclosed
Each coefficient is an integral of f times a sine or cosine. On a piece with centre c and half-width h, f(c + hs) is expanded in powers of s to order 14 in fixed-point interval arithmetic; the even terms integrate exactly, and the coefficient of order 14, enclosed over the whole piece, bounds the rest. The widest piece is halved until the total is narrow enough. A piecewise function is integrated rule by rule between its breakpoints, so a jump costs nothing.
Parseval and the error of a partial sum
The mean square of f equals the sum of the squares of the coefficients:
The partial sum S_N is the best approximation by harmonics up to N in the mean-square sense, and its error is what is left of the sum. meanSquare() encloses the left side, and meanSquareError(N) subtracts the part of the right side that S_N uses. This is a proven statement about the whole function, not a sample of points. It says nothing about any single point: near a jump the partial sums overshoot by about 9% however many terms are taken (the Gibbs phenomenon), which F.partial(N).range(...) will show.
| call | returns |
|---|---|
| fourier(f, [a, b], { terms = 10, digits = 10, kind }) | R: preview { a, b } as numbers, exact FourierSeries |
| F.a[n], F.b[n], F.coefficient(n), F.amplitude(n) | RationalInterval; a[0] is a₀ |
| F.partial(N) | AnalyticFunction (coefficients at the midpoints) |
| F.value(x, N) | the partial sum at a rational point, certified |
| F.meanSquare(), F.meanSquareError(N) | RationalInterval |
| F.toString(N, places), F.period, F.length, F.kind, F.terms |
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.