Fourier Series: Complex Form

Global Assumptions

$Assumptions={Element[{m,n},Integers]}

Fourier Series for f(x)=1-(x^3), -p≤x≤p, p=1

(Subscript[c, n_]):=Simplify[(1/(2p))(∫_(-p)^p)f[t]Exp[-I((n π)/p)t]\[DifferentialD]t]
fourier[x_,k_]:=(∑_(n=-k)^k)((Subscript[c, n])Exp[I((n π)/p)x])

p=1
g[x_]=1-(x^2)
f[x_]:=g[x-2p*Floor[((x+p)/(2p))]]
(Subscript[c, n])
approx[x_]=fourier[x,5]

Do[

approx[x_]=fourier[x,k];
plot[k]=Plot[{f[x],approx[x]},{x,-5,5},PlotStyle->{Thick},PlotRange->{All,{-0.2,1.2}}]

,{k,1,5,1}]

Manipulate[Show[plot[k]],{k,1,5,1}]

Mathematica Built-in Commands Uses Complex Form

(Subscript[c, n_]):=Simplify[(1/(2p))(∫_(-p)^p)f[t]Exp[-I((n π)/p)t]\[DifferentialD]t]
fourier[x_,k_]:=(∑_(n=-k)^k)((Subscript[c, n])Exp[I((n π)/p)x])

p=1;
g[x_]=x;
f[x_]:=g[x-2p*Floor[((x+p)/(2p))]]

approx[x_]=fourier[x,4]

Plot[{f[x],approx[x]},{x,-5,5},PlotStyle->{Thick},PlotRange->{All,{-1.2,1.2}}]

The built-in command does not automatically use the same parameters as our definition, so we can set them using the FourierParameters option. And it is still fairly slow like our approximation since there are interesting integrals to perform.

fseries[x_]=FourierSeries[f[x],x,4,FourierParameters->{1,(π/p)}]