[CHWisGevordTech] Voorbeeld Fourier-reeks

1456 days ago by Carmen.Streat

Voorbeeld:   Vierkante golf in [-2,2] 

$\begin{equation}f(x) =  \begin{cases} 0 & \text{als  }& -2<x<-1 \\ 1 & \text{als  }&-1<x<1 \\0 & \text{als     } &1<x<2 \end{cases} \end{equation}$

 http://doc.sagemath.org/html/en/constructions/calculus.html#fourier-series-of-periodic-functions

f=piecewise([((-2,-1),0),((-1,1),1),((1,2),0)]) plot(f,-2,2) 
       

Bekijk de volgende commando's en gebruik ze om de Fourier-coëfficiënten  van $f$ te bepalen :

f.fourier_series_cosine_coefficient? 
       

File: /opt/sage/sage-7.4/local/lib/python2.7/site-packages/sage/symbolic/function_factory.py

Type: <type ‘instancemethod’>

Definition: f.fourier_series_cosine_coefficient(cls, self, parameters, variable, n, L)

Docstring:

Returns the n-th Fourier series coefficient of \cos(n\pi x/L), a_n.

INPUT:

  • self - the function f(x), defined over -L x L
  • n - an integer n=0
  • L - (the period)/2

OUTPUT: a_n = \frac{1}{L}\int_{-L}^L f(x)\cos(n\pi x/L)dx

EXAMPLES:

sage: f(x) = x^2
sage: f = piecewise([[(-1,1),f]])
sage: f.fourier_series_cosine_coefficient(2,1)
pi^(-2)
sage: f(x) = x^2
sage: f = piecewise([[(-pi,pi),f]])
sage: f.fourier_series_cosine_coefficient(2,pi)
1
sage: f1(x) = -1
sage: f2(x) = 2
sage: f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]])
sage: f.fourier_series_cosine_coefficient(5,pi)
-3/5/pi

File: /opt/sage/sage-7.4/local/lib/python2.7/site-packages/sage/symbolic/function_factory.py

Type: <type ‘instancemethod’>

Definition: f.fourier_series_cosine_coefficient(cls, self, parameters, variable, n, L)

Docstring:

Returns the n-th Fourier series coefficient of \cos(n\pi x/L), a_n.

INPUT:

  • self - the function f(x), defined over -L x L
  • n - an integer n=0
  • L - (the period)/2

OUTPUT: a_n = \frac{1}{L}\int_{-L}^L f(x)\cos(n\pi x/L)dx

EXAMPLES:

sage: f(x) = x^2
sage: f = piecewise([[(-1,1),f]])
sage: f.fourier_series_cosine_coefficient(2,1)
pi^(-2)
sage: f(x) = x^2
sage: f = piecewise([[(-pi,pi),f]])
sage: f.fourier_series_cosine_coefficient(2,pi)
1
sage: f1(x) = -1
sage: f2(x) = 2
sage: f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]])
sage: f.fourier_series_cosine_coefficient(5,pi)
-3/5/pi
f.fourier_series_sine_coefficient? 
       

File: /opt/sage/sage-7.4/local/lib/python2.7/site-packages/sage/symbolic/function_factory.py

Type: <type ‘instancemethod’>

Definition: f.fourier_series_sine_coefficient(cls, self, parameters, variable, n, L)

Docstring:

Returns the n-th Fourier series coefficient of \sin(n\pi x/L), b_n.

INPUT:

  • self - the function f(x), defined over -L x L
  • n - an integer n0
  • L - (the period)/2

OUTPUT: b_n = \frac{1}{L}\int_{-L}^L f(x)\sin(n\pi x/L)dx

EXAMPLES:

sage: f(x) = x^2
sage: f = piecewise([[(-1,1),f]])
sage: f.fourier_series_sine_coefficient(2,1)  # L=1, n=2
0

File: /opt/sage/sage-7.4/local/lib/python2.7/site-packages/sage/symbolic/function_factory.py

Type: <type ‘instancemethod’>

Definition: f.fourier_series_sine_coefficient(cls, self, parameters, variable, n, L)

Docstring:

Returns the n-th Fourier series coefficient of \sin(n\pi x/L), b_n.

INPUT:

  • self - the function f(x), defined over -L x L
  • n - an integer n0
  • L - (the period)/2

OUTPUT: b_n = \frac{1}{L}\int_{-L}^L f(x)\sin(n\pi x/L)dx

EXAMPLES:

sage: f(x) = x^2
sage: f = piecewise([[(-1,1),f]])
sage: f.fourier_series_sine_coefficient(2,1)  # L=1, n=2
0
L=2 for n in [1..5]: an=f.fourier_series_cosine_coefficient(n,L) an 
       

                                
                            

                                

Merk op dat   a0= (1/2)*f.fourier_series_cosine_coefficient(0,L)

a0=(1/2)*f.fourier_series_cosine_coefficient(0,L) a0 
       

                                
                            

                                
var('n') assume(n,'integer') L=2 an=f.fourier_series_cosine_coefficient(n,L) an 
       

                                
                            

                                
assume(n,'even') an=f.fourier_series_cosine_coefficient(n,L) an 
       

                                
                            

                                
forget() assume(n,'odd') an=f.fourier_series_cosine_coefficient(n,L) an 
       

                                
                            

                                

Besluit:

$a_n = \begin{cases} 0 & \text{ als   } n  \text{  even is }\\ \frac{2}{\pi n} & \text{ als   } n =1,5,9,... \\  \frac{-2}{\pi n} &\text{  als  } n=3,7,11,... \end{cases}$

#eerst de vorige assume voor n ongedaan maken: forget() 
       
bn=f.fourier_series_sine_coefficient(n,L) bn 
       

                                
                            

                                

De partiële som $f_N$ van de Fourier-reeks van $f$  bepalen: 

f.fourier_series_partial_sum? 
       

File: /opt/sage/sage-7.4/local/lib/python2.7/site-packages/sage/symbolic/function_factory.py

Type: <type ‘instancemethod’>

Definition: f.fourier_series_partial_sum(cls, self, parameters, variable, N, L)

Docstring:

Returns the partial sum

f(x) \sim \frac{a_0}{2} + \sum_{n=1}^N [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],

as a string.

EXAMPLE:

sage: f(x) = x^2
sage: f = piecewise([[(-1,1),f]])
sage: f.fourier_series_partial_sum(3,1)
cos(2*pi*x)/pi^2 - 4*cos(pi*x)/pi^2 + 1/3
sage: f1(x) = -1
sage: f2(x) = 2
sage: f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]])
sage: f.fourier_series_partial_sum(3,pi)
-3*cos(x)/pi - 3*sin(2*x)/pi + 3*sin(x)/pi - 1/4

File: /opt/sage/sage-7.4/local/lib/python2.7/site-packages/sage/symbolic/function_factory.py

Type: <type ‘instancemethod’>

Definition: f.fourier_series_partial_sum(cls, self, parameters, variable, N, L)

Docstring:

Returns the partial sum

f(x) \sim \frac{a_0}{2} + \sum_{n=1}^N [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],

as a string.

EXAMPLE:

sage: f(x) = x^2
sage: f = piecewise([[(-1,1),f]])
sage: f.fourier_series_partial_sum(3,1)
cos(2*pi*x)/pi^2 - 4*cos(pi*x)/pi^2 + 1/3
sage: f1(x) = -1
sage: f2(x) = 2
sage: f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]])
sage: f.fourier_series_partial_sum(3,pi)
-3*cos(x)/pi - 3*sin(2*x)/pi + 3*sin(x)/pi - 1/4
fN=f.fourier_series_partial_sum(10,2) fN 
       

                                
                            

                                
plot(f,-2,2)+plot(fN,(x,-2,2),color='red') 
       

De totale kwadratische fout E bepalen:

plot((f-fN)^2,x,-2,2) 
       

$\displaystyle E=\int_{-2}^2 (f(x)-f_N(x))^2 dx = 4\int_{0}^1 (f(x)-f_N(x))^2 dx=4\int_{0}^1 (1-f_N(x))^2 dx$

var('x,n') L=2 for n in [1..14]: fN=f.fourier_series_partial_sum(n+1,L) E=4*N(integral((1-fN)^2,x,0,1)) E 
       

                                
                            

                                
var('x,n') L=2 lst=[] for n in [1..14]: fN=f.fourier_series_partial_sum(n+1,L) E=4*N(integral((1-fN)^2,x,0,1)) lst.append((n,E)) line(lst)