1

It is given the following periodic function $$f(x)=x-x^3, \quad -1<x<1$$ This is an odd function, so the coefficients $a_n,a_0$ of the Fourier Series are zero.

Usually, I face function with two branches so I tried to code it on Mathematica as follows:

f[x_] := Which[-1 < x <= 0, x - x^3, 0 < x < 1, x - x^3]
Plot[f[x], {x, -1, 1}]

enter image description here

Edited

a[n_] := (2/L)*Integrate[f[x]*Cos[2 n*Pi*x/L], {x, -L/2, L/2}]
a[0] := (1/L)*Integrate[f[x], {x, -L/2, L/2}]
b[n_] := (2/L)*Integrate[f[x]*Sin[2 n*Pi*x/L], {x, -L/2, L/2}]
F[x_, Nmax_] := 
 a[0] + Sum[a[n]*Cos[2 n*Pi*x/L] + b[n]*Sin[2 n*Pi*x/L], {n, 1, N}]
p[Nmax_, a_] := 
 Plot[Evaluate[F[x, Nmax]], {x, -a, a}, PlotRange -> All, 
  PlotPoints -> 200]
L = 2;
f[x_] := If[x > 0, x - x^3, x - x^3];
a[n]
a[0]
b[n]
Simplify[b[n], n \[Element] Integers]

Integrate[f[x]^2, {x, -L, L}] (a[0]^2)/2 + Sum[(a[n]^2 + b[n]^2), {n, 1, Infinity}] <= Integrate[f[x]^2, {x, -L, L}]

Is my consideration correct? I can realize that the two plots are the same and for this reason seems weird. enter image description here

1 Answers1

2

is this the reason that the plots are identical?

They are not identical. But the approximation is very good. Even for 2 terms, you get very good approximation. You can see this as below

L = 2; (*period*)
f[x_] := x - x^3;
a[n_, x_] = (2/L)*Integrate[f[x]*Cos[2 n*Pi*x/L], {x, -L/2, L/2}]
a[n_ /; n == 0, x_] = (1/L)*Integrate[f[x], {x, -L/2, L/2}]
b[n_, x_] = (2/L)*Integrate[f[x]*Sin[2 n*Pi*x/L], {x, -L/2, L/2}]

Mathematica graphics

Notice also since odd function, only $b_n$ survives.

fFourierApprox[x_, nTerms_] := 
  a[0, x] + Sum[a[n, x]*Cos[2 n*Pi*x/L] + b[n, x]*Sin[2 n*Pi*x/L], {n, 1,  nTerms}];

Let compare $f(x)$ with its Fourier series, using one term only

Plot[{f[x], fFourierApprox[x, 1]}, {x, -L/2, L/2}, 
 PlotLegends -> {"f(x)", "approx"}, PlotRange -> All]

Mathematica graphics

Using 2 terms now

Plot[{f[x], fFourierApprox[x, 2]}, {x, -L/2, L/2}, 
 PlotLegends -> {"f(x)", "approx"}, PlotRange -> All]

Mathematica graphics

With 3 terms you can hardly see the difference

Plot[{f[x],fFourierApprox[x,3]},{x,-L/2,L/2},PlotLegends->{"f(x)","approx"},PlotRange->All]

Mathematica graphics

You original function is continuous, so as typical in Fourier series, few terms are needed to give very accurate approximation. (There are more Fourier series animations showing this point all done using Mathematica I saw at this page)

ps. If you extend the range to the full -L..L, this is the result using only one term in F.S.

Plot[{f[x], fFourierApprox[x, 1]}, {x, -L, L}, 
 PlotLegends -> {"f(x)", "approx"}, PlotRange -> All]

Mathematica graphics

Nasser
  • 143,286
  • 11
  • 154
  • 359