2
$\begingroup$

I would like to plot

f1[x_, n_] := Sum[Sin[(2 j - 1) x]/(2 j - 1), {j, 1, n}]

on a single plot containing f1(x,50), f1(x,100), f1(x,1000), with three different colored lines.

Please help

$\endgroup$
2
  • $\begingroup$ Are j and k equal?! What x should be? $\endgroup$
    – Mahdi
    Oct 1, 2014 at 22:41
  • $\begingroup$ Yes, j and k and equal. I must've input k instead of j at the end $\endgroup$
    – Curious14
    Oct 1, 2014 at 22:42

2 Answers 2

3
$\begingroup$
f1[x_, n_] := Sum[Sin[(2 j - 1) x]/(2 j - 1), {j, 1, n}]; 
Plot[ f1[x, #] & /@ {50, 100, 1000}, {x, -Pi, Pi}, Evaluated -> True]

enter image description here

$\endgroup$
4
  • $\begingroup$ Can you help me to understand what the input "f1[x, #] & /@ {50, 100, 1000}" means? Specifically the "#" and "& /@" $\endgroup$
    – Curious14
    Oct 1, 2014 at 22:47
  • 1
    $\begingroup$ @MathStudent, f1[x, #] & /@ {50, 100, 1000} is shorter way of writing {f1[x, 50],f1[x,100],f1[x,1000]}. See docs on Slot (#) and Map (/@) $\endgroup$
    – kglr
    Oct 1, 2014 at 22:49
  • $\begingroup$ Nice plot, it shows the Gibbs phenomenon clearly. $\endgroup$ Oct 1, 2014 at 22:52
  • $\begingroup$ Thank you for the clarification! $\endgroup$
    – Curious14
    Oct 1, 2014 at 22:56
2
$\begingroup$

Assuming that this the correct function:

f1[x_, n_] := Sum[Sin[(2 k - 1) x]/(2 k - 1), {k, 1, n}];

We can plot them by the following command:

Plot[f1[x, #] & /@ {50, 100, 1000}, {x, 0, Pi}, PlotLegends -> Placed[{"50", "100", "1000"}, {1.01, 0.5}]]

or equivalently:

Plot[{f1[x,50],f1[x,100],f1[x,1000]}, {x, 0, Pi}, PlotLegends -> Placed[{"50", "100", "1000"}, {1.01, 0.5}]]

for 0 to $\pi$.

enter image description here

$\endgroup$
2
  • $\begingroup$ Your second plotting input is much easier to understand, where you commanded each of the plots. May you help me to understand what "{1.01, 0.5}" means? $\endgroup$
    – Curious14
    Oct 1, 2014 at 22:51
  • $\begingroup$ Yes! I use Placed command to put the legend at {1.01,0.5}. Mathematica assumes that the figure coordinates is {0,1} horizontally and vertically. So for example if write {0.5,0.5} legend will be right at the middle of figure. $\endgroup$
    – Mahdi
    Oct 1, 2014 at 23:09

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.