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.

  • 2
    $\begingroup$ I think this is more a math question than a Mathematica one... $\endgroup$ Commented May 24, 2012 at 15:44
  • 1
    $\begingroup$ @J.M. likely, but I wonder if a programmatic solution could be devised. $\endgroup$
    – rcollyer
    Commented May 24, 2012 at 15:50
  • $\begingroup$ 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. $\endgroup$
    – kevin
    Commented May 24, 2012 at 15:52
  • 2
    $\begingroup$ 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. $\endgroup$ Commented May 24, 2012 at 16:02
  • 1
    $\begingroup$ 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. $\endgroup$ Commented May 24, 2012 at 16:07

2 Answers 2


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]
g[n_, x_] := g[n, x] = FourierTrigSeries[f[x], x, n, FourierParameters -> {1, \[Pi]/b}] // 
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$.

  • $\begingroup$ There's a FourierTrigSeries[] function which would be more appropriate here, I think... $\endgroup$ Commented May 24, 2012 at 16:28
  • $\begingroup$ @J.M. Thanks, changed it. $\endgroup$
    – Andrew
    Commented May 24, 2012 at 16:33
  • $\begingroup$ Thanks. But i still don't understand how to calculate n for desired error bound. $\endgroup$
    – kevin
    Commented May 24, 2012 at 17:30
  • $\begingroup$ @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. $\endgroup$
    – Andrew
    Commented May 24, 2012 at 17:40
  • $\begingroup$ 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. $\endgroup$
    – kevin
    Commented May 24, 2012 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.

Fortunately, if you change your error criterion to the mean squared error, you can use a simple formula to find it exactly using only the Fourier coefficients.

$$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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.