Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

marked as duplicate by whuber, Artes, Verbeia Jan 8 '13 at 0:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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: – R. M. 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

Browse other questions tagged or ask your own question.