# Computing the minimum number of terms required in a Fourier series to achieve a particular upper bound on the error

In a Fourier series, the maximum error bound is the difference of the function and the partial sum of its Fourier series. Within an interval, as we increase the number of terms of partial sums, the error decreases.

$$e(x) = \left|f(x) − s(x)\right|$$

How can I determine the number of terms needed to calculate the partial sum so as to have a specific maximum error bound? For e.g $|e(x)| ≤ 0.01$ or $|e(x)| ≤ 0.001$

For example in a typical question, $f(x)$ defined as

$$f (x) = \begin{cases} 0 &−3 \leq x \leq 0\\ x^2(3 − x) & 0 < x < 3 \end{cases}$$

is a periodic function with period $6$ i.e., $f (x + 6) = f (x)$

• Plot $|e(x)|$ versus $x$ for $0 ≤ x ≤ 3$ for several values of $m$.
• Find the smallest value of $m$ for which $|e(x)| ≤ 0.1$ for all $x$.

Constructing the Fourier series for this periodic function and plotting $|e(x)|$ versus $x$ for several partial sums like $m = 5, 10, 20$ is easy. It can be accomplished by DiscretePlot.

How can I find the smallest value of $m$ (i.e., the number of terms needed in the partial sum to achieve a specific error bound)?

I use Mathematica 6 and 7.

-
I think this is more a math question than a Mathematica one... –  Ｊ. Ｍ. May 24 '12 at 15:44
@J.M. likely, but I wonder if a programmatic solution could be devised. –  rcollyer May 24 '12 at 15:50
Suppose i have to calculate m, that is number of terms needed for partial sum so as to obtain error bound of very small order. Like 0.00001. Then i have to calculate partial sum to a high degree. maybe to m = 100. –  kevin May 24 '12 at 15:52
In the interest of teaching you how to fish: try FourierTrigSeries[UnitStep[x] (3 - x) x^2, x, n, FourierParameters -> {1, Pi/3}], where n is the number of terms you need for the Fourier series. –  Ｊ. Ｍ. May 24 '12 at 16:02
Right, so make plots of the difference between the function and the Fourier series, for an increasing number of terms. Note the numbers in the vertical axis. –  Ｊ. Ｍ. May 24 '12 at 16:07

If function $f$ has period $2b$ and is defined on $(-b,b)$ then

b = 3;
f [x_] := x^2 (3 - x) Boole[0 < x < b]
Clear[g]
g[n_, x_] := g[n, x] = FourierTrigSeries[f[x], x, n, FourierParameters -> {1, \[Pi]/b}] //
FullSimplify
maxdif[n_] := NMaximize[{Abs[f[x] - g[n, x]], {-b <= x < b}}, x][[1]]


gives function maxdif calculating the maximum of the absolute value between $f$ and the $n$-th partial Fourier sum. From here it is straightforward to write code calculating the required minimal value of $n$.

-
There's a FourierTrigSeries[] function which would be more appropriate here, I think... –  Ｊ. Ｍ. May 24 '12 at 16:28
@J.M. Thanks, changed it. –  Andrew May 24 '12 at 16:33
Thanks. But i still don't understand how to calculate n for desired error bound. –  kevin May 24 '12 at 17:30
@kevin If we have a function depending on $n$ the simplest way is to write a cycle, which would check maxdif[n] until the required value will be found. Or one can try maxdif[2^m], $m=1,2,\ldots$ until getting the bound and then using bisection method. –  Andrew May 24 '12 at 17:40
The problem is that i am not proficient with Numerical analysis package of Mathematica because its not part of my course yet.Though i understood the basic idea you stated. I do not understand how to apply it in Mathematica. –  kevin May 24 '12 at 17:47
In general, this is not possible (though it is with your example function). Fourier series minimize error with respect to the mean squared error ($L_2$ norm), which is not the same as the max absolute error that you want ($L_\infty$ norm). The mean squared error goes to zero in the limit, but the max error may not -- any function with a step discontinuity will always have a non-zero max error in its Fourier series near the discontinuity (Gibbs phenomenon), no matter how many terms you take. So for a large class of functions, your error criterion is effectively useless. Since your function doesn't have such discontinuities, I think the max error will eventually go to zero, but I'm not certain.
$$e^2 = \int_{-b}^b |f(x)|^2 dx - \sum_{n=-N}^N |\phi_n|^2 |F[n]|^2$$
where $F[n]$ is a Fourier coefficient, and the $\phi_k$ depends on the particular definition of Fourier series that you're using (which you didn't specify). This comes directly from Bessel's inequality.