Phys 238
Fourier Series Representation of a Triangle Wave

f[t_] := Piecewise[{
{4 * A * (t + T/4)/T,-T/2 ≤ t ≤ 0},
{-4 * A * (t - T/4)/T, 0 < t ≤ T/2}
}]

A = 5.0;
T = 10.0;

Plot[f[t], {t, -T/2, T/2}]

Fourier Transform

Nmax = 10;

ω[n_] := n * (2 π/T)

b[n_] := (2/T) Integrate[f[t] Cos[ω[n] t], {t, -T/2, T/2}]

b[1]

b[1] / (8 A/(π^2))

bs = Table[b[n], {n, 1, Nmax}]

bs = Chop[bs]

bs / (8 A / (π^2))

ListPlot[bs, Joined->True, Mesh->All, PlotRange->All]

ListPlot[bs, Filling -> Axis, FillingStyle->Black, PlotRange->All]

Reconstructing the original waveform

fs[nmax_, t_] :=  Sum[bs[[n]] Cos[ω[n] t], {n, 1, nmax}]

fs[3, t]

Manipulate[Plot[{f[t], fs[nmax, t], bs[[nmax]] Cos[ω[nmax] t]}, {t, -T/2, T/2}, LabelStyle->Larger, AxesLabel -> {"t", "f(t)"}, ImageSize -> Scaled[0.8]], {nmax, 1, Nmax, 2, Appearance->"Open"}]

Analyitic Version

an[n_] := (4 A / ((π^2)(n^2))) (1 - (((-1))^n))

fsa[nmax_, t_] := Sum[an[n] Cos[ω[n] t], {n, 1, nmax}]

fsa[5, t]

Plot[fsa[99, t], {t, -T/2, T/2}]