Fourierov red

Teorem o konvergenciji

Neka je $f$ po djelovima glatka periodična funkcija s periodom $T=x_1-x_0$ koja na intervalu $[x_0,x_1]$ zadovoljava Dirichletove uvjete:

  • $f$ je po djelovima neprekidna i njezini prekidi su prve vrste
  • $f$ je ili monotona ili ima konačno strogih ekstrema

Tada Fourierov red $$ S(x)=\frac{a_0}{2}+\sum_{n=1}^\infty a_n \cos \frac{2 n \pi}{T}x + b_n \sin \frac{2 n \pi}{T}x, $$ gdje je $$ a_0=\frac{2}{T} \int_{x_0}^{x_1} f(x) \, dx, \\ a_n=\frac{2}{T} \int_{x_0}^{x_1} f(x) \cos \frac{2 n \pi}{T}x \,dx,\\ b_n=\frac{2}{T} \int_{x_0}^{x_1} f(x) \sin \frac{2 n \pi}{T}x \,dx, $$ konvergira u svakoj točki $x\in [x_0,x_1]$ i vrijedi $$ S(x)=\begin{cases} f(x), \textrm{ ako je } f \textrm{ neprekidna u točki } x,\\ \frac{1}{2} [f(x-0)+f(x+0)], \textrm{ inače.} \end{cases} $$

Simboličko računanje

SymPy je paket za simboličko računanje preuzet iz Python-a, a Plots je jedan od paketa za crtanje.


In [1]:
using SymPy
using Plots


┌ Info: Precompiling SymPy [24249f21-da20-56a4-8eb1-6a02cf4ae2e6]
└ @ Base loading.jl:1260

Definirajmo simboličku varijablu $x$, simboličku cjelobrojnu (integer) varijablu $n$, i granice intervala $x_0$ i $x_1$.


In [2]:
x=symbols("x",real=true)
n=symbols("n",integer=true)
x₀=symbols("x₀")
x₁=symbols("x₁")
T=symbols("T")


Out[2]:
\begin{equation*}T\end{equation*}

Definiranje koeficijenata


In [7]:
a(n)=2*integrate(f(x)*cos(2*PI*n*x/T),(x,x₀,x₁))/T
b(n)=2*integrate(f(x)*sin(2*PI*n*x/T),(x,x₀,x₁))/T


Out[7]:
b (generic function with 1 method)

Zadavanje funkcije $f(x)$ i granica intervala $[x_0,x_1]$

Potrebno je koristiti predefiniranu simboličku varijablu PI za razliku od varijabli pi ili $\pi$ kojima je definirana Float64 vrijednost.


In [5]:
PI


Out[5]:
\begin{equation*}\pi\end{equation*}

In [25]:
f(x)=x
x₀=-PI
x₁=PI
T=x₁-x₀


Out[25]:
\begin{equation*}2 \pi\end{equation*}

In [10]:
a(1)


Out[10]:
\begin{equation*}0\end{equation*}

In [11]:
[a(n) for n=0:5]


Out[11]:
\[ \left[ \begin{array}{r}0\\0\\0\\0\\0\\0\end{array} \right] \]

In [12]:
[b(n) for n=0:5]


Out[12]:
\[ \left[ \begin{array}{r}0\\2\\-1\\\frac{2}{3}\\- \frac{1}{2}\\\frac{2}{5}\end{array} \right] \]

Računanje i crtanje sume

Trebaju nam i numeričke granice.


In [15]:
x0=Float64(x₀)
x1=Float64(x₁)
K=20
S=a(0)/2+sum([a(n)*cos(2*PI*n*x/T)+b(n)*sin(2*PI*n*x/T) for n=1:K])
g(x)=S(x)
title="Funkcija i "*string(K)*" članova reda"
plot(f,x0,x1)
plot!(g,x0,x1,title=title)


Out[15]:

In [16]:
S


Out[16]:
\begin{equation*}2 \sin{\left(x \right)} - \sin{\left(2 x \right)} + \frac{2 \sin{\left(3 x \right)}}{3} - \frac{\sin{\left(4 x \right)}}{2} + \frac{2 \sin{\left(5 x \right)}}{5} - \frac{\sin{\left(6 x \right)}}{3} + \frac{2 \sin{\left(7 x \right)}}{7} - \frac{\sin{\left(8 x \right)}}{4} + \frac{2 \sin{\left(9 x \right)}}{9} - \frac{\sin{\left(10 x \right)}}{5} + \frac{2 \sin{\left(11 x \right)}}{11} - \frac{\sin{\left(12 x \right)}}{6} + \frac{2 \sin{\left(13 x \right)}}{13} - \frac{\sin{\left(14 x \right)}}{7} + \frac{2 \sin{\left(15 x \right)}}{15} - \frac{\sin{\left(16 x \right)}}{8} + \frac{2 \sin{\left(17 x \right)}}{17} - \frac{\sin{\left(18 x \right)}}{9} + \frac{2 \sin{\left(19 x \right)}}{19} - \frac{\sin{\left(20 x \right)}}{10}\end{equation*}

Korištenje funkcije fourier_series()

Prvo pogledajmo simboličko računanje razvoja u Taylor-ov red.


In [17]:
G₁=series(sin(x),x,0,7)


Out[17]:
\begin{equation*}x - \frac{x^{3}}{6} + \frac{x^{5}}{120} + O\left(x^{7}\right)\end{equation*}

In [18]:
G₂=series(cos(x),x,0,7)


Out[18]:
\begin{equation*}1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + O\left(x^{7}\right)\end{equation*}

In [19]:
G₃=simplify(G₁*G₂)


Out[19]:
\begin{equation*}x - \frac{2 x^{3}}{3} + \frac{2 x^{5}}{15} + O\left(x^{7}\right)\end{equation*}

In [20]:
G₃.removeO()


Out[20]:
\begin{equation*}\frac{2 x^{5}}{15} - \frac{2 x^{3}}{3} + x\end{equation*}

Sada razvoj u Fourier-ov red


In [21]:
# Ovo učitava funkcije za Fourierovu analizu i Laplaceovu transformaciju
import_from(sympy)

In [26]:
c=fourier_series(x^2,(x,x₀,x₁))


Out[26]:
\begin{equation*}- 4 \cos{\left(x \right)} + \cos{\left(2 x \right)} + \frac{\pi^{2}}{3} + \ldots\end{equation*}

In [27]:
# Prvih 10 harmonika
c₁=c.truncate(10)


Out[27]:
\begin{equation*}- 4 \cos{\left(x \right)} + \cos{\left(2 x \right)} - \frac{4 \cos{\left(3 x \right)}}{9} + \frac{\cos{\left(4 x \right)}}{4} - \frac{4 \cos{\left(5 x \right)}}{25} + \frac{\cos{\left(6 x \right)}}{9} - \frac{4 \cos{\left(7 x \right)}}{49} + \frac{\cos{\left(8 x \right)}}{16} - \frac{4 \cos{\left(9 x \right)}}{81} + \frac{\pi^{2}}{3}\end{equation*}

In [28]:
# Numerička vrijednost u točki x=1
Float64(c₁(1))


Out[28]:
1.0247547650708126

Računanje aproksimacije i crtanje


In [29]:
X = range(x0,x1,length=100)
css=[Float64(c₁(x)) for x in X]


Out[29]:
100-element Array{Float64,1}:
 9.448939058362615
 9.377200212742677
 9.170904135704877
 8.854932757403411
 8.464960518690962
 8.040276907029902
 7.616347739550003
 7.218862857043369
 6.860562867586582
 6.541363474264308
 6.251423983812782
 5.976078186725812
 5.701153263614476
 ⋮
 5.976078186725812
 6.251423983812782
 6.541363474264308
 6.860562867586582
 7.218862857043369
 7.616347739550003
 8.040276907029902
 8.464960518690962
 8.854932757403411
 9.170904135704877
 9.377200212742677
 9.448939058362615

In [30]:
plot(x->x^2,x0,x1)
plot!(X,css)


Out[30]:

Interaktivno računanje i crtanje


In [31]:
f₁=x
K=20
cs=fourier_series(f₁,(x,x₀,x₁)).truncate(K)
css=[Float64(cs(x)) for x in X]
title="Funkcija i "*string(K)*" članova reda"
plot(f,x0,x1)
plot!(X,css,title=title)


Out[31]:

Numerička integracija

Fourierove koeficijente čemo izračunati numeričkom integracijom (vidi Numeričko integriranje) koristeći Julia naredbu quadgk.

Ovaj dio je izrađen prema bilježnici lecture-2.ipynb Stevena Johnsona izrađenoj za predmet 18.303.

Definirajmo funkcije sinecoef i coscoef koje numerički računaju koeficijente. Parametar abstol je toleranca numeričke integracije: želimo da je greška mala u odnosu na $\sqrt{\int_{x_0}^{x_1} |f(x)|^2 dx}$.


In [32]:
using QuadGK


┌ Info: Precompiling QuadGK [1fd47b50-473d-5c70-9696-f719f8f3bcdc]
└ @ Base loading.jl:1260

In [34]:
?quadgk


search: quadgk QuadGK

Out[34]:
quadgk(f, a,b,c...; rtol=sqrt(eps), atol=0, maxevals=10^7, order=7, norm=norm)

Numerically integrate the function f(x) from a to b, and optionally over additional intervals b to c and so on. Keyword options include a relative error tolerance rtol (if atol==0, defaults to sqrt(eps) in the precision of the endpoints), an absolute error tolerance atol (defaults to 0), a maximum number of function evaluations maxevals (defaults to 10^7), and the order of the integration rule (defaults to 7).

Returns a pair (I,E) of the estimated integral I and an estimated upper bound on the absolute error E. If maxevals is not exceeded then E <= max(atol, rtol*norm(I)) will hold. (Note that it is useful to specify a positive atol in cases where norm(I) may be zero.)

The endpoints a et cetera can also be complex (in which case the integral is performed over straight-line segments in the complex plane). If the endpoints are BigFloat, then the integration will be performed in BigFloat precision as well.

!!! note It is advisable to increase the integration order in rough proportion to the precision, for smooth integrands.

More generally, the precision is set by the precision of the integration endpoints (promoted to floating-point types).

The integrand f(x) can return any numeric scalar, vector, or matrix type, or in fact any type supporting +, -, multiplication by real values, and a norm (i.e., any normed vector space). Alternatively, a different norm can be specified by passing a norm-like function as the norm keyword argument (which defaults to norm).

!!! note Only one-dimensional integrals are provided by this function. For multi-dimensional integration (cubature), there are many different algorithms (often much better than simple nested 1d integrals) and the optimal choice tends to be very problem-dependent. See the Julia external-package listing for available algorithms for multidimensional integration or other specialized tasks (such as integrals of highly oscillatory or singular functions).

The algorithm is an adaptive Gauss-Kronrod integration technique: the integral in each interval is estimated using a Kronrod rule (2*order+1 points) and the error is estimated using an embedded Gauss rule (order points). The interval with the largest error is then subdivided into two intervals and the process is repeated until the desired error tolerance is achieved.

These quadrature rules work best for smooth functions within each interval, so if your function has a known discontinuity or other singularity, it is best to subdivide your interval to put the singularity at an endpoint. For example, if f has a discontinuity at x=0.7 and you want to integrate from 0 to 1, you should use quadgk(f, 0,0.7,1) to subdivide the interval at the point of discontinuity. The integrand is never evaluated exactly at the endpoints of the intervals, so it is possible to integrate functions that diverge at the endpoints as long as the singularity is integrable (for example, a log(x) or 1/sqrt(x) singularity).

For real-valued endpoints, the starting and/or ending points may be infinite. (A coordinate transformation is performed internally to map the infinite interval to a finite one.)


In [33]:
Tf=Float64(T)
sinecoef(f, m, x0, x1, Tf) = 2 * quadgk(x -> f(x) * sin(2*m*π*x/Tf)/Tf, x0,x1)[1]
coscoef(f, m, x0, x1, Tf) = 2 * quadgk(x -> f(x) * cos(2*m*π*x/Tf)/Tf, x0,x1)[1]

# i druga funkcija koja računa na vektoru prirodnih brojeva
sinecoef(f, M::AbstractVector,x0,x1,Tf) = Float64[sinecoef(f,m,x0,x1,Tf) for m in M]
coscoef(f, M::AbstractVector,x0,x1,Tf) = Float64[coscoef(f,m,x0,x1,Tf) for m in M]


Out[33]:
coscoef (generic function with 2 methods)

In [35]:
# First, define a function to evaluate N terms of the sine series, given the coefficients b
function fouriersum(a, b, x, T)
    f = a[1]/2
    for n = 1:length(b)
        f += a[n+1]* cos(2*n*π*x/T) + b[n] * sin(2*n*π*x/T)
    end
    return f
end
fouriersum(a, b, X::AbstractVector, T) = Float64[fouriersum(a, b,x,T) for x in X]


Out[35]:
fouriersum (generic function with 2 methods)

In [36]:
# Uzmimo više točaka (inače "nestane" Gibbsov efekt!!)
X=range(x0,x1,length=1000)
# try for n=1:2:99
n=50
an = coscoef(f, 0:n, x0, x1, Tf)
bn = sinecoef(f, 1:n, x0, x1, Tf)
xlabel="x"
title="Funkcija i "*string(n)*" članova reda"
plot(f,x0,x1,lab="exact f")
plot!(X, fouriersum(an, bn, X, Tf),lab="$n-term sine series",
    xlabel=xlabel,title=title)


Out[36]:

Primjeri


In [37]:
f(x)=x^2
x₀=-1
x₁=1
T=x₁-x₀


Out[37]:
2

In [38]:
a(3)


Out[38]:
\begin{equation*}- \frac{4}{9 \pi^{2}}\end{equation*}

In [39]:
f(x)


Out[39]:
\begin{equation*}x^{2}\end{equation*}

In [40]:
a(n)


Out[40]:
\begin{equation*}\frac{1}{625 \pi^{2}}\end{equation*}

In [41]:
b(2)


Out[41]:
\begin{equation*}0\end{equation*}

In [42]:
f(2)


Out[42]:
4

In [43]:
plot(f,x₀,x₁)


Out[43]:

In [44]:
x0=Float64(x₀)
x1=Float64(x₁)
# Try for N=1:10
N=10
S=a(0)/2+sum([a(n)*cos(2*PI*n*x/T)+b(n)*sin(2*PI*n*x/T) for n=1:N])
g(x)=S(x)
title="Funkcija i "*string(N)*" članova reda"
plot(f(x),x₀,x₁)
plot!(g,x₀,x₁,title=title)


Out[44]:

In [45]:
S


Out[45]:
\begin{equation*}- \frac{4 \cos{\left(\pi x \right)}}{\pi^{2}} + \frac{\cos{\left(2 \pi x \right)}}{\pi^{2}} - \frac{4 \cos{\left(3 \pi x \right)}}{9 \pi^{2}} + \frac{\cos{\left(4 \pi x \right)}}{4 \pi^{2}} - \frac{4 \cos{\left(5 \pi x \right)}}{25 \pi^{2}} + \frac{\cos{\left(6 \pi x \right)}}{9 \pi^{2}} - \frac{4 \cos{\left(7 \pi x \right)}}{49 \pi^{2}} + \frac{\cos{\left(8 \pi x \right)}}{16 \pi^{2}} - \frac{4 \cos{\left(9 \pi x \right)}}{81 \pi^{2}} + \frac{\cos{\left(10 \pi x \right)}}{25 \pi^{2}} + \frac{1}{3}\end{equation*}

In [47]:
# Po dijelovima definirana funkcija
# f(x)=x<0 ? 0 : x
# f(x)=sympy.Piecewise((0,x<=0),(x,x>0))
f(x)=x*Heaviside(x)
plot(f(x),x₀,x₁)


Out[47]:

In [48]:
# Try for N=1:10
N=10
S=a(0)/2+sum([a(n)*cos(2*PI*n*x/T)+b(n)*sin(2*PI*n*x/T) for n=1:N])
g(x)=S(x)
title="Funkcija i "*string(N)*" članova reda"
plot(f(x),x₀,x₁)
plot!(g,x₀,x₁,title=title)


Out[48]:

In [36]:
S


Out[36]:
\begin{equation*}\frac{\sin{\left(\pi x \right)}}{\pi} - \frac{\sin{\left(2 \pi x \right)}}{2 \pi} + \frac{\sin{\left(3 \pi x \right)}}{3 \pi} - \frac{\sin{\left(4 \pi x \right)}}{4 \pi} + \frac{\sin{\left(5 \pi x \right)}}{5 \pi} - \frac{\sin{\left(6 \pi x \right)}}{6 \pi} + \frac{\sin{\left(7 \pi x \right)}}{7 \pi} - \frac{\sin{\left(8 \pi x \right)}}{8 \pi} + \frac{\sin{\left(9 \pi x \right)}}{9 \pi} - \frac{\sin{\left(10 \pi x \right)}}{10 \pi} - \frac{2 \cos{\left(\pi x \right)}}{\pi^{2}} - \frac{2 \cos{\left(3 \pi x \right)}}{9 \pi^{2}} - \frac{2 \cos{\left(5 \pi x \right)}}{25 \pi^{2}} - \frac{2 \cos{\left(7 \pi x \right)}}{49 \pi^{2}} - \frac{2 \cos{\left(9 \pi x \right)}}{81 \pi^{2}} + \frac{1}{4}\end{equation*}