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

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

Not sure how to phrase this in a concise way, anyway it seems like the function FourierSeries assumes the interval for which to compute the Fourier coefficients of a given function is $[-\pi,\pi]$, which is all well and good. But if the function $f$ is defined as $g$ on, say, $[-\pi,a)$ and as $h$ on $[a,\pi]$, is there a way to define $f$ this way so that one can apply FourierSeries immediately on $f$?

Also, is there a way to make Mathematica understand that $\cos{n\pi}=(-1)^n$ and $\sin{n\pi}=0$?

share|improve this question
Yes, there is a way. Do you have an example of a function to use to show you? For your second question: Assuming[Element[n, Integers], Simplify[Cos[n Pi]]] gives (-1)^n and Assuming[Element[n, Integers], Simplify[Sin[n Pi]]] gives 0 – Nasser Jan 10 '14 at 4:43
Sure. $f=1-x^2$ on $[-\pi,0)$ and $f=1+x^2$ on $[0,\pi)$. Thanks! – Erik Vesterlund Jan 10 '14 at 4:50
up vote 3 down vote accepted

You examples are easy, I was hoping for harder ones ;) This is from the definition.

f3[x_] := Piecewise[{{1 - x^2 , x < 0}, {1 + x^2, x > 0}}];
FourierSeries[f3[x], x, 3]

Mathematica graphics

A quick Manipulate:

enter image description here

 r = FourierSeries[f[x], x, n];
 Show[Plot[r, {x, -2 Pi, 2 Pi}, Frame -> True], Plot[f[x], {x, -2 Pi, 2 Pi}, 
      PlotStyle -> {Thick, Red}]],
   {Control[{{n, 3, "how many terms?"}, 1, 20, 1}], Dynamic[n]}
 ContinuousAction -> False,
 SynchronousUpdating -> True,
 Initialization :>
   f[x_] := Piecewise[{{1 - x^2 , x < 0}, {1 + x^2, x > 0}}]

Mathematica graphics

And if you meant them to be different functions:

f1[x_] := Piecewise[{{1 - x^2 , x < 0}, {0, True}}];
f2[x_] := Piecewise[{{1 + x^2 , x > 0}, {0, True}}];
FourierSeries[f1[x], x, 3]

Mathematica graphics

FourierSeries[f2[x], x, 3]

Mathematica graphics

You can use the definition of the $c_k$ also by using FourierParameters to make it match the textbook you are using. So make sure to look at FourierParameters and adjust it as needed else you'll get different looking result from the textbook if the textbook does not use the default setting used by Mathematica.

share|improve this answer
Thank you! I looked up FourierParameters, but I don't understand what it's about? Really the only thing I'd want to change is for Mathematica to calculate real Fourier series. – Erik Vesterlund Jan 10 '14 at 5:45
@ErikVesterlund there are different definitions for the integral used to obtain the Fourier coefficients. In signal processing vs. say control vs. pure math. Different books use different definitions. So, if you are trying to compare results with some book, you need to make sure the same definitions are used in your code, else you'd think M is making a mistake. The help explains all the main 4 definitions and how to change them using FourierParameters-> – Nasser Jan 10 '14 at 5:52
I'm not sure what you by the Fourier series is always real; my book mentions both real and complex Fourier series, and the former can be derived from the latter, but that's just the process I don't want to work with, I just want to be able to, in one step, check my answers. For example, check this out to see what series I want when using FourierSeries:… – Erik Vesterlund Jan 10 '14 at 6:26
Well sure. But I mean that if I write FourierSeries[x,x,2] I want $2 \sin{x}-\sin{2x}$, the real Fourier series, as output, not $i e^{-ix}-ie^{ix}-(1/2)ie^{-2ix}+(1/2)ie^{2ix}$, the complex Fourier series. – Erik Vesterlund Jan 10 '14 at 6:47
@ErikVesterlund For that, you can simply convert it to real. Like this: FourierSeries[x, x, 2]; ExpToTrig[%] which gives 2 Sin[x] - Sin[2 x] – Nasser Jan 10 '14 at 6:57

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.