An archived instance of discourse for discussion in undergraduate PDE.

Illustration of a Fourier series

Mark

In class, we showed that
$$x(1-x) = \frac{8}{\pi ^3} \sum _{n=0}^{\infty }
\frac{\sin ((2 n+1) \pi x)}{(2
n+1)^3},$$
over the interval $0 \leq x \leq 1$. How can we illustrate this convergence in Mathematica?




Note: A clear and correct answer containing an animated GIF and code that others can execute by simply copying and pasting into a notebook could earn an Awesome Badge. The animation should look something like the penultimate result in our Intro Fourier notebook. You will probably find that you need only a very few terms to get a good approximation.

dwillia2

Just the animation, this code is slightly different than the final version:

Animate[
   Plot[{x(1-x),
    8/Pi^3 Sum[
      Sin[n Pi x]/n^3, {n, 1, a, 2}]},
    {x, -.2,1.2}, PlotRange -> {-.2, .3}],
 {a, 1, 5, 2}]

Begin Post:

The first thing to do is to graph $x(1-x)$ which is a parabola of height $1/2$ and intercepts at $0,$ and $1$.

Plot[x (1 - x), {x, -.2, 1.2}, PlotRange -> {-.2, .5}]

Noitice how similar this looks to $sin(\pi x)$. Since Fourier series are made out of $sin$ we expect a very good approximation.

We then want to stack the Fourier series onto this image, but we also want to animate it. Lets worry about the Fourier series before the animation.
The method I used was first defining the individual terms $u_n$ and then using the Sum command. It will help us to define the individual terms so we can look at each one separate from the series as a whole.

fourierTerms[n_, x_] = 8/Pi^3 Sin[(2 n + 1) Pi x]/(2 n + 1)^3;
series[a_,x_]:=Sum[fourierTerms[n,x],{n,0,a,1}]

Why don't we check to see this is what we expect. Lets graph the first 3 terms.

Plot[{
  fourierTerms[0, x],
  fourierTerms[1, x],
  fourierTerms[2, x]},
 {x, -.2, 1.2}, PlotRange -> {-.2, .3}]

Good, this makes sense. Now that we have everything we can toss an animate around it and go!

Note we only used the first 3 terms because the approximation is so good we don't really need any more.

Animate[
 Plot[
  {x (1 - x),
   series[a, x]},
  {x, -.2, 1.2}, PlotRange -> {-.2, .3}],
 {a, 0, 2, 1}
]

Tada!

gigaliciousness

I goofed and solved for the steady state initially, but the following code works for the Fourier Series.
For those interested, the code for the steady state was kept and moved to the bottom of this individual post

Interesting, this is the code that I got,

Animate[
    Plot[
        {x(1-x),
         Sum[(8(Pi+2n*Pi)^-3)Sin[(2n+1)Pi*x],{n,0,m}]}, 
        {x,-.05,1.05},PlotRange -> {-.05,.275}], 
    {m,0,10,1}]

Ninja-edit: Upon further discussion with Dylan I have apparently misread the problem and just solved the equation. Whoops!
Ninja-edit 2: Fixed/changed code to plot emergence of function from Fourier series rather than the steady state function, for those who'd still want to see the steady state, here:

Steady-State Code Below

 Animate[
    Plot[
        {x(1-x),
         Sum[(8(Pi+2n*Pi)^-3)Exp[-(((2n+1)Pi)^2)t]Sin[(2n+1)Pi*x], 
            {n,0,50}]},
        {x,0,1},PlotRange -> {0,.275}], 
    {t,0,.5,.001}]
Mark

I like it so far! I edited your answer for formatting. You can hit the edit button to see how I did that. I do recommend a PlotRange option to maintain stability of the plot as the animation progresses.

dwillia2

I was looking at the series Fourier and showing it converged to $x(1-x)$. I think you were looking at it from the steady state perspective.

gigaliciousness

Yea...that's probably it.

Guess I was just stuck in steady-state gear. Time to re-do!

dwillia2

Also in case anyone is interested in how close of an approximation this is, here are some values with four terms of the Fourier series:

$$\left(
\begin{array}{cccc}
\text{x} & \text{x(1-x)} & \text{Fourier Series} & \text{Percent Error} \\
0.1 & 0.09 & 0.0901338 & -0.148679 \\
0.2 & 0.16 & 0.160029 & -0.0179439 \\
0.3 & 0.21 & 0.209858 & 0.0677922 \\
0.4 & 0.24 & 0.24021 & -0.0872997 \\
0.5 & 0.25 & 0.249768 & 0.0927436 \\
0.6 & 0.24 & 0.24021 & -0.0872997 \\
0.7 & 0.21 & 0.209858 & 0.0677922 \\
0.8 & 0.16 & 0.160029 & -0.0179439 \\
0.9 & 0.09 & 0.0901338 & -0.148679 \\
\end{array}
\right)$$












Mark

Neato! I think. What is the order of the Fourier series you're using?

dwillia2

I used the first three terms for the animation. The final code was actually not using that, but I fixed it. For the numerical approximation I used the first 4 terms of the series.