I am a newbie in Mathematica and is currently using Mathematica v9.0. I would like to consult if anyone knows how can I plot a given functions with conditions as shown below on how they look like.

$$ g(x)= \left\{\begin{split} \frac{(\pi-x)}{2} &\text{, } 0 \leq x \leq \pi \\ -\frac{(\pi+2)}{2} &\text{, } -\pi \leq x < 0 \end{split}\right. $$

Then together in the same graph, I would like to plot its partial sums $S_{4}$ and $S_{8}$ based on the fourier series of the function $\sum^{n}_{k=1} \frac{\sin kx}{k}$.

Note: If there is anyone who can edit the above latex to mathematics form, please do so as I don't know how to convert the above $\LaTeX$ code into mathematical wordings. Sorry about that.


2 Answers 2


Since there still seems to be no answer to the second part about adding the partial sums to the plot, here you go:

partialSum[n_, x_] := Sum[Sin[k x]/k, {k, n}]
  Piecewise[{{(Pi - x)/2, 
     0 <= x <= Pi}, {-((Pi + x)/2), -Pi <= x <= 0}}],
  partialSum[4, x], partialSum[8, x]},
 {x, -2, 2}]

enter image description here

As you see, you can show several functions in the same plot just by passing a list of functions to Plot. The definition of partialSum is pretty straight forward, too.

Btw, if you don't know the Fourier series already, you can calculate it easily (using FourierSinSeries in this case to immediately get the desired form):

g[x_] := 
 Piecewise[{{(Pi - x)/2, 
    0 <= x <= Pi}, {-((Pi + x)/2), -Pi <= x <= 0}}]

FourierSinSeries[g[x], x, 4]

(* -----> Sin[x] + 1/2 Sin[2 x] + 1/3 Sin[3 x] + 1/4 Sin[4 x] *)
  • $\begingroup$ Thank you so much. I failed myself in the second part and thank you for solving the issue for me and thanks also your kindness in introducing FourierSinSeries to me. I didn't know this. I thought there is one for "FourierSeries" by my friends told me that there is no need for this in Mathematics v9.0 since it is embedded. Guess they are referring to different things. Thank you again! $\endgroup$
    – Sandra
    Jan 8, 2013 at 23:32
  • $\begingroup$ You're welcome. It is custom on this site to mark an answer that fits your needs as "accepted" by clicking the tick mark on the left. That way, people will no if you need more help or if you consider the topic to be settled. $\endgroup$
    – einbandi
    Jan 8, 2013 at 23:39

I had solved the first part to my question. Thanks to @b.gatessucks

Plot[Piecewise[{{(Pi - x)/2, 0 <= x <= Pi}, {-((Pi + x)/2), -Pi <= x <= 0}}], {x, -2, 2}]

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.