Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted

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] *)
share|improve this answer
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! – Sandra Jan 8 '13 at 23:32
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. – einbandi Jan 8 '13 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}]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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