Ahoy! I'm trying to create a Fourier series visualization tool that allows me to interactively view a Fourier series as a function of the number of terms using Manipulate, but every time I run it, Mathematica becomes crazy slow and in most cases aborts the cell automatically. Here is the command I'm using right now:

f = \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 1\), \(m\)]\(
\*FractionBox[\(4\), \(n*\[Pi]\)] \((Sin[
\*FractionBox[\(\[Pi]*n\), \(2\)]]*Cos[n*t])\)\)\);

 Plot[Evaluate[Table[f, {m, b}]], {t, -2 \[Pi], 2 \[Pi]}, 
  PlotStyle -> {Thickness[.002]}], {b, 1, 10}]

It seems to work okay for small upper bounds of b ($ \leq 8$), but beyond that I have problems.

Also, the first block of code that defines the function is input text for Mathematica. It says this: $$f=\sum _{n=1}^m \frac{4 \left(\sin \left(\frac{\pi n}{2}\right) \cos (n t)\right)}{\pi n}$$ Many thanks for your help!


2 Answers 2


You can store the plots as they are generated and reuse them.

mem : f[m_] := mem = Plot[
    Evaluate@Sum[(4/(n*Pi))*(Sin[(Pi*n)/2]*Cos[n*t]), {n, 1, m}],
    {t, -2*Pi, 2*Pi},
    PlotStyle -> {Thickness[.002], ColorData[97][m]}];

 Show[Table[f[m], {m, b}]],
 {{b, 1, "b"}, 1, 10, 1, Appearance -> "Labeled"},
 TrackedSymbols :> {b}]

try to see if this is faster

f[m_, t_] := Sum[(4/(n*Pi))*(Sin[(Pi*n)/2]*Cos[n*t]), {n, 1, m}]; 

Manipulate[Plot[Evaluate[Table[f[m, t], {m, b}], {t, -2*Pi, 2*Pi}], 
  PlotStyle -> {Thickness[0.002]}], 
   {{b, 1, "b"}, 1, 10, 1, Appearance -> "Labeled"}, 
 ContinuousAction -> False,
 TrackedSymbols :> {b}

Mathematica graphics


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.