# How can I plot a function and its partial sums? [duplicate]

Possible Duplicate:
How to Plot an Infinite Series
Coding mistake?

I have Mathematica v9.0 and I am a newbie to Mathematica. I am trying to plot a function $f(x)=x$ over the interval $(-\pi,\pi)$. Together on the same plot, I want two partial sums, $S_{4}$ and $S_{8}$.

I have tried to plot $S_{4}$ and $f(x)$, but can't seem to get it right. Can anyone show me how to do it? Here is my code:

f[x_] = x
s[n_, x_] := Sum[((-1)^(n + 1)) (2/n) (Sin[nx]), {n, 1, Infinity}]
partialsums = Table[s[n, x], {n, 1, 4}];
Plot[Evaluate[f[x], partialsums], {x, -3, 3}]


Is there anything wrong with my code? Hope someone can shed light on this.

• Try f[x_] := x s[k_, x_] := Sum[((-1)^(n + 1)) (2/n) (Sin[n x]), {n, 1, k}] partialsums = Table[s[n, x], {n, {4, 8, 12}}]; Plot[Evaluate[{f[x], partialsums}], {x, -3, 3}]! You are making some syntax errors. By the way think if you sum up to infinity can it be called a partial sum? – PlatoManiac Jan 7 '13 at 17:45
• Thanks a lot @PlatoManiac! Currently we have like {n,1,8} if I change this to {n,4,4}, the graph will just have $f(x)=x$ and $S_{4}$ partial sum. How can I adjust the code so that only the function and $S_{4}$ and $S_{8}$ appear on the graph only without going from 1 to 8? And how can I change the lines of $S_{4}$ to dotted line while $S_{8}$ to dot type because when I print it in black and white, it will be easier to differentiate. – Sandra Jan 7 '13 at 17:49
• Possible duplicate: mathematica.stackexchange.com/q/14083/5 – rm -rf Jan 7 '13 at 17:51
• @Sandra you are welcome. In my comment I plot $\{f,S_4,S_8,S_{12}\}$! – PlatoManiac Jan 7 '13 at 17:52
• @Hypnotoad - Thanks for giving me the link and sorry for the duplicate. I am very new to this forum. Let assured that duplicate will not occur again. Thanks again for pointing this out. – Sandra Jan 7 '13 at 17:58