# Fourier Series plot

I'm trying to plot partial sum of Fourier series. I've finally managed to make it work but there are several things I don't understand

f = Piecewise[{{Mod[x, Pi], Mod[x, 2 Pi] < Pi}, {Mod[-x, Pi], Mod[x, 2 Pi] >= Pi}}]
g = FourierSeries[f, x, 2]
Plot[{g, f}, {x, -10, 10}]


My questions are :

1. Why does it stop working when I directly put the value of g in Plot (only f is plotted) ?

f = Piecewise[{{Mod[x, Pi], Mod[x, 2 Pi] < Pi}, {Mod[-x, Pi],Mod[x, 2 Pi]>= Pi}}]
Plot[{FourierSeries[f, x, 2], f}, {x, -10, 10}]

2. Why does it stop working when I define g like this (only f is plotted)?

g[x_] := FourierSeries[f, x, 2]
Plot[{g[x], f}, {x, -10, 10}]

3. I also tried to add a slider to set the number terms to add. It seems to work but it makes Mathematica freeze...

f = Piecewise[{{Mod[x, Pi], Mod[x, 2 Pi] < Pi}, {Mod[-x, Pi],Mod[x, 2 Pi] >= Pi}}]
Slider[Dynamic[n], {1, 10, 1}]
Dynamic[g = FourierSeries[f, x, n]]
Dynamic[Plot[{g, f}, {x, -10, 10}]]


Thanks :) !

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Nov 21 '15 at 17:59
• Re 1: Because x is set to a numeric value before the series is computed. Re 2: Ditto; use g[x_?NumericQ] := FourierSeries[f, x, 2]. Re 3: Totally different. You get into a Dynamic infinite loop because setting g causes another update. Try Dynamic[g = FourierSeries[f, x, n], TrackedSymbols :> {n}]. – Michael E2 Nov 21 '15 at 18:05

## 1 Answer

I guess I can put my comment in an answer:

• Re 1: Because x is set to a numeric value before the series is computed.
• Re 2: Ditto; use g[x_?NumericQ] := FourierSeries[f, x, 2].
• Re 3: Totally different. You get into a Dynamic infinite loop because setting g causes another update. Try Dynamic[g = FourierSeries[f, x, n], TrackedSymbols :> {n}].

The first two questions are more or less a duplicate of What are the most common pitfalls awaiting new users?

The third question comes up in so many forms, it's hard to find an exact duplicate. The underlying issue in Can Table be used inside Manipulate? is the same, although it's a rather complicated example compared to this one.

• Thanks ! I learned a lot from those two links ! – user2737248 Nov 22 '15 at 10:39