3
$\begingroup$

How do I plot this on Mathematica version 5.2?

$\frac{4}{\pi} \sin{x} + \frac{4}{3 \pi} \sin{3 x} + \cdots + \frac{4}{(2 N+1) \pi} \sin{(2 N+1) x}$

over $x \in [-\pi,\pi]$ for $N= 3, 6, 12, 24$.

I tried and got this error:

enter image description here

$\endgroup$

2 Answers 2

1
$\begingroup$

This seems to work fine:

f[x_, n_] := (4/Pi) Sum[Sin[(2 k + 1) x]/(2 k + 1), {k, 0, n}]; 
Plot[Table[f[x, n], {n, 5}], {x, -Pi, Pi}]

enter image description here

$\endgroup$
4
  • $\begingroup$ May be it does not work in version 5.2 of mathematica $\endgroup$
    – Avinesh
    Oct 10, 2013 at 2:47
  • $\begingroup$ How to get get single curve for n=5 only? $\endgroup$
    – Avinesh
    Oct 10, 2013 at 3:08
  • $\begingroup$ Plot[f[x, 5], {x, -Pi, Pi}] gives just the n=5 curve. $\endgroup$
    – bill s
    Oct 10, 2013 at 3:34
  • 1
    $\begingroup$ This one works for version 5.2 s[n_, x_] := 4/( π) Sum[Sin[(2 k + 1) x]/(2 k + 1), {k, 0, n}] partialsums = Table[s[5, x], {n, 6}]; Plot[Evaluate[partialsums], {x, -π, π}] $\endgroup$
    – Avinesh
    Oct 10, 2013 at 3:59
1
$\begingroup$

This is a documented behavior of Plot in or before v5.2. Just click the More... button in the Plot::plnr warning, you'll see the following explanation:

If Plot is used to plot a list of functions, that list should appear explicitly as the first argument in Plot or should be introduced using Evaluate or other means.

together with a typical example:

enter image description here

So one solution is, as you've noticed, using Evaluate:

f[x_, n_] := (4/Pi) Sum[Sin[(2 k + 1) x]/(2 k + 1), {k, 0, n}];
Plot[Table[f[x, n], {n, 5}] // Evaluate, {x, -Pi, Pi}]

It's not the only solution, of course. A few other solutions:

With[{table = Table[f[x, n], {n, 5}]}, Plot[table, {x, -Pi, Pi}]]
Plot[#, {x, -Pi, Pi}] &@Table[f[x, n], {n, 5}]
Plot @@ {Table[f[x, n], {n, 5}], {x, -Pi, Pi}}
$\endgroup$

Your Answer

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

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