Fourier Series with Arbitrary Period

Global Assumptions

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

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

(Subscript[a, 0]):=(1/(2p))(∫_(-p)^p)f[x]\[DifferentialD]x
(Subscript[a, n_]):=Simplify[(1/p)(∫_(-p)^p)f[x]Cos[((n π)/p)x]\[DifferentialD]x]
(Subscript[b, n_]):=Simplify[(1/p)(∫_(-p)^p)f[x]Sin[((n π)/p)x]\[DifferentialD]x]
fourier[x_,k_]:=(Subscript[a, 0])+(∑_(n=1)^k)((Subscript[a, n])Cos[((n π)/p)x]+(Subscript[b, n])Sin[((n π)/p)x])

p=1
g[x_]=1-(x^2)
f[x_]:=g[x-2p*Floor[((x+p)/(2p))]]
(Subscript[a, 0])
(Subscript[a, n])
(Subscript[b, 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,10,1}]

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

Fourier Series for Even Function f(x)=1-(x^2), -p≤x≤p, p=1/2

For an even function, the (Subscript[b, n])=0 for all n. Also, the integral limits can go from 0 to p with a multiplicative factor of 2. You should exploit this when you can since it will speed up computation.

(Subscript[a, 0]):=(1/p)(∫_0^p)f[x]\[DifferentialD]x
(Subscript[a, n_]):=Simplify[(2/p)(∫_0^p)f[x]Cos[((n π)/p)x]\[DifferentialD]x]
fourier[x_,k_]:=(Subscript[a, 0])+(∑_(n=1)^k)((Subscript[a, n])Cos[((n π)/p)x])

p=1/2
g[x_]=1-(x^2)
f[x_]:=g[x-2p*Floor[((x+p)/(2p))]]
(Subscript[a, 0])
(Subscript[a, 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,10,1}]

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

Fourier Series for Odd Function f(x)=x cos x, -p≤x≤p, p=π/4

For an even function, the (Subscript[a, n])=0 for all n. Also, the integral limits can go from 0 to p with a multiplicative factor of 2. You should exploit this when you can since it will speed up computation.

(Subscript[b, n_]):=Simplify[(2/p)(∫_0^p)f[x]Sin[((n π)/p)x]\[DifferentialD]x]
fourier[x_,k_]:=(∑_(n=1)^k)((Subscript[b, n])Sin[((n π)/p)x])

p=π/4
g[x_]=x Cos[x]
f[x_]:=g[x-2p*Floor[((x+p)/(2p))]]
(Subscript[b, 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.8,0.8}}]

,{k,1,10,1}]

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