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$?

  • 1
    $\begingroup$ 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 $\endgroup$
    – Nasser
    Jan 10, 2014 at 4:43
  • $\begingroup$ Sure. $f=1-x^2$ on $[-\pi,0)$ and $f=1+x^2$ on $[0,\pi)$. Thanks! $\endgroup$ Jan 10, 2014 at 4:50

1 Answer 1


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.

  • $\begingroup$ 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. $\endgroup$ Jan 10, 2014 at 5:45
  • $\begingroup$ @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-> $\endgroup$
    – Nasser
    Jan 10, 2014 at 5:52
  • $\begingroup$ 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: math.stackexchange.com/questions/633395/… $\endgroup$ Jan 10, 2014 at 6:26
  • $\begingroup$ 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. $\endgroup$ Jan 10, 2014 at 6:47
  • 2
    $\begingroup$ @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] $\endgroup$
    – Nasser
    Jan 10, 2014 at 6:57

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.