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 :) !

• 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}]. Nov 21 '15 at 18:05