Fourier Series of a Triangular Wave Function
Author's Data: Housam Binous
Department of Chemical Engineering
National Institute of Applied Sciences and Technology
Tunis, TUNISIA
Email: binoushousam@yahoo.com

g[t_] := 
      If[t < 0, 0, If[t < Pi/2, t, If[Pi > t > Pi/2, Pi - t, 0]]]

f[t_] := 
      If[t < Pi   ||  t > Pi, g[t - Quotient[t, Pi] Pi], g[t]]

plt1 = 
      Plot[f[x], {x, -3 Pi, 3 Pi}, 
        PlotStyle -> RGBColor[1, 0, 0]]

a = 
      Table[NIntegrate[f[t] Cos[2 i Pi t/Pi], {t, 0, Pi}, 
            AccuracyGoal -> 4]/Pi, {i, 1, 100}]

b = 
      Table[NIntegrate[f[t] Sin[2 i Pi t/Pi], {t, 0, Pi}, 
            AccuracyGoal -> 4]/Pi, {i, 1, 100}]

a0 = 2/Pi NIntegrate[f[t], {t, 0, Pi}, AccuracyGoal -> 4]

plt2 = 
      Plot[0.5 a0 + (∑_(i = 1)^100) 
              2 (a[[i]] Cos[2 i Pi x/Pi] + 
                  b[[i]] Sin[2 i Pi x/Pi]), {x, -2 Pi, 2  Pi}, 
        PlotRange -> All, 
        PlotStyle -> RGBColor[0, 0, 1]]

Plot of the function f(t) and its Fourier series approximation

Show[plt2, plt1, 
      PlotRange ->  {{-3 Pi, 3 Pi}, {0, 2}}]