10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis...

82
1 © 2001 – Gilberto E. Urroz All rights reserved Orthogonal Functions, Gaussian Quadrature, and Fourier Analysis with SCILAB By Gilberto E. Urroz, Ph.D., P.E. Distributed by i nfoClearinghouse.com ©2001 Gilberto E. Urroz All Rights Reserved

Transcript of 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis...

Page 1: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

1 © 2001 – Gilberto E. Urroz All rights reserved

Orthogonal Functions, Gaussian Quadrature, and Fourier Analysis with

SCILAB

By

Gilberto E. Urroz, Ph.D., P.E.

Distributed by

i nfoClearinghouse.com

©2001 Gilberto E. Urroz All Rights Reserved

Page 2: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

2 © 2001 – Gilberto E. Urroz All rights reserved

OOrrtthhooggoonnaall ffuunnccttiioonnss,, GGaauussssiiaann qquuaaddrraattuurree,, aanndd FFoouurriieerr aannaallyyssiiss Orthogonal functions are sets of functions of an independent variable, say x, given in terms of an index n, i.e., gn(x). The functions gn(x) and gm(x) are said to be orthogonal on an interval a < x < b, if

for n ≠ m. The notation (gn,gm) is used when defining linear vector spaces to indicate the inner or dot product of two vectors, thus, for vectors u and v, (u,v) = u• v. If vectors u and v are orthogonal (in the Euclidean sense), (u,v) = u• v = 0. By similarity with vectors, we can state that if (gn,gm) = 0 for a set of functions gn(x), then functions gn and gm are orthogonal. Orthogonal functions have a variety of applications in mathematics. For example, sets of orthogonal sine and cosine functions can be used to expand a function into what is called a Fourier series. Solutions to certain types of second-order differential equations constitute sets of orthogonal functions (e.g., Bessel functions, Lagrange polynomials, Laguerre polynomials, etc.) which can be used for evaluating integrals through a procedure refer to as Gaussian quadrature.

SSoolluuttiioonnss ttoo sseelleecctteedd ssppeecciiffiicc sseeccoonndd--oorrddeerr ddiiffffeerreennttiiaall eeqquuaattiioonnss

In this section we present and solve specific types of ordinary differential equations whose solutions are defined in terms of some classical functions, e.g., Bessel’s functions, Hermite polynomials, etc. Most of the functions that solve these differential equations are orthogonal functions.

The Cauchy or Euler equation An equation of the form

x2⋅(d2y/dx2) + a⋅x⋅ (dy/dx) + b⋅y = 0,

where a and b are real constants, is known as the Cauchy or Euler equation. A solution to the Cauchy equation can be found by assuming that

y(x) = xn. Replacing this function into the differential equation and simplifying results in an auxiliary algebraic equation:

,0)()(),( =⋅⋅= ∫b

a mnmn dxxgxggg

Page 3: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

3 © 2001 – Gilberto E. Urroz All rights reserved

If the equation has two different roots, say n1 and n2, then the general solution of this

equation is

y(x) = K1⋅x n1 + K2⋅x n2.

If b = (1-a)2/4, then the equation has a double root n1 = n2 = n = (1-a)/2, and the solution turns out to be

y(x) = (K1 + K2⋅ln x)xn.

Legendre’s equation An equation of the form

(1-x2)⋅(d2y/dx2)-2⋅x⋅ (dy/dx)+n⋅ (n+1) ⋅y = 0, where n is a real number, is known as the Legendre’s differential equation. Any solution for this equation is known as a Legendre’s function. When n is a nonnegative integer, the solutions are called Legendre’s polynomials. Legendre’s polynomial of order n is given by

where M = n/2 or (n-1)/2, whichever is an integer. The first six Legendre polynomials are

P0(x) = 1.0,

P1(x) = x,

P2(x) = (3x2-1)/2,

P3(x) =(5x3-3x)/2,

P4(x) =(35x4-30x2+3)/8,

P5(x) =(63x5-70x3+15x)/8.

Gamma, log-gamma and di-gamma functions Notice that the definition of the Legendre polynomials involves the use of factorials. SCILAB, however, does not provide a factorial function per se. Instead it relies on the use of the Gamma function, Γ(α), defined as

.0)1(2 =+⋅−+ bnan

mnM

mn

mn x

mnmnmmnxP 2

0 )!2()!(!2)!22()1()( −

=∑ ⋅

−⋅−⋅⋅−⋅−=

......)!2()!1(!12

)!22()!(2

)!2( 22 −+⋅

−−⋅⋅−−⋅

⋅= −n

nn

n xnn

nxnn

Page 4: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

4 © 2001 – Gilberto E. Urroz All rights reserved

∫∞ −−=Γ

0

1 ,)( dtte t αα

which is related to the factorial function for α = n integer by:

Γ(n+1) = n!, or Γ (n) = (n-1)!. SCILAB also provides function gammaln, which calculates the natural logarithm of the Gamma function (the log-gamma function), i.e.,

gammaln(x) = ln[Γ(x)]. Related to the log-gamma function is the di-gamma function, defined as the derivative of the log-gamma function:

ψ(z) = d[ln Γ(z)]/dz = Γ’(z)/ Γ (z),

which can be calculated by using SCILAB function dlgamma(x). Legendre’s polynomials can be generated by using the following user-defined function legendrepol: function [pL] = legendrepol(n,var) //Generates the Legendre polynomial //of order n in variable var if n == 0 then cc = [1]; elseif n == 1 then cc = [0 1]; else if modulo(n,2) == 0 then M = n/2 else M = (n-1)/2 end; cc = zeros(1,M+1); for m = 0:M k = n-2*m; cc(k+1)=... (-1)^m*gamma(2*n-2*m+1)/(2^n*gamma(m+1)*gamma(n-m+1)*gamma(n-2*m+1)); end; end; pL = poly(cc,var,'coeff'); //End function legendrepol The following SCILAB statements generate the first 10 Legendre polynomials as elements of a column vector p. -->getf('legendrepol')

Page 5: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

5 © 2001 – Gilberto E. Urroz All rights reserved

-->p = zeros(10,1); -->for nn = 1:10 --> p(nn) = legendrepol(nn,'x'); -->end; -->p p = ! x ! ! ! ! 2 ! ! - .5 + 1.5x ! ! ! ! 3 ! ! - 1.5x + 2.5x ! ! ! ! 2 4 ! ! .375 - 3.75x + 4.375x ! ! ! ! 3 5 ! ! 1.875x - 8.75x + 7.875x ! ! ! ! 2 4 6 ! ! - .3125 + 6.5625x - 19.6875x + 14.4375x ! ! ! ! 3 5 7 ! ! - 2.1875x + 19.6875x - 43.3125x + 26.8125x ! ! ! ! 2 4 6 8 ! ! .2734375 - 9.84375x + 54.140625x - 93.84375x + 50.273438x ! ! ! ! 3 5 7 9 ! ! 2.4609375x - 36.09375x + 140.76562x - 201.09375x + 94.960937x ! ! ! ! 2 4 6 8 10 ! ! - .2460938 + 13.535156x - 117.30469x + 351.91406x - 427.32422x + 180.42578x ! A plot of Legendre polynomials P1(x), P2(x), P3(x), P4(x), and P5(x), is produced using the following SCILAB commands. A matrix pp of five columns stores the values of the polynomials, one per column, corresponding to a vector of values of x, xx. -->getf('legendrepol') -->xx = [-1:0.01:1]; -->n = length(xx) n = 201. -->for j = 1:5 --> pp(:,j) = horner(p(j),xx'); -->end; -->plot2d([xx',xx',xx',xx',xx'],[pp(:,1),pp(:,2),pp(:,3),pp(:,4),pp(:,5)],... -->[1:1:5],'111','n=1@n=2@n=3@n=4@n=5',[-1 -1 1 1 ]) -->xtitle('Legendre polynomials','x','Pn(x)')

Page 6: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

6 © 2001 – Gilberto E. Urroz All rights reserved

The result is the following graph:

The ODE (1-x2)⋅(d2y/dx2)-2⋅x⋅ (dy/dx)+[n⋅ (n+1)-m2/(1-x2)] ⋅y = 0,

has for solution the function

y(x) = Pnm(x)= (1-x2)m/2⋅(dmPn/dxm).

This function is referred to as an associated Legendre function.

Bessel’s equation The ordinary differential equation

x2⋅(d2y/dx2) + x⋅ (dy/dx)+ (x2-ν2) ⋅y = 0,

Page 7: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

7 © 2001 – Gilberto E. Urroz All rights reserved

where the parameter ν is a nonnegative real number, is known as Bessel’s differential equation. Solutions to Bessel’s equation are given in terms of Bessel functions of the first kind of order νννν:

where ν is not an integer. If ν = n, an integer, the Bessel functions of the first kind for n = integer are defined by

For non-integer values ν, the solution to the Bessel equation is given by

y(x) = K1⋅Jν(x)+K2⋅J-ν(x). For integer values, the functions Jn(x) and J-n(x) are linearly dependent, since

Jn(x) = (-1)n⋅J-n(x), therefore, we cannot use them to obtain a general function to the equation. Instead, we introduce the Bessel functions of the second kind defined as

Yν(x) = [Jν(x) cos νπ – J−ν(x)]/sin νπ,

for non-integer ν, and for n integer, with n > 0, by

where γ is the so-called Euler constant, defined by

∑∞

=+ ++Γ⋅⋅

⋅−⋅=0

2

2

,)1(!2

)1()(m

m

mm

mmxxxJ

ννν

ν

∑∞

=+ +⋅⋅

⋅−⋅=0

2

2

.)!(!2

)1()(m

nm

mmn

n mnmxxxJ

m

mnm

nmmmn

nn xnmm

hhxxxJxY 2

02

1

)!(!2)()1(

)2

(ln)(2)( ⋅+⋅⋅

+⋅−⋅++⋅⋅= ∑

=+

+−

πγ

π

mn

mnm

n

xm

mnx 21

02 !2

)!1( ⋅⋅−−⋅− ∑

=−

π

...,05772156649.0]ln1...31

211[lim ≈−++++=

∞→r

rrγ

Page 8: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

8 © 2001 – Gilberto E. Urroz All rights reserved

and hm represents the harmonic series

For the case n = 0, the Bessel function of the second kind is defined as

With these definitions, a general solution of Bessel’s equation for all values of ν is given by

y(x) = K1⋅Jν(x)+K2⋅Yν(x). In some instances, it is necessary to provide complex solutions to Bessel’s equations by defining the Bessel functions of the third kind of order νννν as

Hn(1)(x) = Jν(x)+i⋅Yν(x),

and Hn

(2)(x) = Jν(x)−i⋅Yν(x),

These functions are also known as the first and second Hankel functions of order νννν. In some applications you may also have to utilize the so-called modified Bessel functions of the first kind of order νννν defined as Iν(x)= i-ν⋅Jν(i⋅x), where i is the unit imaginary number. These functions are solutions to the differential equation

x2⋅(d2y/dx2) + x⋅ (dy/dx)- (x2+ν2) ⋅y = 0. The modified Bessel functions of the second kind,

Kν(x) = (π/2)⋅[I-ν (x)−Iν (x)]/sin νπ,

are also solutions of this ODE. Bessel functions are available in SCILAB through the following functions:

• besselj: Bessel functions of the first kind [Jν(x)], with general call:

y = besselj(nu,x)

• bessely: Bessel functions of the second kind [Yν(x)], with general call:

.1...31

211

mhm ++++=

.)!(2

)1()

2(ln)(2)( 2

022

1

00

⋅⋅−

++⋅⋅= ∑∞

=

−m

mm

mm

xm

hxxJxY γπ

Page 9: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

9 © 2001 – Gilberto E. Urroz All rights reserved

y = bessely(nu,x))

• besseli: modified Bessel functions of the first kind [Iν(x)], with general calls:

y = besseli(nu,x)

y = besseli(nu,x,s) • besselk: modified Bessel functions of the second kind [Kν(x)], with general calls:

y = besselk(nu,x)

y = besselk(nu,x,s)

In the calls to functions besselj, bessely, besseli, and besselk, the parameter nu represents the order of the function ν, and x is a single non-negative value or vector of non-negative values where the function is to be evaluated. The calls of functions besseli and besselk may include the parameter s whose default value is 1. When the value of s is 2, the so-called exponentially scaled Bessel functions are calculated. To see a plot of the functions Jn(x), for n = 0,1,2,3,4, use the following SCILAB commands: -->xx = [0:0.1:10]; nu = [0,1,2,3,4]; n = length(xx), m = length(nu) n = 101. m = 5. -->bb = zeros(n,m); -->for j = 1:m --> bb(:,j) = besselj(nu(j),xx); -->end; -->plot2d([xx',xx',xx',xx',xx'],[bb(:,1),bb(:,2),bb(:,3),bb(:,4),bb(:,5)],... -->[1,2,3,4,5],'111','nu=1@nu=2@nu=3@nu=4@nu=5',[0,-0.6,10,1]) -->xtitle('Bessel functions J_nu(x)','x','J_nu(x)')

Page 10: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

10 © 2001 – Gilberto E. Urroz All rights reserved

Chebyshev polynomials The functions

Tn(x) = cos(n⋅cos-1 x), and Un(x) = sin[(n+1) cos-1 x]/(1-x2)1/2, n = 0, 1, … are called Chebyshev polynomials of the first and second kind, respectively. The polynomials Tn(x) are solutions of the differential equation

(1-x2)⋅(d2y/dx2) − x⋅ (dy/dx) + n2⋅y = 0. In SCILAB, the function chepol generates the Chebyshev polynomial of the first kind of order n, given a value of n > 0. The general call to the function is

[Tn]=chepol(n,var) where n is the order of the polynomial, and var is a SCILAB polynomial variable. Chebyshev polynomials of the second kind are obtained from:

Un(x) = sin(n⋅arccos(x))/sin(arccos(x)). The first four Chebyshev or Tchebycheff polynomials of the first and second kind are:

T0(x) = 1.0, U1(x) = 1.0, T1(x) = x, U1(x) =1.0,

T2(x) =2x2-1, U2(x) =2x, T3(x) = 4x3-3x, U3(x) =24x2-1. The polynomials of the first kind can be obtained using SCILAB function chepol as follows:

Page 11: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

11 © 2001 – Gilberto E. Urroz All rights reserved

-->T0 = chepol(0,'x') T0 = 1 -->T1 = chepol(1,'x') T1 = x -->T2 = chepol(2,'x') T2 = 2 - 1 + 2x -->T3 = chepol(3,'x') T3 = 3 - 3x + 4x -->T4 = chepol(4,'x') T4 = 2 4 1 - 8x + 8x Plots of the Chebyshev polynomials of the first kind for n = 0, 1, 2, 3, 4, are shown below: -->xx = [-1:0.01:1]; -->y0 = horner(T0,xx);y1 = horner(T1,xx);y2 = horner(T2,xx); -->y3 = horner(T3,xx);y4 = horner(T4,xx); -->plot2d([xx',xx',xx',xx',xx'],[y0',y1',y2',y3',y4'],... -->[1,2,3,4,5],'111','T0@T1@T2@T3@T4',[-1,-1,1,1]) -->xtitle('Chebyshev polynomials','x','T_n(x)')

Page 12: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

12 © 2001 – Gilberto E. Urroz All rights reserved

Laguerre’s equation Laguerre’s equation is the second-order, linear ODE of the form

x⋅(d2y/dx2) +(1−x)⋅ (dy/dx) + n⋅y = 0. Laguerre polynomials, defined as

are solutions to Laguerre’s equation. Laguerre’s polynomials can also be calculated with:

The term

is the m-th coefficient of the binomial expansion (x+y)n. It also represents the number of combinations of n elements taken m at a time. The Laguerre polynomials can be generated in SCILAB through the following user-defined function, laguerrepol: function [Lp] = laguerrepol(n,var) //Calculates the Laguerre polynomial //of order n in polynomial variable var if n == 0 then c = [1]; else c = []; for m = 0:n c = [c (-1)^m*gamma(n+1)/((gamma(m+1))^2*gamma(n-m+1))]; end; end; Lp = poly(c,var,'coeff'); //end function laguerrepol The first four Laguerre polynomials are: -->getf('laguerrepol')

,...2,1,)(!

)(,1)(0 =⋅⋅==−

ndx

exdnexLxL n

xnnx

n

.!)1(.......

4)1(1

!)1()( 2

0

nn

mn

m

m

n xn

xnnxnxmn

mxL ⋅−++−⋅−+⋅−=⋅

⋅−=∑

=

),()!(!

! mnCmnm

nmn

=−

=

Page 13: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

13 © 2001 – Gilberto E. Urroz All rights reserved

-->L0 = laguerrepol(0,'x') L0 = 1 -->L1 = laguerrepol(1,'x') L1 = 1 - x -->L2 = laguerrepol(2,'x') L2 = 2 1 - 2x + .5x -->L3 = laguerrepol(3,'x') L3 = 2 3 1 - 3x + 1.5x - .1666667x -->L4 = laguerrepol(4,'x') L4 = 2 3 4 1 - 4x + 3x - .6666667x + .0416667x The following is the plot of the functions L0(x), L1(x), L2(x), L3(x) and L4(x): -->x = [-2:0.01:6]; -->y0 = horner(L0,x); y1 = horner(L1,x); y2 = horner(L2,x); -->y3 = horner(L3,x); y4 = horner(L4,x); -->plot2d([x',x',x',x',x'],[y0',y1',y2',y3',y4'],... -->[1,2,3,4,5],'111','L0@L1@L2@L3@L4',[-2,-5,6,5]) -->xtitle('Laguerre polynomials','x','L_n(x)')

Weber’s equation and Hermite polynomials

Page 14: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

14 © 2001 – Gilberto E. Urroz All rights reserved

Weber’s equation is defined as

d2y/dx2+(n+1/2-x2/4)y = 0, For n = 0, 1, 2, … A particular solution of this equation is given by the function

y(x) = exp(-x2/4)H*(x/√2), where the function H*(x) is the Hermite polynomial:

To calculate Hermite polynomials in SCILAB we will use the following recurrence equation

H*n+1(x) = 2xH*

n(x)-2nH*n-1(x)

with H0

*(x) = 1, H1*(x) = 2x. A user-defined function to produce the Hermite polynomials is

listed below: function [Hn] = hermitepol(n,var) //determines the Hermite polynomial of order n in variable var H = zeros(1,n+1); H(1) = poly([1],var,'coeff'); H(2) = poly([0 2],var,'coeff'); for j = 2:n H(j+1) = poly([0 2],var,'coeff')*H(j)-2*(j-1)*H(j-1); end; Hn = H(n+1); //end function hermitepol The following are the first five Hermite polynomials: -->getf('hermitepol') -->H0 = hermitepol(0,'x') H0 = 1 -->H1 = hermitepol(1,'x') H1 = 2x -->H2 = hermitepol(2,'x') H2 = 2 - 2 + 4x -->H3 = hermitepol(3,'x') H3 = 3 - 12x + 8x -->H4 = hermitepol(4,'x')

,...2,1),()1()(*,1*22

0 =−== − nedxdexHH x

n

nxn

n

Page 15: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

15 © 2001 – Gilberto E. Urroz All rights reserved

H4 = 2 4 12 - 48x + 16x A plot of these polynomials is shown below: -->x=[-3:0.01:3]; y0 = horner(H0,x); y1 = horner(H1,x); -->y2 = horner(H2,x); y3 = horner(H3,x); y4 = horner(H4,x); -->plot2d([x',x',x',x',x'],[y0',y1',y2',y3',y4'],... -->[1,2,3,4,5],'111','H0@H1@H2@H3@H4',[-3,-30,3,20]) -->xtitle('Hermite polynomials','x','H_n(x)')

IInntteeggrraattiioonn bbyy GGaauussssiiaann qquuaaddrraattuurree In this section we present techniques of integration known as Gaussian quadrature as well as other quadrature procedures that can be used for the calculation of improper integrals. Most of the quadrature formulae require coefficients that can be obtained from orthogonal polynomials such as Legendre, Laguerre, or Hermite polynomials.

Gaussian quadrature The idea of Gaussian quadrature for the numerical calculation of integrals consists in identifying a discrete number of points xi, i = 1,2,…,n, and a corresponding number of constants, Ai, i = 1,2,…,n, so that the integral is estimated as

Page 16: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

16 © 2001 – Gilberto E. Urroz All rights reserved

∫ ∑=

≈b

a

n

iii xfAdxxf

1)()( .

The figure below illustrates the meaning of the coefficients Ai as the base of rectangles whose heights are given by f(xi). The sum of the areas of the rectangles approximates the integral.

Suppose that we take a = -1 and b = 1 in the integral, and we attempt to determine the values of xi and Ai for n = 2. We can accomplish this solution by calculating the Gaussian integral

2),()()()(1

1 22111

=+=≈∫ ∑−=

nxfAxfAxfAdxxfn

iii

exactly for the cases f(x) = 1, f(x) = x, f(x) = x2, f(x) = x3. Replacing these polynomials for f(x) will produce four equations from which we can solve for the four unknowns, x1, x2, A1, and A2. The equations produced are:

∫− +==⋅⇒=1

1 21 ,211)(For AAdxxf

∫− +==⋅⇒=1

1 2211 ,0)(For xAxAdxxxxf

∫− +==⋅⇒=1

1

222

211

22 ,32)(For xAxAdxxxxf

∫− +==⋅⇒=1

1

322

311

33 .0)(For xAxAdxxxxf

The solution of the system of algebraic equations thus generated is

x1 = -1/√3, x2 = 1/√3, A1 = 1, A2 = 1.

Page 17: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

17 © 2001 – Gilberto E. Urroz All rights reserved

The required integral is now written as

∫− +−≈1

1).

31()

31()( ffdxxf

The integral calculated with this formula will be exact if f(x) = 1, x, x2, or x3, and approximate for any other function. If we want to calculate an integral of function f(x) in the interval [a,b] we introduce the following change of variable

,2ab

baxt−

−−=

so that for x = a, t = -1, for x = b, t = 1, and dx =(1/2)(b-a)dt, thus the integral is calculated as

∫ ∫−++⋅−⋅−=

b

adtabtabfabdxxf

1

1)

22()(

21)(

)].223

1()223

1([)(21 ababfababfab ++−⋅+++−⋅−⋅−≈

General form of Gaussian quadrature We can generalize the previous result for any integer n by writing

.)22

()(21)

22()(

21)(

1

1

1 ∑∫ ∫=

++⋅−⋅⋅−≈++⋅−⋅−=n

iii

b

a

abxabfAabdtabtabfabdxxf

where xi, i = 1,2,…,n, are the values obtained from

∫ ∑−=

≈1

11

)()(n

iii xfAdxxf .

For values of n>2, the values xi and Ai are calculated by solving the 2n equations of the form

.12,...,2,1,0,,...,2,1,1

11

−⋅=== ∫∑ −=

nknidxxxA kn

i

kii

It can be shown that the values of xi, i=1,2,…,n, are the roots of the Legendre polynomial Pn(x), while the coefficients Ai, i = 1,2,…,n, are related to the derivative of the n-th Legendre polynomial, P’n(x) = dPn/dx, by

.)](')[1(

222

inii xPx

A−

=

Page 18: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

18 © 2001 – Gilberto E. Urroz All rights reserved

Legendre’s polynomial of order n are the solutions to the Legendre’s ordinary differential equation

(1-x2)⋅(d2y/dx2)-2⋅x⋅ (dy/dx)+n⋅ (n+1) ⋅y = 0, and are given by

where M = n/2 or (n-1)/2, whichever is an integer.

User-defined SCILAB function for calculating Gaussian quadrature A user-defined SCILAB function, legendrepol, was introduced earlier for generating Legendre polynomials. The following function, gaussquad, incorporates function legendrepol for the calculation of Gaussian integrals according to the formulae developed earlier. function [I] = gaussquad(a,b,f,n) //Calculates the integral of function f //in the range [a,b] using the Gaussian //quadrature approximation with n>=2 intervals p = legendrepol(n,'x'); xr = roots(p); A = []; for j = 1:n pd = derivat(p); A = [A 2/((1-xr(j)^2)*(horner(pd,xr(j)))^2)]; end; tr = ((b-a)/2.*xr)+((b+a)/2); I = (b-a)/2*A*f(tr); //end of function gaussquad //================================================ function [pL] = legendrepol(n,var) //Generates the Legendre polynomial //of order n in variable var if n == 0 then cc = [1]; elseif n == 1 then cc = [0 1]; else if modulo(n,2) == 0 then M = n/2 else M = (n-1)/2 end; cc = zeros(1,M+1); for m = 0:M k = n-2*m; cc(k+1)=... (-1)^m*gamma(2*n-2*m+1)/(2^n*gamma(m+1)*gamma(n-m+1)*gamma(n-2*m+1));

mnM

mn

mn x

mnmnmmnxP 2

0 )!2()!(!2)!22()1()( −

=∑ ⋅

−⋅−⋅⋅−⋅−=

......)!2()!1(!12

)!22()!(2

)!2( 22 −+⋅

−−⋅⋅−−⋅

⋅= −n

nn

n xnn

nxnn

Page 19: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

19 © 2001 – Gilberto E. Urroz All rights reserved

end; end; pL = poly(cc,var,'coeff'); //End function legendrepol Application of function gaussquad To illustrate the use of function gaussquad we will use it to integrate the function f(x) = ex/(1+x2)2 for n = 2, 3, 4, 5, 6, 7, 8 , 9, 10, 12, 14, 16, 18, and 20. A plot of the function in the range of integration is presented below. -->deff('[y]=f(x)','y=exp(x)./(1+x^2)^2') -->x=[0:0.05:10];y=f(x);plot(x,y,'x','y','f(x)=exp(x)/(1+x^2)^2')

The following table shows the values of the integral: -->Res = [ ]; -->for j=1:length(nn) --> Res=[Res; [nn(j),gaussquad(0,10,f,nn(j))]]; -->end; -->disp(' n Int'), disp(Res) n Int ! 2. 4.7168792 ! ! 3. 5.7578069 ! ! 4. 5.9621965 ! ! 5. 5.8817127 ! ! 6. 5.8034783 ! ! 7. 5.7774564 ! ! 8. 5.778712 ! ! 9. 5.7846526 ! ! 10. 5.7877236 ! ! 12. 5.7879883 ! ! 14. 5.7876393 !

Page 20: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

20 © 2001 – Gilberto E. Urroz All rights reserved

! 16. 5.787655 ! ! 18. 5.7876657 ! ! 20. 5.7876644 !

Improper integrals Improper integrals is the name given to definite integrals that have infinite limits, for example:

∫∫∫ ∞−

∞− +==

+=

1

20 22

1

221 .1

,,1 x

dxIxdxI

xdxI

Symbolic integration of improper integrals is accomplished by using limits of the definite integral as a dummy variable, ε, approaches plus or minus infinity, e.g.,

.4

3)2

(4

))arctan(4

(lim|)arctan(lim1

lim1

11

2

1

21πππεπ

εεεεε=−−=−==

+=

+=

−∞→−∞→∞− −∞→ ∫∫ xx

dxx

dxI

Gauss-Laguerre formulae for improper integrals Gauss-Laguerre formulae can be used to numerically evaluate integrals of the form shown below through a procedure similar to Gaussian quadrature:

∫ ∑∞

=

− ⋅≈0

1.)()(

n

iii

x xgAdxxge

The values of xi, i=1,2,…,n, are the roots of the Laguerre polynomial on order n, Ln(x), and the coefficients Ai, i=1,2,…,n, are calculated from

.)]('[

1)]([)1( 22

12

inin

ii xLxxLn

xA

⋅=

+=

+

Laguerre polynomials are the solutions to the Laguerre’s differential equation

x⋅(d2y/dx2) +(1−x)⋅ (dy/dx) + n⋅y = 0, and are given by

where

),()!(!

! mnCmnm

nmn

=−

=

.!)1(.......

4)1(1

!)1()( 2

0

nn

mn

m

m

n xn

xnnxnxmn

mxL ⋅−++−⋅−+⋅−=⋅

⋅−=∑

=

Page 21: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

21 © 2001 – Gilberto E. Urroz All rights reserved

is the binomial coefficient. To calculate an improper integral of a general function, f(x), we will use the following formula:

∫ ∑∫∞

=

−∞⋅⋅≈=

01

0.)()}({)(

n

ii

xi

xx xfeAdxxfeedxxf i

Of course, the indefinite integral shown can be evaluated only if the integrand decreases quickly towards zero as x grows without bound. The integral in this case is said to be convergent. SCILAB user-defined function for calculating Gauss-Laguerre quadrature Earlier in this chapter we defined function laguerrepol to generate Laguerre polynomials. The following function, laguerrequad, which incorporates function laguerrepol, is used to calculate the improper integral shown above. function [I] = laguerrequad(f,n) //Calculates the improper integral of function f(x) //in the interval [0,infinity] using Gauss-Laguerre //quadrature with n >= 2 intervals p = laguerrepol(n,'x'); pd = derivat(p); pnp1 = laguerrepol(n+1,'x'); xr = roots(p); A=[]; for i = 1:n A = [A xr(i)/((n+1)^2*(horner(pnp1,xr(i)))^2)]; end; I = A*(exp(xr).*f(xr)); //end of function laguerrequad //=============================================================== function [Lp] = laguerrepol(n,var) //Calculates the Laguerre polynomial //of order n in polynomial variable var if n == 0 then c = [1]; else c = []; for m = 0:n c = [c (-1)^m*gamma(n+1)/((gamma(m+1))^2*gamma(n-m+1))]; end; end; Lp = poly(c,var,'coeff');

Page 22: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

22 © 2001 – Gilberto E. Urroz All rights reserved

//end function laguerrepol Application of function laguerrequad to calculate improper integral We use function laguerrequad to calculate the improper integral

∫∞

+=

0 21 .1 x

dxI

The following SCILAB commands define the function to be integrated, f(x) = 1/(1+x2). A plot of the function is shown next. -->deff('[y]=f(x)','y=(1+x^2)^(-1)') -->x=[0:0.1:10];y=f(x);plot(x,y,'x','y','f(x)=1/(1+x^2)')

The following commands calculate the improper integral for values of n=2,4,…,30: -->getf('laguerrequad') -->Res=[]; for j=2:2:30, Res = [Res; [j laguerrequad(f,j)]]; end; -->disp(Res) ! 2. 1.493257 ! ! 4. 1.5011895 ! ! 6. 1.5188948 ! ! 8. 1.53376 ! ! 10. 1.5425763 ! ! 12. 1.5478021 ! ! 14. 1.5512373 ! ! 16. 1.5537377 ! ! 18. 1.555679 ! ! 20. 1.5572394 ! ! 22. 1.558519 ! ! 24. 1.5595837 ! ! 26. 1.5604823 ! ! 28. 1.5612765 ! ! 30. 1.567911 !

Page 23: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

23 © 2001 – Gilberto E. Urroz All rights reserved

The exact value of the integral is π/2: -->%pi/2 ans = 1.5707963 Gauss-Hermite formulae for improper integrals Gauss-Hermite formulae can be used to numerically evaluate integrals of the form shown below through a procedure similar to Gaussian or Gauss-Laguerre quadrature:

∫ ∑∞

∞−=

− ⋅≈ .)()(1

2n

iii

x xgAdxxge

The values of xi, i=1,2,…,n, are the roots of the Hermite polynomial on order n, Hn(x), and the coefficients Ai, i=1,2,…,n, are calculated from

.)]('[

!2)]([

!22

1

21

2

1

in

n

in

n

i xHn

xHnnA ππ +

==

Hermite polynomials are defined as:

To calculate an improper integral of a general function, f(x), we will use the following formula:

∫ ∑∫∞

∞−=

−∞

∞−⋅⋅≈= .)()}({)(

1

222n

ii

xi

xx xfeAdxxfeedxxf i

The indefinite integral shown can be evaluated numerically only if it is convergent. SCILAB user-defined function for calculating Gauss-Hermite quadrature Earlier in the chapter we defined function hermitepol to generate Hermite polynomials. The following function, hermitequad, which incorporates function hermitepol, is used to calculate the improper integral shown above. function [I] = hermitequad(f,n) //Calculates the improper integral of function //f(x) in the range [-infinity, infinity] using //n intervals and the Gauss-Hermite formulae p = hermitepol(n,'x');

,...2,1),()1()(,122

0 =−== − nedxdexHH x

n

nxn

n

Page 24: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

24 © 2001 – Gilberto E. Urroz All rights reserved

pnp1 = hermitepol(n-1,'x'); xr = roots(p); A = []; for j = 1:n A = [A 2^(n-1)*gamma(n+1)*sqrt(%pi)/(n^2*horner(pnp1,xr(j))^2)]; end; I = A*(exp(xr.^2).*f(xr)); //end function hermitequad //--------------------------------------------- function [Hn] = hermitepol(n,var) //determines the Hermite polynomial //of order n in variable var H = zeros(1,n+1); H(1) = poly([1],var,'coeff'); H(2) = poly([0 2],var,'coeff'); for j = 2:n H(j+1) = poly([0 2],var,'coeff')*H(j)-2*(j-1)*H(j-1); end; Hn = H(n+1); //end function hermitepol Application of function hermitequad for calculating improper integrals We use function hermitequad to calculate the integral of the function f(x) = exp(-|x|). A plot of the function is shown next: -->deff('[y]=f(x)','y=exp(-abs(x))') -->x=-4:0.1:4;y=f(x);plot(x,y,'x','y','f(x)=exp(-x^2)')

Values of the improper integral for n = 2, 4, …, 30, are shown next: -->getf('hermitequad') -->Res=[]; for j = 2:2:30, Res=[Res; [j, hermitequad(f,j)]]; end; -->Res Res = ! 2. 1.4408859 ! ! 4. 1.730544 ! ! 6. 1.8420198 ! ! 8. 1.8963115 ! ! 10. 1.9264848 ! ! 12. 1.9447991 ! ! 14. 1.9566605 ! ! 16. 1.9647431 ! ! 18. 1.9704841 !

Page 25: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

25 © 2001 – Gilberto E. Urroz All rights reserved

! 20. 1.9747063 ! ! 22. 1.9779055 ! ! 24. 1.9803932 ! ! 26. 1.9823717 ! ! 28. 1.9839769 ! ! 30. 1.9853024 !

Integrals involving sine and cosine terms - Filon’s formulae Filon’s formulae is used to evaluate integrals involving a single sine or cosine term. The general formulae are

,])}sin()()sin()({[)cos()(0

00∫ ⋅+⋅+−⋅∆=⋅nx

x oenn kxxfkxxfxdxkxxf κγκβα

,])}cos()()cos()({[)sin()(0

00∫ ⋅+⋅+−⋅∆=⋅nx

x oenn kxxfkxxfxdxkxxf σγσβα

with

∆x = (xn-x0)/n, θ = k⋅∆x,

α = (θ2+ θ sin(2 θ)/2 - 2 sin2(θ))/θ 3,

β = 2{ θ (1+cos2(θ) - sin(2 θ)}/ θ 3,

γ = 4(sin(θ)-θ cos(θ))/θ 3,

∑−

=

++=2

,...6,4,200 ),cos()()}cos()()cos()({

21 n

iiinne kxxfkxxfkxxfκ

∑−

=

=1

,...5,3,1

),cos()(n

iiio kxxfκ

∑−

=

++=2

,...6,4,200 ),sin()()}sin()()sin()({

21 n

iiinne kxxfkxxfkxxfσ

and

∑−

=

=1

,...5,3,1

).sin()(n

iiio kxxfσ

The values of xi, i=0,1,2,…,n, are calculated using xi = x0+i⋅∆x. Values x0 and xn are the limits of integration, and n is required to be an even number. SCILAB user-defined function for calculating integrals with Filon’s formulae The following function, filonquad, can be used to calculate the integrals involving sine or cosine function as described above. The first argument in the function call indicates which

Page 26: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

26 © 2001 – Gilberto E. Urroz All rights reserved

type of trigonometric function, sine (‘sin’) or cosine (‘cos’), is to be used in the integral. The other arguments are the lower and upper limits of integration, x0 and xn, the name of the function, f, and the number of (even) sub-intervals, n: function [I] = filonquad(ftype,k,x0,xn,n,f) //Calculates the integral of the function f(x)*cos(k*x), if ftype = 'cos' or of //f(x)*sin(k*x), if ftype = 'sin' in the interval [x0,xn] using Filon's //integration formulae if modulo(n,2) <> 0 | n<=2 then error('filonquad - n must be an even number larger than 2'); abort; end; Dx = (xn-x0)/n; x = [x0+Dx:Dx:xn]; xe = [x(2):2*Dx:x(n-2)]; xo = [x(1):2*Dx:x(n-1)]; theta = k*Dx; alpha = (theta^2+theta*sin(2*theta)/2-2*sin(theta)^2)/theta^3; beta = 2*(theta*(1+cos(theta)^2)-sin(2*theta))/theta^3; gama = 4*(sin(theta)-theta*cos(theta))/theta^3; fxe=[];coskxe=[];sinkxe=[]; for j = 1:length(xe) fxe = [fxe f(xe(j))]; coskxe = [coskxe cos(k*xe(j))]; sinkxe = [sinkxe sin(k*xe(j))]; end; fxo=[];coskxo=[];sinkxo=[]; for j = 1:length(xo) fxo = [fxo f(xo(j))]; coskxo = [coskxo cos(k*xo(j))]; sinkxo = [sinkxo sin(k*xo(j))]; end; if ftype == 'cos' then kappa_e = 0.5*(f(x0)*cos(k*x0)+f(xn)*cos(k*xn)) + fxe*coskxe'; kappa_o = fxo*coskxo'; I = Dx*(alpha*(f(xn)*sin(k*xn)-f(x0)*sin(k*x0))+beta*kappa_e+gama*kappa_o); else sigma_e = 0.5*(f(x0)*sin(k*x0)+f(xn)*sin(k*xn)) + fxe*sinkxe'; sigma_o = fxo*sinkxo'; I = Dx*(alpha*(f(x0)*cos(k*x0)-f(xn)*cos(k*xn))+beta*sigma_e+gama*sigma_o); end; //end of function filonquad Application of function filonquad The following SCILAB commands are used to generate tables of the integrals calculated through Filon’s integral formulae with function filonquad. The function f(x) in the integrals is f(x) = exp(-x), and it is integrated between x0 = 2 and xn = 4.5. The parameter k in sin(kx) or cos(kx) takes values k = 1,2,3,4,5, one value of k for each column of the tables, while the number of sub-intervals takes values n = 4,6,…,20, corresponding to each row in the tables. The first table corresponds to integrals involving sine functions while the second table corresponds to integrals containing cosine functions. -->deff('[y]=f(x)','y=exp(-x)') //define function -->x0 = 2; xn = 4.5; //define limits of integration

Page 27: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

27 © 2001 – Gilberto E. Urroz All rights reserved

-->kk = [1:1:5]; //define values of k = 1,2,3,4,5 -->nn = [4:2:20] //define values of n = 4,6,…,20 -->mk = length(kk); mn = length(nn); -->Res=zeros(mn,mk); //Table for function with sin(k*x) -->for i = 1:mn --> for j = 1:mk --> Res(i,j) = filonquad('sin',kk(j),x0,xn,nn(i),f); --> end; -->end; -->Res Res = ! .0403248 - .0530721 .0322401 .0026027 - .0234170 ! ! .0400408 - .0527902 .0322865 .0021268 - .0226910 ! ! .0399930 - .0527517 .0323124 .0020419 - .0226145 ! ! .0399799 - .0527421 .0323205 .0020210 - .0226019 ! ! .0399752 - .0527387 .0323235 .0020140 - .0225983 ! ! .0399732 - .0527374 .0323248 .0020110 - .0225970 ! ! .0399723 - .0527367 .0323255 .0020096 - .0225964 ! ! .0399718 - .0527364 .0323258 .0020089 - .0225961 ! ! .0399715 - .0527362 .0323260 .0020085 - .0225959 ! -->Res=zeros(mn,mk); //Table for function with sin(k*x) -->for i = 1:mn --> for j = 1:mk --> Res(i,j) = filonquad('cos',kk(j),x0,xn,nn(i),f); --> end; -->end; -->Res Res = ! - .0939334 .0268378 .0269223 - .0357034 .0095905 ! ! - .0939411 .0270685 .0264442 - .0350895 .0090338 ! ! - .0939457 .0271117 .0263818 - .0350622 .0090964 ! ! - .0939472 .0271238 .0263666 - .0350587 .0091129 ! ! - .0939479 .0271282 .0263614 - .0350579 .0091186 ! ! - .0939482 .0271300 .0263592 - .0350577 .0091209 ! ! - .0939483 .0271310 .0263582 - .0350576 .0091220 ! ! - .0939484 .0271314 .0263577 - .0350576 .0091226 ! ! - .0939484 .0271317 .0263574 - .0350575 .0091229 !

OOrrtthhooggoonnaall ffuunnccttiioonnss aanndd sseerriieess eexxppaannssiioonnss The concept of orthogonal functions apply to the case of functions of an independent variable x often defined in terms of an index n over a certain interval, for example, gn(x) = sin(nπx) on the interval (0,1), with n = 1, 2, 3, … The functions gn(x) and gm(x) are said to be orthogonal on an interval a < x < b, if

for n ≠ m.

,0)()(),( =⋅⋅= ∫b

a mnmn dxxgxggg

Page 28: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

28 © 2001 – Gilberto E. Urroz All rights reserved

If n = m, then the resulting integral represents the square of a quantity, ||gn||, known as the

norm of gn(x): Example 1 -- For the functions gn(x) = sin(nπx) on the interval (0,1), you can check that the integral

evaluates to

This latter expression is equal to zero for n ≠ m (n and m integers), and it is not defined if m = n. For the case m = n, we can calculate the value of the integral by using:

∫1

0

2 )(sin dxxnπ

which evaluates to

If n is an integer, this result evaluates to ½. To verify the previous integrals for a few combinations of integer values of n and m we can use the following SCILAB commands. First, we define a function f1(x) corresponding to the integrand sin(nπx)⋅sin(mπx): -->deff('[ff]=f1(x)','ff=sin(n.*%pi.*x).*sin(m.*%pi.*x)') Next, we calculate the integrals of f1(x) for combinations of values of n and m, verifying that the integrals are zero (i.e., nearly zero, for numerical integrals) for n ≠ m, and equal to ½ for n = m: -->m=2;n=5;intg(0,1,f1,0.01) ans = 8.951E-17 -->m=3;n=2;intg(0,1,f1,0.01) ans = 4.339E-18 -->m=3;n=3;intg(0,1,f1,0.01)

.0])([),(|||| 22 ≠⋅== ∫ dxxggggb

a nnnn

d⌠⌡

0

1( )sin n π x ( )sin m π x x

−− + n ( )cos n π ( )sin m π m ( )sin n π ( )cos m π

π ( )− + n m ( ) + n m

12

− + ( )cos n π ( )sin n π n πn π

Page 29: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

29 © 2001 – Gilberto E. Urroz All rights reserved

ans = .5 -->m=4;n=4;intg(0,1,f1,0.01) ans = .5

Series expansions with orthogonal functions If a function f(x) can be written as a series expansion of orthogonal functions gn(x), i.e.,

over the interval a < x < b, then we can use the property of orthogonality to obtain the coefficients Cn of the series as follows:

Multiply the function by gm(x), i.e.,

Integrate the resulting expression over the interval a < x < b, i.e.,

Because all integrals in the summation in the right-hand side of the expression above are zero, except for m = n, the expression simplifies to:

The coefficient Cn is calculated from the expression above by replacing m with n, i.e.,

Example 2 -- For the orthogonal functions gn(x) = sin(nπx), 0<x<1, the corresponding series is a Fourier sine series, i.e.,

∑∞

=

⋅=1

),()(n

nn xgCxf

∑∞

=

⋅⋅=⋅1

).()()()(n

mnnm xgxgCxgxf

∑ ∫∫∞

=

⋅⋅⋅=⋅⋅1

.)()()()(n

b

a mnn

b

a m dxxgxgCdxxgxf

∫∫ ⋅⋅=⋅⋅b

a mm

b

a m dxxgCdxxgxf 2)]([)()(

⋅=⋅

⋅⋅=∫∫

||||),(

)]([

)()(2 n

nb

a n

b

a nn g

gf

dxxg

dxxgxfC

Page 30: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

30 © 2001 – Gilberto E. Urroz All rights reserved

and,

Suppose that we want to find a few coefficients for the series expansion of the function f(x) = x2 for the orthogonal functions gn(x) = sin(nπx), 0<x<1, we can use the following SCILAB commands. First, we define the function f(x): -->deff('[y]=f(x)','y=x^2') Next, we define a function representing the integrand in the coefficient: -->deff('[gg]=g(x)','gg=f(x)*sin(n*%pi*x)') Finally, we define a function C(n) representing the coefficients Cn: -->deff('[CC]=C(n)','CC=2*intg(0,1,g,0.01)') The following vector represents the first five coefficients of the series: -->[C(1) C(2) C(3) C(4) C(5)] ans = ! .3786075 - .3183099 .2026506 - .1591549 .1252599 !

Orthonormal functions A set of normal functions φn(x) on an interval a < x < b is said to be orthonormal if

Thus, the norm of an orthonormal function is ||φn|| = 1.0. Given a set of orthogonal functions gn(x) on an interval a<x<b, you can generate the corresponding orthonormal set as

φn(x) = gn(x)/||gn||. Example 1 -- Determine the orthonormal functions corresponding to the set of orthogonal functions

gn(x) = cos(nπx), on the interval 0 < x < 1. First, to verify that the set of functions given is orthogonal, check that the integral

∑∞

=

⋅=1

),sin()(n

n xnCxf π

∫∫

∫∫ ⋅⋅⋅=

⋅⋅=

⋅⋅=

1

0

1

01

0

2

1

0 .)sin()(22/1

)sin()(

)(sin

)sin()(dxxnxf

dxxnxf

dxxn

dxxnxfCn π

π

π

π

=≠

=⋅⋅=b

a mnmn nmwhennmwhen

dxxgxg,1,0

)()(),( φφ

d⌠⌡

0

1( )cos n π x ( )cos m π x x

Page 31: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

31 © 2001 – Gilberto E. Urroz All rights reserved

evaluates to zero for m ≠ n. Therefore, the functions gn(x) are orthogonal. The norm of the function gn(x) is calculated by using

You can check that the latter integral evaluates to ½. Thus, ||gn||2 = ½, and ||gn|| = 1/√2. The orthonormal functions will be defined, therefore, as

φn(x) = gn(x)/||gn|| = √2⋅cos(nπx).

Orthogonality with respect to a weight function Some functions are orthogonal with respect to a weight function p(x), so that

In this case, the norm squared of gn(x) is given by

If a function f(x) can be expanded in terms of the functions gn(x), i.e.,

the coefficients of the series are given, in this case, by

.0)()()( =⋅⋅⋅∫b

a mn dxxgxgxp

.0])([)(|||| 22 ≠⋅⋅= ∫ dxxgxpgb

a nn

⋅⋅⋅⋅⋅=⋅⋅

⋅⋅⋅= ∫∫∫ b

a nn

b

a n

b

a nn dxxgxfxp

gdxxgxp

dxxgxfxpC )()()(

||||1

)]([)(

)()()(2

∑∞

=

⋅=1

),()(n

nn xgCxf

d⌠

⌡0

1

( )cos n π x 2 x

Page 32: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

32 © 2001 – Gilberto E. Urroz All rights reserved

Example 1 -- The Chebyshev polynomials T1(x) = x, T2(x) = 2x2-1, on the interval –1 < x < 1, are orthogonal with respect to the weight function p(x) = (1-x2)-1/2, which can be demonstrated by calculating the integral

You can check that the integral evaluates to zero by using SCILAB as follows. First, we define a function ff(x) representing the integrand in the previous integral. -->deff('[y] = f(x)','y = x.*(2.*x.^2-1)./sqrt(1-x.^2)') Next, we generate points in the interval (-1,1), not including the extremes of the interval, i.e., x = -1 and x = 1, where the integrand goes to infinity. -->xx=[-0.99:0.01:0.99]; yy = f(xx); A plot of the integrand is produced by: -->plot(xx,yy);xtitle('integrand for Chebyshev','x','integrand')

With the vectors xx and yy generated before, we can calculate the integral for the Chebyshev’s polynomials shown earlier by using function inttrap. The result, as expected, is zero: -->inttrap(xx,yy) ans =m- 9.021E-17

The Sturm-Liouville problem

d

-1

1

x ( ) − 2 x2 1

− 1 x2x

Page 33: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

33 © 2001 – Gilberto E. Urroz All rights reserved

Although the details of the proof are not included in this book, you can verify that Bessel functions, Laguerre polynomials, Hermite polynomials, and Chebyshev (or Tchebycheff) polynomials are all sets of orthogonal functions. In fact, all of these functions can be obtained from solving the so-called Sturm-Liouville problem defined by the differential equation

to be solved in the interval a < x < b, and subject to the boundary conditions

α1⋅y(a) + β1⋅y’(a) = 0, and

α2⋅y(b) + β2⋅y’(b) = 0.

By selecting the values of the functions r(x), p(x), and q(x), as well as the constants α1, α2, β1, and β2, we can generate, out of the general ODE shown above, the Bessel equation, Laguerre’s equation, etc. The solutions to the Sturm-Liouville ODE are given in terms of different values of the parameter λ, known as the eigenvalues of the problem, while the solutions themselves are known as the eigenfunctions.

FFoouurriieerr AAnnaallyyssiiss Fourier analysis involves the expansion of periodic functions into infinite series of sine and cosines. In general, any continuous function can be expanded into a series of sine functions (Fourier sine series), cosine functions (Fourier cosine functions), or a series combining sine and cosine functions (Fourier series). Expansion of functions into Fourier series finds applications in areas related to any type of wave propagation, be it electromagnetic waves or surface waves in waters. In practice, we evaluate Fourier series using only a finite number of sine and/or cosine functions representing a discrete number of frequencies. Fourier analysis can be extended to a continuous frequency domain through the use of Fourier transforms. Fourier transforms have applications in the analysis of periodic and non-periodic signals such as communication signals, turbulence velocity variations, etc. Most measured signals include a finite number of (typically) equally spaced data points suitable for analysis through discrete Fourier transforms. Sine and cosine functions belong to a family of so-called orthogonal functions. Properties of such functions are presented in the following section.

Fourier series Fourier series are series involving sine and cosine functions that are typically used to expand periodic functions. A function f(x) is said to be periodic of period L if f(x+L) = f(x). For example, because sin(x+2 π ) = sin x, and cos(x+2 π ) = cos x, the functions sin and cos are 2 π -periodic functions. If two functions f(x) and g(x) are periodic of period L, then their linear combination h(x) = af(x) + bg(x), where a and b are constant, is also periodic of period L.

The figure below shows a periodic function of period L = 2:

,0)]()([)( =⋅++

⋅ yxpxq

dxdyxr

dxd λ

Page 34: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

34 © 2001 – Gilberto E. Urroz All rights reserved

-->deff('[y]=f(x)','y=2.*sin(2.*%pi.*x)-3.*cos(%pi.*x)') -->x = [0:%pi/100;2*%pi]; y = f(x); -->plot(x,y,'x','y','Example of a periodic function')

To verify that the function is indeed periodic, we try the following evaluations: -->f(0),f(2),f(4) ans = - 3. ans = - 3. ans = - 3.

Fourier sine series Fourier sine series are such that,

= ( )f x ∑ = n 1

bn

sin

n π xL

Because the sine functions

sin

i π xL and

sin

j π xL are orthogonal, i.e.,

= d⌠

0

L

sin

i π xL

sin

j π xL x 0 , for ≠ i j ,

then

= d⌠

0

L

( )f x

sin

n π xL

x bn d⌠

0

L

sin

n π xL

2

x = L bn

2 ,

from which it follows that

Page 35: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

35 © 2001 – Gilberto E. Urroz All rights reserved

bn = 2L d

0

L

( )f x

sin

n π xL x

for n = 1, 2, 3,.... Consider the function = ( )f x − x x2 . This function is obviously not periodic, as shown in the figure below. -->deff('[y]=f(x)','y=x-x^2') -->x=[-2:0.1:3];y=f(x); -->plot(x,y,'x','y','y=x-x^2')

However, we can assume that it is periodic with period, say, L = 2, for the purpose of expanding the function in a Fourier sine series. The next SCILAB commands are used to calculate a Fourier sine series for the function. Notice that we define two functions, first a function g(x) which represents the integrand f(x)sin(nπx/L), and then a function b(n) that calculates the coefficient bn for the Fourier sine series that approximates f(x). -->deff('[w]=g(x)','w=f(x)*sin(n*%pi*x/L)') -->deff('[bb]=b(n)','bb=(2/L)*int(0,L,g)') We use the following for…end loop to generate the first 20 coefficients for the Fourier expansion (i.e., n = 0, 1, 2, …, 19). -->c = []; for j=1:20, c = [c b(j-1)]; end; -->c c = column 1 to 6 ! 0. - .2411904 .6366198 - .3861891 .3183099 - .2463915 ! column 7 to 11

Page 36: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

36 © 2001 – Gilberto E. Urroz All rights reserved

! .2122066 - .1788825 .1591549 - .1400554 .1273240 ! column 12 to 16 ! - .1149737 .1061033 - .0974717 .0909457 - .0845768 ! column 17 to 20 ! .0795775 - .0746864 .0707355 - .0668621 ! To calculate the values for the series expansion we will use the following function fouriersine. function [y] = fouriersine(c,x,L) //Calculates the Fourier sine series given //a vector of coefficients c and a vector //of values of x. It returns a vector of //values of y representing the Fourier series. //L is the period of the series. m = length(c); n = length(x); y = []; for i = 1:n yy = 0; for j = 1:m yy = yy + c(j)*sin((j-1)*%pi*x(i)/L); end; y = [y yy]; end; //end of function fouriersine A call to fouriersine for L = 2 is used to produce the following plot. -->getf('fouriersine') -->x=[-2:0.01:3];y=f(x);yf=fouriersine(c,x,L); -->plot2d([x' x'],[y' yf'],[1,-1],'011',' ',[-2 -6 3 6]) -->xtitle('Fourier sine series for y = x-x^2','x','y')

Page 37: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

37 © 2001 – Gilberto E. Urroz All rights reserved

The plot shows the original function (continuous line) and the approximation through 20 terms of the Fourier sine series (crosses). The approximation is somewhat acceptable in the range 0 < x < L, which corresponds to the artificial period L = 2 that we imposed on the Fourier series. Still, the sinusoidal character of the fitting is impossible to eliminate, at least with the 20 components shown. Notice that the Fourier sine series fitting makes the resulting function periodic of period L = 2, as expected. Also, notice that at x = 2, the fitted function shows some wild oscillations. This latter effect is known as the Gibbs phenomenon and it is characteristic of Fourier series. We will see more examples of this phenomenon in other applications in this chapter. The following graph represents the Fourier series for the function y = x-x2 using 40 components in the series. Obviously, the fitting is much better than when using 20 components only. The Gibbs phenomenon is still present at x = 2, but up to that value the Fourier sine series fitting is excellent for this case.

The following SCILAB commands are used to produce graphs of the original function as well as the Fourier sine series fitting corresponding to 5, 10, 20, 30, and 40 components. -->x = [-2:0.01:3]; y = f(x); -->c5=c(1:5); yf5 = fouriersine(c5,x,L); -->c10=c(1:10); yf10 = fouriersine(c10,x,L); -->c20=c(1:20); yf20 = fouriersine(c20,x,L); -->c30=c(1:30); yf30 = fouriersine(c30,x,L); -->c40=c(1:40); yf40 = fouriersine(c40,x,L); -->plot2d([x' x' x' x' x' x'],[y' yf5' yf10' yf20' yf30' yf40'],[1:1:6],'011',' ',[-1.5 -2.5 2.5 2.5]) -->xtitle('Fourier sine series using 5,10,20,30,40 components','x','y')

Page 38: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

38 © 2001 – Gilberto E. Urroz All rights reserved

Fourier cosine series Fourier cosine series are such that,

= ( )f x ∑ = n 0

an

cos

n π xL

.

Because the cosine function

cos

i π xL and

cos

j π xL are also orthogonal, i.e.,

= d⌠

0

L

cos

i π xL

cos

j π xL x 0 , for ≠ i j ,

then

= d⌠

0

L

( )f x

cos

n π xL

x an d⌠

0

L

sin

n π xL

2

x = L an

2 ,

from which it follows that

an = 2L d

0

L

( )f x

cos

n π xL x

for n = 1, 2, 3, ... For n = 0, the corresponding coefficient is:

a0 = 1L

d⌠⌡0

L

( )f x x .

As an example, to determine the coefficient of the cosine series that fits the function f(x) = (1-x)(x+1)(x+2) assuming that it is periodic with period L = 1, use the following SCILAB commands: -->deff('[y]=f(x)','y=(1-x).*(x+1).*(x-2)')

Page 39: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

39 © 2001 – Gilberto E. Urroz All rights reserved

-->deff('[y]=g(x)','y=f(x)*cos(n*%pi*x/L)') -->deff('[aa]=a(n)',... ['if n==0 then';'aa=intg(0,L,f)/L';'else';'aa=(2/L)*intg(0,L,g)/L';'end']) -->c=[];for j=1:40, c=[c a(j-1)]; end; -->c c = column 1 to 5 ! - 1.0833333 - .8543107 .0506606 - .0705892 .0126651 ! column 6 to 10 ! - .0247113 .0056290 - .0125093 .0031663 - .0075428 ! column 11 to 15 ! .0020264 - .0050410 .0014072 - .0036058 .0010339 ! column 16 to 20 ! - .0027068 .0007916 - .0021065 .0006254 - .0016859 ! column 21 to 25 ! .0005066 - .0013798 .0004187 - .0011501 .0003518 ! column 26 to 30 ! - .0009733 .0002998 - .0008344 .0002585 - .0007232 ! column 31 to 35 ! .0002252 - .0006329 .0001979 - .0005585 .0001753 ! column 36 to 40 ! - .0004964 .0001564 - .0004442 .0001403 - .0003998 ! Function fouriercosine, listed below, is used to calculate the Fourier series given the coefficients c, a vector of values of x, and the period of the function L. function [y] = fouriercosine(c,x,L) //Calculates the Fourier cosine series given //a vector of coefficients c and a vector //of values of x. It returns a vector of //values of y representing the Fourier series. //L is the period of the series. m = length(c); n = length(x); y = []; for i = 1:n

Page 40: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

40 © 2001 – Gilberto E. Urroz All rights reserved

yy = c(1); for j = 2:m yy = yy + c(j)*cos((j-1)*%pi*x(i)/L); end; y = [y yy]; end; //end of function fouriercosine A plot of the original function and the Fourier cosine series for 5, 10, 20, 30, and 40 components are shown in the following plot. The graph shows the original function and the different Fourier series to be practically indistinguishable from each other in the range 0 < x < 1. -->x = [0:0.01:1]; y = f(x); -->c5=c(1:5);yf5=fouriercosine(c5,x,L); -->c10=c(1:10);yf10=fouriercosine(c10,x,L); -->c20=c(1:20);yf20=fouriercosine(c20,x,L); -->c30=c(1:30);yf30=fouriercosine(c30,x,L); -->c40=c(1:40);yf40=fouriercosine(c40,x,L); -->plot2d([x' x' x' x' x' x'],[y' yf5' yf10' yf20' yf30' yf40'],... -->[1,2,3,4,5,6],'011',' ',rect) -->xtitle('Fourier cosine series - 5,10,20,30,40 components','x','y')

The following figure shows the original function (continuous line) and the Fourier cosine series (crosses) with 40 components in the range -0.5 < x < 1.5. It is obvious that the Fourier cosine series fits the original function very well in the range 0 < x < 1. Outside of that range, the function shows periodicity. -->x = -0.5:0.1:1.5; y = f(x); -->c40=c(1:40);yf40=fouriercosine(c40,x,L); -->plot2d([x' x'],[y' yf40'],[1,-1],'011',' ',[-0.5 -2.5 1.5 0.5]) -->xtitle('Fourier cosine series - 40 components','x','y')

Page 41: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

41 © 2001 – Gilberto E. Urroz All rights reserved

_________________________________________________________________________________ NOTE: If you have a function that is defined in the interval (-L/2, L/2), where L is the period of the function, the formulas to use for the coefficients of the cosine and sine functions are:

a0 = 1L d⌠

⌡−

L2

L2

( )f x x , an = 2L d

L2

L2

( )f x

cos

2 n π xL

x ,

and

bn = 2L d

L2

L2

( )f x

sin

2 n π xL

x

for n = 1, 2, 3, ... The formulas for the series themselves are:

= ( )f x + a0

= n 1

an

cos

2 n π xL and = ( )f x ∑

= n 1

bn

sin

2 n π xL .

____________________________________________________________________________ For example, a sine series fitting of the function f(x) = x3 in the interval [-1,1] (i.e., L=2), is shown below. The following SCILAB commands are used to define the function y = x3, and the functions necessary to calculate the coefficients of the corresponding Fourier sine series expansion: -->deff('[y]=f(x)','y=x^3') -->deff('[y]=g(x)','y=f(x)*sin(2*n*%pi*x/L)') -->deff('[bb]=b(n)','bb=(2/L)*intg(-L/2,L/2,g)') Next, we calculate 20 coefficients of the Fourier sine series:

Page 42: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

42 © 2001 – Gilberto E. Urroz All rights reserved

-->L = 2; c=[];for j=1:20, c=[c b(j)]; end; The function fouriersine2 is a modified version of fouriersine to account for the case in which the function is fitted in the interval [-L/2,L/2]: function [y] = fouriersine2(c,x,L) //Calculates the Fourier sine series given //a vector of coefficients c and a vector //of values of x. It returns a vector of //values of y representing the Fourier series. //L is the period of the series. m = length(c); n = length(x); y = []; for i = 1:n yy = 0; for j = 1:m yy = yy + c(j)*sin(2*j*%pi*x(i)/L); end; y = [y yy]; end; //end of function fouriersine2 The next commands produce a plot of the original function, y = x3, and of the Fourier sine series: -->x=[-L:0.01:L]; y = f(x); yf = fouriersine2(c,x,L); -->plot2d([x' x'],[y' yf'],[1,-1],'011',' ',[-2 -5 2 5]) -->xtitle('Fourier sine series y = x^3, 20 components','x','y')

The figure shows that the fitting is excellent except near the extremes of the interval, i.e., near x = -L/2 and near x = L/2.

Page 43: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

43 © 2001 – Gilberto E. Urroz All rights reserved

The next example shows a cosine series fitting in the interval [-1,1], L = 2, for the function f(x) = − 1 x2 . First, we define functions f(x) and g(x) and the generic coefficient of the Fourier cosine series an = a(n). Next, we calculate the first 40 coefficients in the series. -->deff('[y]=f(x)','y=1-x^2') -->deff('[y]=g(x)','y=f(x)*cos(2*n*%pi*x/L)') -->deff('[aa]=a(n)'... -->,['if n==0 then';'aa=intg(-L/2,L/2,f)/L';'else';... -->'aa=(2/L)*intg(-L/2,L/2,g)';'end']) -->L=2; c=[]; for j = 1:40, c = [c a(j-1)]; end; Function fouriercosine2 is used to calculate the Fourier cosine series: function [y] = fouriercosine2(c,x,L) //Calculates the Fourier cosine series given //a vector of coefficients c and a vector //of values of x. It returns a vector of //values of y representing the Fourier series. //L is the period of the series. m = length(c); n = length(x); y = []; for i = 1:n yy = c(1); for j = 2:m yy = yy + c(j)*cos(2*(j-1)*%pi*x(i)/L); end; y = [y yy]; end; //end of function fouriercosine2 The following plot shows the original function and the corresponding Fourier cosine series: -->c5 = c(1:5) ; y5 = fouriercosine2(c5 ,x,L); -->c10 = c(1:10); y10 = fouriercosine2(c10,x,L); -->c20 = c(1:20); y20 = fouriercosine2(c20,x,L); -->c30 = c(1:30); y30 = fouriercosine2(c30,x,L); -->c40 = c(1:40); y40 = fouriercosine2(c40,x,L); -->plot2d([x',x',x',x',x',x'],[y',y5',y10',y20',y30',y40'],... -->[1:1:6],'011',' ',[-1.5 -0.2 1.5 1.1]) -->xtitle('Fourier cosine series y = 1-x^2','x','y')

Page 44: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

44 © 2001 – Gilberto E. Urroz All rights reserved

As in the case of the previous example for the sine series, the fitting of this cosine series is excellent throughout the whole interval [-L/2,L/2] except very close to the extremes of the intervals. The Gibbs phenomenon is not evident in this case. ______________________________________________________________________________ The last two examples used a series expansion in the interval [-L/2,L/2]. Alternatively, you could use as interval of integration [-L,L], i.e., an interval encompassing two periods of the function, to obtain

= a01L d⌠

⌡−L

L

( )f x x , = an1L d

−L

L

( )f x

cos

n π xL x , and = bn

1L d

−L

L

( )f x

sin

n π xL x ,

for n = 1,2,3, ... The sine and cosine series will be calculated as:

= ( )f x + a0

2

= n 1

an

cos

n π xL

and = ( )f x ∑ = n 1

bn

sin

n π xL .

_______________________________________________________________________________ For example, the function f(x) = tan(x) can be expanded as sine series using the period L = 1 as follows. First, we define the functions that are used to produce the coefficients of the Fourier sine series, followed by the calculation of the first 20 coefficients (including a0): -->deff('[y]=f(x)','y=tan(x)') -->deff('[y]=g(x)','y=f(x)*sin(n*%pi*x/L)') -->deff('[bb]=b(n)','bb=(1/L)*intg(-L,L,g)') -->L=1; c=[]; for j = 1:20, c = [c b(j-1)]; end; Next, we produce a plot of the original function and the Fourier sine series for 5, 10, and 20 components: -->x=[-2.5:0.01:2.5];y=f(x); -->c5 = c(1:5); y5 = fouriersine(c5 ,x,L); -->c10= c(1:10);y10 = fouriersine(c10,x,L); -->c20= c(1:20);y20 = fouriersine(c20,x,L); -->plot2d([x' x' x' x'],[y' y5' y10' y20'],[-1,1,2,3],'011',' ',[-1.5 -2 1.5 2]) -->xtitle('Fourier sine series for y = tan(x), -L<x<L, L = 2','x','y')

Page 45: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

45 © 2001 – Gilberto E. Urroz All rights reserved

In the following example, we use a cosine series to fit the function f(x) = (1-x2)1/2 in the interval [-1,1], i.e., L = 1. -->deff('[y]=f(x)','y=sqrt(1-x^2)') -->deff('[y]=g(x)','y=f(x)*cos(n*%pi*x/L)') -->deff('[aa]=a(n)',... -->['if n==0 then';'aa=intg(-L,L,f)/(2*L)';'else';... --> 'aa=intg(-L,L,g)/L';'end']) -->L=1; c=[]; for j=1:20, c=[c a(j-1)]; end; -->x=[-1:0.01:1];y=f(x);yf=fouriercosine(c,x,L); -->plot2d([x',x'],[y',yf'],[1,-1]) -->xtitle('Fourier cosine for y = sqrt(1-x^2), 20 components','x','y')

Fourier Series

Page 46: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

46 © 2001 – Gilberto E. Urroz All rights reserved

A Fourier series is simply a series expansion of a function that includes both sine and cosine series. The general form of the Fourier series expansion in the interval [-L/2,L/2], i.e., assuming a period L, is:

= ( )f xa0

2 + ∑ = n 1

+ an

cos

2 π n xL bn

sin

2 π n xL

with the coefficients calculated as:

a0 = 2L d⌠

⌡−

L2

L2

( )f x x , an = 2L d

L2

L2

( )f x

cos

2 n π xL

x ,

and

bn = 2L d

L2

L2

( )f x

sin

2 n π xL

x

If the function is periodic of period L, the coefficients an and bn are calculated as

a0 = 2L

d⌠⌡c0

+ c0 L

( )f x x , an = 2L d

c0

+ c0 L

( )f x

cos

2 n π xL x ,

and

bn = 2L d

c0

+ c0 L

( )f x

sin

2 n π xL x

.

Thus, if c0 = -L/2, we recover the first set of equations for an and bn. A user-defined SCILAB function to calculate Fourier series The following function, fourierseries, returns the coefficients of the Fourier series and the evaluation of the same on a range of values of x for different number of components. The general call to the function is

Page 47: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

47 © 2001 – Gilberto E. Urroz All rights reserved

[a0 a b y] = fourierseries(nn, c0, L, x, f, tol) where nn is a vector containing values of the components to be used, e.g., nn = [5, 10, 20], L is the period of the function, x is a vector indicating the values of x where the Fourier series will be evaluated, and tol is a tolerance for convergence used in function intg (see --> help intg). The integrals defining an and bn are calculated in the interval [c0,c0+L]. The function returns values a0, a, b, and y, where a0 is the coefficient a0 in the series, a and b are vectors containing the coefficients of the cosine and sine series, respectively, i.e., an and bn for n=1,2,…, and y is a matrix with the same number of columns as in vector x and the number of rows equal to the number of elements in vector nn. Thus, the i-th row of matrix y contains the values of the Fourier series with the number of components indicated by the i-th component of nn. A listing of function fourierseries follows. The function fourierseries includes a sub-function, fseries, which is called recurrently to produce the rows of matrix y. function [a0,a,b,y] = fourierseries(n,c0,L,x,f,tol) //Calculates coefficients and values for the //Fourier series corresponding to function f //for n1, n2, ..., nm components, where n is //the vector n = [n1,n2,...,nm] of length m. //Integrals are calculated using the interval //[c0,c0+L] where L is the period of the function. //e.g., if c0 = -L/2, then the interval is [-L/2,L/2], //x is a vector of length k for calculating //values of the Fourier series. //a0 = first coefficient for cosine term //a = coefficients of the cosine terms [a1 a2 ...] //b = coefficients of the sine terms [b1 b2 ...] //y = matrix of m rows deff('[gg1]=g1(xi)','gg1=f(xi)*cos(2*nn*%pi*xi/L)'); deff('[gg2]=g2(xi)','gg2=f(xi)*sin(2*nn*%pi*xi/L)'); deff('[aaa]=a1(nn)','aaa=(2/L)*intg(c0,c0+L,g1,tol)'); deff('[bbb]=b1(nn)','bbb=(2/L)*intg(c0,c0+L,g2,tol)'); a0 = (2/L)*intg(c0,c0+L,f,tol); nmax = max(n);a = []; b = []; for j = 1:nmax a = [a a1(j)]; b = [b b1(j)]; end; m = length(n); k = length(x);y = zeros(m,k); for j =1:m aj = a(1:n(j)); bj = b(1:n(j)); y(j,:) = fseries(a0,aj,bj,x,L); end; //end function fourierseries function [yy] = fseries(a0,aa,bb,xx,L) //Calculates fourier series given the

Page 48: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

48 © 2001 – Gilberto E. Urroz All rights reserved

//coefficients of the cosine series //a0 and vector a (of length nn), and //the coefficients of the sine series, //vector b (of length nn). //L = period of function to be fit. nn = length(aa); mm = length(xx);yy = zeros(1,mm); for j = 1:mm yy = a0/2; for k = 1:nn yy = yy + aa(k)*cos(2*k*%pi*x/L) + bb(k)*sin(2*k*%pi*x/L); end; end; Applications of function fourierseries Example 1 - The following example shows the fitting of the function = ( )f x + 1 x2 in the interval [-1,1] using function fourierseries with the option itype = ‘right’. The Fourier series are calculated for 5, 10, 20, and 40 components. A plot of the original figure and of the Fourier series with different number of components is also produced. -->deff('[y]=f(x)','y=sqrt(1+x^2)') -->getf('fourierseries') -->L = 2; x=[-1:0.01:1]; y = f(x); -->[a0,a,b,yy]=fourierseries('right',[5,10,20,40],-L/2,L,x,f,1e-5); -->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)']) -->xtitle('Fourier series for y = sqrt(1+x^2) with 5,10,20,40 components',... -->'x','y')

Example 2 - To fit the function f(x) = ln(x+2) in the interval [-1,1], i.e., L = 2, using Fourier series, we try: -->deff('[y]=f(x)','y=log(x+2)') -->getf('fourierseries') -->L= 2; x=-1:0.01:1; y = f(x); -->[a0,a,b,yy]=fourierseries([5,10,20,40],-L/2,L,x,f,1e-5); -->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])

Page 49: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

49 © 2001 – Gilberto E. Urroz All rights reserved

-->xtitle('Fourier series for y = ln(x+2) with 5,10,20,40 components','x','y')

The fitting of the Fourier series is excellent except near the extremes of the interval where strong Gibbs phenomenon deviations are observed. Also, the fitting improves as the number of components in the Fourier series increases. Notes: [1] Even if a function is not periodic, you can fit a certain section of the function (say, between x = 0 and x = L, or between -L/2 and L/2) using Fourier series. Of course, the resulting trigonometric series will be so that it represents a periodic function of period L. [2] For any given function you can fit sine, cosine, or full Fourier series. It is recommended, however, that you plot the original as well as the resulting expansion(s) to determine how well the Fourier series expansion(s) works. Example 3 - In the next example we produce the Fourier series for the function g(x) = exp(x), x in [0,π], i.e., with period L = π. -->deff('[y]=f(x)','y=exp(x)') -->L=%pi; c0 = 0; x=-%pi:%pi/100:%pi; y = f(x); -->getf('fourierseries') -->[a0,a,b,yy]=fourierseries([5,10,20,40],c0,L,x,f,1e-5); -->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)']) -->xtitle('Fourier series for y = exp(x) with 5,10,20,40 components','x','y')

Page 50: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

50 © 2001 – Gilberto E. Urroz All rights reserved

The Fourier series for this function does an excellent job fitting the curve except near the extremes of the interval where the Gibbs phenomenon is evident. The following graphs shows the Fourier series developed above in the interval [-2π,2 π] to demonstrate the fact that the Fourier series produces a periodic function even though the original function was not periodic. By calculating the Fourier series coefficients in the interval [0, π] we generates a periodic function of period π.

Fourier series for a piecewise periodic function The following example shows the fitting of the Fourier series for a continuous piece-wise function, h(x) = 3, -1/3 < x < 1/3, and h(x) = 0 elsewhere. We assume that the function h(x) represents a periodic function in [-1,1], with period L = 2. The solution is shown for 4, 6, 8, and 10 components. For more than 10 components, function fourierseries presents problems of convergence on function intg which is used to calculate the integrals in the series coefficients. -->deff('[y]=f(x)',['if x>-1/3 & x<1/3 then';'y=3';'else';'y=0';'end']) -->L=2; c0=-L/2; x=[-1:0.1:1]; -->y=[]; for j =1:length(x), y=[y f(x(j))]; end;

Page 51: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

51 © 2001 – Gilberto E. Urroz All rights reserved

-->getf('fourierseries') -->[a0,a,b,yy]=fourierseries([4,6,8,10],c0,L,x,f,1e-5); -->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)']) -->xtitle(Piecewise continuous function,'x','y')

The following graphs shows the original function (single square pulse) and the periodic function generated by the corresponding Fourier series using the interval -3 < x < 3. This result demonstrates how we can generate a square wave out of a single square pulse.

To be able to calculate coefficients for n>10, we can use the definitions for the coefficients an and bn corresponding to the function h(x) defined earlier. These coefficients are:

,3

sin6cos322cos)(2 3/1

3/1

2/

2/ ∫∫ −−

=⋅

⋅=⋅

⋅= π

πππ n

ndx

LXndx

Lxnxh

La

L

Ln

and

.0sin322sin)(2 3/1

3/1

2/

2/ ∫∫ −−=⋅

⋅=⋅

⋅= dx

LXndx

Lxnxh

Lb

L

Lnππ

Page 52: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

52 © 2001 – Gilberto E. Urroz All rights reserved

Also,

∫∫ −−=⋅=⋅=

3/1

3/1

2/

2/0 .0.2322)(2 dxdxxh

La

L

L

Using these definitions we can produce a vector of values for the coefficients an as follows: -->deff('[aa]=a(n)','aa=6*sin(n*%pi/3)/(n*%pi)') -->c = [1]; for j=1:40, c=[c a(j)]; end; Notice that the first term in the vector of coefficients, c, is equal to 1, i.e., a0/2. Next, we generate data from the original function (defined earlier): -->x=-1:0.01:1;y=f(x); To produce the Fourier series we use function fouriercosine3, which is a modified version of function fouriercosine, introduced earlier. function [y] = fouriercosine3(c,x,L) //Calculates the Fourier cosine series given //a vector of coefficients c and a vector //of values of x. It returns a vector of //values of y representing the Fourier series. //L is the period of the series. m = length(c); n = length(x);y = []; for i = 1:n yy = c(1); for j = 2:m yy = yy + c(j)*cos(2*(j-1)*%pi*x(i)/L); end; y = [y yy]; end; To calculate values for the Fourier series with 40 components of cosine we use: -->getf('fouriercosine3') -->yy = fouriercosine3(c,x,L); A plot of the original function and the corresponding Fourier series with 40 components is shown next: -->plot2d([x' x'],[y' yy'])

Page 53: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

53 © 2001 – Gilberto E. Urroz All rights reserved

-->xtitle('square wave with 40 components of Fourier series','x','y')

Calculating Fourier series coefficients with Filon’s sine-cosine formulae An alternative for calculating the coefficients of the Fourier series is to use function filonquad, defined earlier for calculating integrals of the form

∫∫ ⋅⋅ mm x

x

x

xdxkxxfdxkxxf

00

)cos()(,)sin()(

For Fourier series coefficients the following values apply: k=2π/L, x0 = c0, xn=c0+L, and the integrals are to be multiplied by (2/L). Care must exercised when using Filon’s integral formulas for the function f(x) used must be continuous in the full interval [x0,xn]. Filon’s integral formulae use a parameter m (referred to as n when the formulae where first introduced earlier in the Chapter) which must be an even number larger than or equal to 4. Thus, to calculate the coefficients of the cosine terms we will use

an = (2/L)*filonquad(‘cos’,2*n*%pi/L,x0,xn,m,f), while those for the sine terms are calculated with

bn = (2/L)*filonquad(‘sin’,2*n*%pi/L,x0,xn,m,f). For example, to calculate the first five coefficients of the Fourier series corresponding to f(x) = exp(-x), using Filon’s sine-cosine quadrature formulae for m = 4,6,8,…,30, we prepare the following table. Columns represents values of n = 1,2,3,4,5, and rows represent values of m = 4,6,8,…,30: -->deff('[y]=f(x)','y=exp(-x)');x0=-1;xn=1;L=2; -->kk=[1:1:5];mk=length(kk);nn=[4:2:40];mn=length(nn); -->ResCos = zeros(mn,mk); -->for i = 1:mn --> for j = 1:mk

Page 54: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

54 © 2001 – Gilberto E. Urroz All rights reserved

--> ResCos(i,j)=(2/L)*filonquad('cos',2*%pi*kk(j)/L,x0,xn,nn(i),f); --> end; -->end; -->ResCos ResCos = ! - .2201023 .0583263 - .0244558 .0145816 - .0088041 ! ! - .2169092 .0591023 - .0262183 .0141662 - .0093004 ! ! - .2164390 .0583263 - .0265777 .0148071 - .0092038 ! ! - .2163174 .0581622 - .0262908 .0149935 - .0094942 ! ! - .2162749 .0581098 - .0262183 .0148593 - .0096020 ! ! - .2162569 .0580887 - .0261921 .0148217 - .0095293 ! ! - .2162483 .0580789 - .0261806 .0148071 - .0095076 ! ! - .2162437 .0580738 - .0261749 .0148002 - .0094986 ! ! - .2162412 .0580709 - .0261717 .0147966 - .0094942 ! ! - .1933848 .0463011 - .0304248 .0353657 - .0405569 ! ! - .2162386 .0580681 - .0261687 .0147932 - .0094904 ! ! - .2162380 .0580674 - .0261680 .0147924 - .0094895 ! ! - .2162375 .0580669 - .0261675 .0147919 - .0094889 ! ! - .2162372 .0580666 - .0261671 .0147915 - .0094885 ! ! - .2162370 .0580664 - .0261669 .0147913 - .0094882 ! ! - .2162368 .0580662 - .0261667 .0147911 - .0094881 ! ! - .2162367 .0580661 - .0261665 .0147910 - .0094879 ! ! - .2162366 .0580660 - .0261664 .0147909 - .0094878 ! ! - .2162365 .0580659 - .0261664 .0147908 - .0094877 ! -->ResSin = zeros(mn,mk); -->for i = 1:mn --> for j = 1:mk --> ResSin(i,j)=(2/L)*filonquad('sin',2*%pi*kk(j)/L,x0,xn,nn(i),f); --> end; -->end; -->ResSin ResSin = ! - .6795196 .3740782 - .2468433 .1870391 - .1490822 ! ! - .6792471 .3650430 - .2493854 .1860151 - .1490260 ! ! - .6792918 .3648433 - .2467114 .1870391 - .1491070 ! ! - .6793105 .3648335 - .2466187 .1859172 - .1496313 ! ! - .6793183 .3648340 - .2466100 .1858691 - .1490599 ! ! - .6793218 .3648349 - .2466088 .1858634 - .1490322 ! ! - .6793236 .3648356 - .2466087 .1858623 - .1490285 ! ! - .6793245 .3648360 - .2466088 .1858620 - .1490276 ! ! - .6793251 .3648362 - .2466089 .1858620 - .1490273 ! ! - .6940133 .3906049 - .2762026 .2096025 - .1581488 ! ! - .6793256 .3648365 - .2466090 .1858620 - .1490272 ! ! - .6793258 .3648365 - .2466090 .1858620 - .1490272 ! ! - .6793259 .3648366 - .2466090 .1858620 - .1490272 ! ! - .6793260 .3648366 - .2466091 .1858620 - .1490272 ! ! - .6793260 .3648366 - .2466091 .1858621 - .1490273 ! ! - .6793260 .3648367 - .2466091 .1858621 - .1490273 ! ! - .6793261 .3648367 - .2466091 .1858621 - .1490273 ! ! - .6793261 .3648367 - .2466091 .1858621 - .1490273 ! ! - .6793261 .3648367 - .2466091 .1858621 - .1490273 !

Page 55: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

55 © 2001 – Gilberto E. Urroz All rights reserved

For the case of the piecewise continuous function that produces the single rectangular pulse, namely, h(x) = 3, -1/3 < x < 1/3, and h(x) = 0 elsewhere, we can use Filon’s sine-cosine quadrature formulae to calculate the coefficients for the Fourier series by using x0 = -1/3 and xn = 1/3, since the function is zero in the remaining ranges of the interval (-1,1). The period is still taken as L = 2. The following SCILAB commands will produce the Fourier series coefficients for the cosine and sine components for n = 1,2,3,4,5, and m = 4,6,8,…,30: -->deff('[y]=f(x)','y=3');x0=-1/3;xn=1/3;L=2; -->ResCos = zeros(mn,mk); -->for i = 1:mn --> for j = 1:mk --> ResCos(i,j)=(2/L)*filonquad('cos',2*%pi*kk(j)/L,x0,xn,nn(i),f); --> end; -->end; -->ResCos ResCos = ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.5838617 .819752 .0616369 - .3132454 - .2446393 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.618222 .8366987 .0487592 - .3600631 - .3111114 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.630336 .8377577 .0368149 - .3797570 - .3271122 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! ! 1.6331939 .8374929 .0333666 - .3843859 - .3298161 ! ! 1.6539867 .8269933 0. - .4134967 - .3307973 ! -->ResSin = zeros(mn,mk); -->for i = 1:mn --> for j = 1:mk --> ResSin(i,j)=(2/L)*filonquad('sin',2*%pi*kk(j)/L,x0,xn,nn(i),f); --> end; -->end; -->ResSin ResSin = ! 0. 0. 0. 0. 0. ! ! 0. 0. 0. 0. 0. ! ! - 8.651E-17 - 9.985E-17 - 1.471E-16 - 2.321E-17 3.258E-17 ! ! 2.935E-17 0. 0. - 6.795E-17 - 6.210E-17 ! ! - 1.480E-16 - 8.417E-17 - 9.729E-17 5.351E-17 9.501E-17 ! ! - .0650665 - .0966290 - .0772903 - .0151104 .0587416 ! ! 1.355E-16 1.230E-16 8.613E-17 - 6.320E-17 - 8.416E-17 ! ! 0. - 4.398E-17 - 3.772E-17 2.146E-17 0. ! ! 0. 1.383E-16 5.777E-17 - 3.214E-17 - 4.877E-17 !

Page 56: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

56 © 2001 – Gilberto E. Urroz All rights reserved

! - 2.871E-16 - 3.412E-16 - 1.219E-16 1.072E-16 1.111E-16 ! ! - .0426227 - .0550420 - .0281511 .0194482 .0540867 ! ! 2.354E-16 2.053E-16 6.136E-17 - 2.430E-16 - 7.683E-17 ! ! - 1.550E-16 - 9.129E-17 2.827E-17 1.233E-16 1.891E-16 ! ! - 2.369E-16 - 1.058E-16 9.830E-17 2.560E-16 2.373E-16 ! ! - 1.850E-16 - 1.850E-16 - 6.451E-17 1.243E-16 1.295E-16 ! ! - .0313185 - .0378327 - .0142622 .0208907 .0397693 ! ! 1.261E-16 1.643E-16 - 2.211E-17 - 1.201E-16 - 1.692E-16 ! ! - .0282896 - .0335690 - .0114548 .0201849 .0355957 ! ! 1.036E-16 0. 5.427E-17 0. - 1.184E-16 ! As found earlier, the coefficients for the sine components are all zero (nearly zero from the numerical results). The coefficients for the cosine components are very consistent for most values of m except for a few of them. For that reason it is a good idea to check the convergence of the Filon quadrature formulae for different values of m.

Use of Fourier series in ordinary differential equations Fourier series can be used to represent periodic functions within ordinary differential equations. Consider the case of an oscillating mass subject to damping ( β )and a linear restoring force (k):

= + + m

∂2

x2 ( )y x β

∂x ( )y x k ( )y x ( )F x

The function F(x) in the right-hand side of the equation represents the excitation or driving force, i.e., an external effect that activates or affects the oscillating system. If the function F(x) happens to be a periodic function, the expression for F(x) can be replaced by its Fourier series expansion G(x), before obtaining a solution to the ODE. As an example, let a trapezoidal driving function f(x) .

<<

−−

<<<<

=

elsewhere

xx

xxx

xf

,0

13/2,3231

3/23/1,13/10,3

)(

represent the excitation function in the solution of the differential equation for an oscillating mass. In the solution, we will use both the original pulse function f(x) and the Fourier series expansion of f(x), G(x), as illustrated below. Fourier series for a trapezoidal pulse To obtain the Fourier series of the trapezoidal pulse, first, we define function f(x): -->deff('[y]=f(x)',['if x>0 & x<1/3 then';'y=3*x';... --> 'elseif x>1/3 & x<2/3 then';'y=1';'elseif x>2/3 & x<1 then';...

Page 57: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

57 © 2001 – Gilberto E. Urroz All rights reserved

--> 'y=1-3*(x-2/3)';'else';'y=0';'end']) Next, we generate data for plotting the function in the range 0 < x < 1: -->x=0:0.01:1; -->y=[]; for j=1:length(x), y=[y f(x(j))]; end; The following statements generate the coefficients of a Fourier series with 10 components: -->L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5); Next, we produce a plot of the original function and the Fourier series: -->plot2d([x' x'],[y' yy'],[1,-1]) -->xtitle('Trapezoid function - Fourier series 20 components','x','y')

The graph shows that even if we use only 10 components for the Fourier series, the fitting for the trapezoidal function is excellent. Notice that in the definition of f(x) the function is zero outside of the range [0,1]. Thus, f(x) defines a single trapezoidal pulse as illustrated in the figure below in which we plot f(x) in the range -2 < x < 3: -->xx=[-2:0.01:3];y=[];for j=1:length(xx), y=[y f(xx(j))]; end; -->plot2d(xx,y,1,'011',' ',[-2 -0.1 3 1.1]) -->xtitle('Single trapezoidal pulse given by f(x)','x','y')

Page 58: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

58 © 2001 – Gilberto E. Urroz All rights reserved

Next, we calculate the Fourier series generated by using the trapezoidal pulse f(x) in the range 0 < x < 5 to demonstrate the periodicity of the Fourier series: -->x=0:0.01:5;L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5); -->plot(x,yy,'x','y','Trapezoidal periodic wave')

Solution to the damped oscillator equation with trapezoidal pulse and wave excitation Next we will solve the second-order differential equation using as driving signals the original trapezoidal function (a single trapezoidal pulse) and the corresponding Fourier series (a periodic trapezoidal wave). To solve the problem, we use the variables u1 = y(x) and u2 = dy/dx, to form the vector function u(x) = [u1;u2], and transform the equation into

du/dx = ff(x,u) = (F(x)-ku1-βu2)/m. In the latter expression we first replace the function F(x) with the original trapezoidal pulse f(x), and then with the Fourier series G(x). We will use as initial conditions u1(0) = u2(0) = 0 and solve the ODE in the range 0 < x < 20 with an increment ∆x = 0.1.

Page 59: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

59 © 2001 – Gilberto E. Urroz All rights reserved

Following we list the SCILAB commands for solving the ODE. The function f(x) describing the trapezoidal pulse was defined earlier. Also, the coefficients a0, a, and b of the Fourier series are available from the call to function fourierseries. The next step is to define a function G(x) that calculates the Fourier series: -->deff('[w]=G(x)',['n=length(a)';'w=a0/2';'for j=1:n';... --> 'w=w+a(j)*cos(2*j*%pi*x/L)+b(j)*sin(2*j*%pi*x/L)';'end']) Next, we define functions f1(x,u) and f2(x,u) representing the function ff(x,u), i.e., the right-hand side in the equation du/dx = ff(x,u). Function f1(x,u) involves the trapezoidal pulse function f(x) while function f2(x,u) involves the Fourier series function G(x) as indicated below: -->deff('[w]=f1(x,u)','w=[u(2),(f(x)-k*u(1)-beta*u(2))/m]') -->deff('[w]=f2(x,u)','w=[u(2),(G(x)-k*u(1)-beta*u(2))/m]') The next step is to define the parameters of the problem and of the solution: -->k=10;m=5;beta=1; -->x = [0:0.1:20]; x0 = 0; y0 = [0;0]; The solutions to the ODE for the two cases is calculated using function ode as follows: -->y1=ode(y0,x0,x,f1); -->y2=ode(y0,x0,x,f2); The following plot shows the solutions corresponding to the single trapezoidal pulse (continuous line) and to the periodic trapezoidal wave (circles). Notice that the signal corresponding to the single trapezoidal pulse starts at rest and reaches a maximum amplitude at the beginning, decaying in a sinusoidal manner afterwards. On the other hand, the signal due to the periodic trapezoidal wave, although, in general also decaying with x, shows additional secondary peaks that result from the repeated excitation represented by the periodic function represented by the Fourier series. -->plot2d([x' x'],[y1(1,:)' y2(2,:)'],[1,-9]) -->xtitle('Damped oscillator solution','x','y')

Page 60: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

60 © 2001 – Gilberto E. Urroz All rights reserved

Fourier series for other periodic shapes Thus, we have demonstrated how to use Fourier series to generate a periodic function representing almost any type of shape. All we need to get started is a single pulse with the basic shape that needs to be repeated. Thus, Fourier series can be used to produce periodic functions such as those illustrated in the figure below:

Even when using the same basic function, e.g., f(x) in the examples above, to generate a Fourier series, you can change the shape of the resulting periodic signal by changing the period of the function. In the example worked out above, we used a period L = 1.0, thus, the basic shape (the trapezoid) repeats one after another in the Fourier series. If we want to add some space between consecutive trapezoids we can change the period to L = 1.5. The following SCILAB commands show how to generate such signal. -->x=0:0.01:5;L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5); -->plot(x,yy,'x','y','Trapezoidal periodic wave')

Page 61: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

61 © 2001 – Gilberto E. Urroz All rights reserved

Solving the damped oscillator equation with a different periodic trapezoidal pulse With the signal shown above as the excitation to the system represented by the damped oscillator equation, the solution to the ODE is presented in the following plot: -->deff('[w]=G(x)',['n=length(a)';'w=a0/2';'for j=1:n';... --> 'w=w+a(j)*cos(2*j*%pi*x/L)+b(j)*sin(2*j*%pi*x/L)';'end']) -->deff('[w]=f2(x,u)','w=[u(2),(G(x)-k*u(1)-beta*u(2))/m]') -->k=10;m=5;beta=1;x=[0:0.1:20];x0=0;y0=[0;0]; -->y3=ode(y0,x0,x,f2); -->plot(x,y3(1,:),'Damped oscillator solution','x','y')

Compare the latter result with the previous solution.

FFoouurriieerr ttrraannssffoorrmmss

Page 62: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

62 © 2001 – Gilberto E. Urroz All rights reserved

In the previous solution, the variable x can be thought of as representing time, while y may represent the displacement of an oscillator. When writing out Fourier series for a function of time we would use

f(t) = a0/2 + Σ [ an cos(2nπt/T) + bn sin(2nπt/T), n = 1,2, …, where T is the period of the function, f0 = 1/T is the fundamental frequency, and ω0 = 2π/T = 2πf0 is the fundamental angular frequency. The Fourier series can also be written as

f(t) = a0/2 + Σ [ an cos(ωnt) + bn sin(ωnt)], where ωn = n⋅ωo, and

∫∫ ∫ −− −===

2/

2/

2/

2/

2/

2/0 ).sin()(2),cos()(2,)(2 T

T nn

T

T

T

T nn ttfT

bttfT

adttfT

a ϖϖ

Plots of the Fourier coefficients an and bn vs. ωn would be similar to the ones sketched below:

In the figure, ∆ω = 2π/T represents the increment in the discrete values of the frequency ωn. As the period of the signal, T, increases, the increment in frequency, ∆ω, becomes a differential of frequency, dω. Using the definitions of the coefficients shown above, the signal can be written as

.)sin()sin()(2)cos()cos()(22

)(1

2/

2/

2/

2/

0 ∑ ∫∫∞

=−−

+

+=

nn

T

T nn

T

T n dttttfT

tdtttfT

atf ϖϖϖϖ

Using 2/T = ∆ω/π, and replacing the summations with integrals as T ! ∞, the signal is now written as

Page 63: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

63 © 2001 – Gilberto E. Urroz All rights reserved

.)sin()sin()()cos()cos()(2

)(0

0 ∫ ∫∫∞ ∞

∞−

∞−

+

+= tdtttfdtdtttfdatf nnnn ϖϖ

πϖϖϖ

πϖ

If we define

∫ ∫∞

∞−

∞−== ,)sin()(

21)(,)cos()(

21)( dtttfBdtttfA ϖ

πϖϖ

πϖ

we can re-write the signal for an infinitely large period as

[ ] .)sin()()cos()(22

)(0

0 ∫∞

++= ϖϖϖϖϖ dtBtAatf

The terms A(ω) and B(ω) are components of a Fourier transform, and the signal f(t) results from an inverse Fourier transform. The Fourier transform of the signal f(t) is the term F(ω) = A(ω)-i⋅B(ω), where i is the unit imaginary number (i = √-1). Using Euler’s formula, e-iωt

= cos(ωt)-i⋅sin(ωt), the Fourier transform is written as

∫∞

∞−

−⋅= .)(21)( dtetfF tiω

πω

The signal f(t), i.e., the inverse Fourier transform is written as

∫∞

∞−⋅= .)()( ϖϖ ω deFtf ti

The latter result assumes that a0 = 0, which can be accomplished by subtracting the original value of ao from the original function.

Fourier analysis of discrete data Many a time, engineers and scientists are faced with the need of analyzing discrete data resulting from measurements of communication signals, turbulence velocity variations, water surface records, weather data, etc. We may be interested in determining, for example, what are the dominant frequencies present in the signal. Assume that the data is collected at regular intervals ∆t = T/n and consists of the n measurements {x0, x1,x2,…,xn-1} taken at times tr = r⋅∆t. An approximation to the Fourier transform F(ω) presented earlier, is the summation

1,...,1,0,11 1

0

)/2(1

0

))(/2( −==∆= ∑∑−

=

−−

=

∆⋅− nkexn

texT

Xn

r

nkrir

n

r

trTkirk

ππ .

This is known as the discrete Fourier transform (DFT). The inverse discrete Fourier transform (IDFT) provides the data values xr in terms of the coefficients Xk:

Page 64: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

64 © 2001 – Gilberto E. Urroz All rights reserved

.1,...,1,0,1

0

)/2( −==∑−

=nreXx

n

k

nkrikr

π

The coefficients Xk calculated with the discrete Fourier transform, namely, Xk = ak - ibk represent coefficients of a Fourier series from which the data values xr can be obtained. However, the coefficients Xk are only correct for frequencies below the frequency

ωk = 2πk/(n⋅∆t) = π/∆t, i.e., for k = 0, 1, 2, …, n/2. Also, if the signal contains frequencies above π/∆t, these frequencies introduce a distortion of the Xk -vs -ωk graph known as aliasing. If w0 is the maximum component of frequency contained in the signal x(t), aliasing is avoided by selecting ∆t so that π/∆t > ωo, or 1/(2⋅∆t) > f0. The value 1/(2⋅∆t) is known as the Nyquist frequency, which represents the maximum frequency that can be detected by sampling the data at intervals ∆t.

SCILAB functions for discrete Fourier transform (DFT) and fast Fourier transform (FFT) SCILAB provides functions dft and fft to calculate discrete Fourier transforms and fast Fourier transforms, respectively. The fast Fourier transform (FFT) is an algorithm that applies to discrete data that includes 2m elements (m = integer). FFT takes advantage of the fact that the size of the sample is a power of 2 to dramatically reduce the amount of computer operations in the calculation of the DFT that would otherwise be required. This approach thus provides economy of storage and operations when calculating DFTs. (For details on the algorithm for the fast Fourier transform, the reader is referred to Newland, D.E., 1993,”An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition,” Longman Scientific & Technical, Essex, England.) Scilab’s dft function Function dft applies to single data vectors. The general call to the function is

[xf]=dft(x,flag); where x is an input vector which may represent signal data (xr, r = 0,1,…,n-1) or Fourier transform coefficients (Xk, k = 0,1, …, n-1), and flag determines whether to perform a discrete transform (flag=-1) or an inverse discrete transform (flag=1). The function returns a vector containing either the discrete Fourier transform coefficients, Xk, if flag = -1, or the signal values, xr, if flag = 1. Example of DFT applications: Obtaining the discrete Fourier transform First, we generate a set of n=101 random numbers representing discrete values yr, r = 0, 1, …, n-1, of a signal y(t), through the use of function y = f(x) shown below. The function is a sum of m=10 cosine components plus a couple of random terms. The coefficients in the cosine components are random numbers generated using SCILAB function rand. The figure below shows the signal calculated: -->m=10; -->k=int(100*rand(1,m));a=int(1000*rand(1,m));phi=int(2*%pi*rand(1,m)); -->deff('[w]=f(x)',...

Page 65: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

65 © 2001 – Gilberto E. Urroz All rights reserved

-->['w=0';'for j=1:m';... -->'w=w+a(j)*cos(2*k(j)*%pi*x+phi(j))+100*(rand()-0.5)-2000*(rand()-0.5)';... -->'end']) -->x=[0:0.1:10]; y = f(x); plot(y); xtitle('Random data','k','y'); -->m=length(y)m = 101.

The discrete Fourier transform of the signal is obtained by using the following call to function dft. Of interest to us is the magnitude of the coefficients, i.e., |Xk| = (Ak

2+Bk2)1/2, which we

refer to as XfA. A plot of XfA is shown next. -->Xf=dft(y,-1); XfA = abs(Xf); -->plot2d([1:1:101]',XfA,-9) -->plot2d3('onn',[1:1:101]',XfA)

There are a few frequencies that shown strong components, e.g., for k = 1, 11, 31, 41, 42, 52, and so on. Also, notice that there is a symmetry in the figure, which results from the periodic nature assumed for the signal in Fourier analysis. This verifies the statement made earlier that out of the n components of the discrete Fourier transform, only those for k = 0, 1, …, n/2, are necessary to reproduce the signal. Example of DFT applications: Obtaining the inverse Fourier transform In this exercise we generate twenty of complex coefficients representing the Fourier transform of a signal, i.e., Xk, k = 0,1, …, n-1, with n=20. To visualize the Fourier transform we plot its absolute value, |Xk|: -->Xf2 = int(20*rand(1,20))+%i*int(10*(rand(1,20)-0.5));

Page 66: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

66 © 2001 – Gilberto E. Urroz All rights reserved

-->Xf2A = abs(Xf2); -->xset('mark',-9,1); plot2d([1:1:20]',Xf2A',-9) -->plot2d3('onn',[1:1:20]',Xf2A') -->xtitle('|X(w)| vs. w','w','|X(w)|')

Through the use of function dft, as shown below, we can generate a signal xs2 based on the Fourier transform coefficients Xf2. Signal xs2 is the inverse discrete Fourier transform of Xf2: -->xs2 = dft(Xf2,1) xs2 = ! 11.3 - .3i ! ! .3709582 + .1198511i ! ! - 2.2329752 - 1.2846661i ! ! 2.1378546 - .3916269i ! ! - .1146755 + .0364961i ! ! - .55 - 1.95i ! ! - .1372551 - .7108113i ! ! 1.5447692 - 1.233702i ! ! 2.1785955 + 1.5250802i ! ! - .2862057 + .1284268i ! ! - 1.2 + .3i ! ! 1.1105288 + .2007308i ! ! 1.604686 - 1.0823751i ! ! 1.075967 + .5249291i ! ! - .0593008 + .6035164i ! ! .45 + 1.35i ! ! - 1.468606 + .0707988i ! ! 2.1392805 + 1.176793i ! ! - .870469 + .8419610i ! ! 1.0068474 + .0745981i ! The resulting signal contains complex numbers. We can, therefore, obtain a signal consisting only of the real part of xs2 (xs2R), one consisting only of the imaginary part of xs2 (xs2I), and one formed by taking the magnitude of the elements of xs2 (xs2A). The three different signals are plotted below. -->xs2R = real(xs2);xs2I = imag(xs2); xs2A = abs(xs2);nn=[1:1:20]; -->plot2d(nn',xs2R,-9,'011',' ',[0 -3 21 13]) -->plot2d(nn',xs2R,1,'011',' ',[0 -3 21 13]) -->xtitle('Real part of signal','r','x(r)')

Page 67: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

67 © 2001 – Gilberto E. Urroz All rights reserved

-->plot2d(nn',xs2I,-9,'011',' ',[0 -2 21 1.6]) -->plot2d(nn',xs2I,1,'011',' ',[0 -2 21 1.6]) -->xtitle('Imaginary part of signal','r','x(r)')

-->plot2d(nn',xs2A,-9,'011',' ',[0 0 21 12]) -->plot2d(nn',xs2A,1,'011',' ',[0 0 21 12]) -->xtitle('Magnitude of signal','r','x(r)')

Page 68: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

68 © 2001 – Gilberto E. Urroz All rights reserved

Example of DFT applications: Obtaining a real inverse Fourier transform In order to obtain a real inverse Fourier transform, the vector of Fourier transform coefficients must satisfy certain symmetry conditions. Let X represent the vector of Fourier transform coefficients, the first term must be a real number, representing the coefficient a0 in the equation

[ ] .)sin()()cos()(22

)(0

0 ∫∞

++= ϖϖϖϖϖ dtBtAatf

If the vector contains an even number of elements n, then, the elements Xk, k=2,3, …,n in the vector X must be such that Xn = X2, Xn-1 = X3, Xn-2 = X4,…, or, in general, Xn-j+2 = Xj , j = 2,3, …, n/2. The terms X represents the complex conjugate of X. Finally, element Xn/2+1 must also be a real number. As an example, we construct the following vector of coefficients, Xf3, with n=20 elements: -->m=10;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5)); -->n=20;Xf3 = [120 X0 zeros(1,m-1)]; -->for j=2:n/2, Xf3(n-j+2)=conj(Xf3(j)); end; -->Xf3(n/2+1)=2.0; Xf3' ans = ! 120. ! ! 18. - 20.i ! ! 36. - 20.i ! ! - 85. + 9.i ! ! 88. + 43.i ! ! - 74. + 43.i ! ! - 59. - 3.i ! ! - 60. + 46.i ! ! 78. + 18.i ! ! - 7. + 12.i ! ! 2. ! ! - 7. - 12.i ! ! 78. - 18.i ! ! - 60. - 46.i ! ! - 59. + 3.i ! ! - 74. - 43.i ! ! 88. - 43.i ! ! - 85. - 9.i ! ! 36. + 20.i ! ! 18. + 20.i ! A plot of the Fourier coefficients’ magnitude is shown next. The symmetry of the coefficients is apparent from the plot: -->Xf3A=abs(Xf3); -->nn=[1:1:20];xset('mark',-9,1);plot2d(nn',Xf3A',-9,'010',' ',[0 0 21 150])

Page 69: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

69 © 2001 – Gilberto E. Urroz All rights reserved

-->plot2d3('onn',nn',Xf3A',1,'010',' ',[0 0 21 150]) -->xtitle('Symmetric Fourier coefficients','w','|X(w)|')

The corresponding signal is generated using: -->xs3 = dft(Xf3,1) xs3 = ! - .4 ! ! 20.141905 - 2.176E-15i ! ! 13.737285 - 9.770E-16i ! ! - 6.6365465 - 4.885E-15i ! ! - 5.0329987 + 6.040E-15i ! ! 22.8 - 1.137E-14i ! ! - 1.157441 + 2.469E-14i ! ! - 9.2480507 + 1.226E-14i ! ! - 9.98364 + 1.457E-14i ! ! 10.952211 - 6.528E-15i ! ! 41.2 - 5.351E-14i ! ! 1.3207894 + 1.164E-14i ! ! - 1.0044013 + 2.549E-14i ! ! - 7.835515 - 1.679E-14i ! ! - 12.637178 + 8.331E-14i ! ! 26.8 - 1.272E-13i ! ! 9.92104 - 1.137E-14i ! ! 4.9413984 - 6.040E-15i ! ! 26.357334 - 3.366E-14i ! ! - 4.2361923 + 4.836E-14i ! The generated signal is made of complex numbers, however, their imaginary parts are negligible. We will use only their real parts: -->xs3=real(xs3); A plot of the signal is presented next: -->plot(xs3,'r','x(r)','Real signal IDFT') -->plot2d(nn',xs3,-9,'010',' ',[1 -20 21 50])

Page 70: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

70 © 2001 – Gilberto E. Urroz All rights reserved

If the vector of Fourier transform coefficients contains an odd number of elements, the elements Xk, k=2,3, …,n in the vector X must be such that Xn = X2, Xn-1 = X3, Xn-2 = X4,…, or, in general, Xn-j+2 = Xj , j = 2,3, …, n/2+1. The first element in vector X must still be a real number. As an example, we put together the Fourier coefficients vector Xf4 with 21 components. A plot of the magnitude of the coefficients is shown next: -->m=10;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5)); -->n=21;Xf4 = [50 X0 zeros(1,m)]; -->for j=2:n/2+1, Xf4(n-j+2)=conj(Xf4(j)); end; -->Xf4A = abs(Xf4); -->nn=[1:1:21];xset('mark',-9,1);plot2d(nn',Xf4A',-9,'010',' ',[0 0 21 150]) -->plot2d3('onn',nn',Xf4A',1,'011',' ',[0 0 21 150]) -->xtitle('Symmetric Fourier coefficients - odd number','w','|X(w)|') The signal is generated with the following call to function dft. We replace the resulting signal, which may still contain negligible imaginary components, with its real part only, and plot the results as follows: -->xs4 = dft(Xf4,1); xs4=real(xs4); -->plot(xs4,'r','x(r)','Real signal IDFT - odd number')

Page 71: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

71 © 2001 – Gilberto E. Urroz All rights reserved

SCILAB’s fft function SCILAB’s fast Fourier transform function fft is called in a similar manner as dft. The general call to the function is

[xf]=fft(x,flag); where x is an input vector which may represent signal data (xr, r = 0,1,…,n-1) or Fourier transform coefficients (Xk, k = 0,1, …, n-1), and flag determines whether to perform a discrete transform (flag=-1) or an inverse discrete transform (flag=1). As with function dft, the function returns a vector containing either the discrete Fourier transform coefficients, Xk, if flag = -1, or the signal values, xr, if flag = 1. Note: while the FFT algorithm was developed for data sets containing 2m, m=integer, data points, the SCILAB function fft has no restriction with respect to the size of the sample used. Example of FFT applications: Obtaining a discrete Fourier transform First we generate a one-dimensional signal by adding random numbers: -->xs1=[];for j=1:200, xs1=[xs1 100*rand()-50*(rand()-0.5)]; end; -->plot(xs1,’r’,’x(r)’,’’)

Page 72: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

72 © 2001 – Gilberto E. Urroz All rights reserved

The discrete Fourier transform and a graph of the magnitude of the transform coefficients is shown next: -->Xf1=fft(xs1,-1); -->Xf1A=abs(Xf1); -->plot(Xf1A,'w','|X(w)|')

The original signal obtained by adding uniformly generated random numbers (with SCILAB function rand) produces what is typically known as white noise, i.e., a random signal where it is very difficult to detect any periodicity. The resulting Fourier transform coefficients show one single large component, for k=1, and almost negligible components for the rest of the frequency spectrum. Example of FFT applications: Obtaining an inverse Fourier transform To generate a real signal out of a vector of Fourier transform coefficients we use the following approach so that the vector of coefficients presents the symmetry characteristics defined earlier. We will use a vector of coefficients with 201 components: -->m=100;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5)); -->n=201;Xf2 = [50 X0 zeros(1,m)]; -->for j=2:n/2+1, Xf2(n-j+2)=conj(Xf2(j)); end; -->Xf2A=abs(Xf2); -->plot2d3('onn',[1:1:201]',Xf2A') -->xtitle('Symmetric Fourier coefficients','k','|X(k)|')

Page 73: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

73 © 2001 – Gilberto E. Urroz All rights reserved

Although the plot of the magnitude of the coefficients seems random, if you observe the graph carefully you will see the symmetry of the coefficients about the middle point. The following call to function fft produces the signal. A plot of the real part of the signal is also shown: -->xs2=fft(Xf2,1); -->xs2=fft(Xf2,1);xs2=real(xs2);plot(xs2,'r','x(r)','Real signal from IFFT')

Discrete Fourier transform for two-dimensional functions While function dft applies to a single vector of data, function fft can be applied also to a matrix representing a two-dimensional signal. You can actually apply fft to an array of N dimensions (N>2) by using SCILAB hypermatrices (see --> help hypermat). The call to fft when using hypermatrices is different from the one shown above. For more information the reader is referred to the SCILAB help facility (--> help fft). To understand the application of Fourier transforms to two-dimensional data we start with a two-dimensional array of data in the rectangle defined by 0 < x < Lx, 0 < y < Ly. Suppose that the rectangular region is divided in a grid of n1 points along the x-axis and n2 points along the y-axis so that at point zrs = z(xr,ys) represents the values of the data array. The coefficients of the two-dimensional Fourier transform for the data array z are given by

Page 74: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

74 © 2001 – Gilberto E. Urroz All rights reserved

,)](2exp[1 1

0

1

0 2121

1 2

∑∑−

=

=

⋅+⋅⋅−=n

r

n

srskm n

smn

rkiynn

Z π

for k=0,1,2,…,n1-1 and m=0,1,2,…,n2-1. On the other hand, the two-dimensional inverse Fourier transform will produce the values yrs according to

,)](2exp[1

0

1

0 21

1 2

∑∑−

=

=

⋅+⋅⋅−⋅=n

r

n

skmrs n

smn

rkiYz π

for r=0,1,2,…,n1-1 and s=0,1,2,…,n2-1. Example of FFT applications: Obtaining the discrete Fourier transform for a two-dimensional signal We will generate a two-dimensional signal by defining the following function, which involves a random component. A plot of such signal is shown next: -->deff('[w]=f(x,y)','w=sin(x)*cos(y) + (rand()-0.5)') -->x=[-6:0.5:6];y=x;z=feval(x,y,f); -->plot3d(x,y,z)

The following call to function fft generates the coefficients of the corresponding two-dimensional Fourier transform. To visualize the coefficients we plot their magnitudes using function hist3d (three-dimensional histogram -- see Chapter 3): -->Z = fft(z,-1);ZA=abs(Z); -->hist3d(ZA)

Page 75: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

75 © 2001 – Gilberto E. Urroz All rights reserved

The figure depicting the magnitudes of the Fourier transform coefficients show four components that tower above the remaining set of components. There is indeed only one major component, the one shown located closest to the origin (0,0). The other three components are simply reflections of the major component about of lines of symmetry located in the middle of the ranges of X and Y in the figure. This property of the two-dimensional Fourier transform of a real signal follows, as in the one-dimensional case, from the fact that Fourier transforms assume a certain periodicity in the independent variables of the function. Example of FFT applications: Obtaining a real two-dimensional inverse Fourier transform In order to obtain a real two-dimensional Fourier transform, the matrix of values Zkm must satisfy certain symmetry conditions. For the case in which the n1× n2 matrix of coefficients for the Fourier transform is such that both n1 and n2 are even, the values Z(1,1), Z(n1/2+1,1), Z(1,n2/+1), and Z(n1/2+1,n2/2+1) must always be real. The remaining elements of matrix Z are such that Z(n1-k+1,1) = Z(k,1), for k = 1,2,…,n1/2, Z(1,n2-m+1) = Z(1,m+1), for m = 1,2,…,n2, and Z(n1-k+1,n2-m+1) = Z(k,m), for k = 1,2,…,n1, m = 1,2,…,n2, where Z represents the complex conjugate of Z. The following SCILAB script (called Zgen) produces a matrix of Fourier transform coefficients from which a signal (inverse Fourier transform) is generated: n1=16;n2=16; Z=zeros(n1,n2); for i = 1:n1 for j = 1:n2 Z(i,j) = (int(100*((rand()-0.5)+%i*(rand()-0.5)))); end; end; for i = 2:n1/2 Z(n1-i+2,1) = conj(Z(i,1)); end; for j = 2:n2/2 Z(1,n2-j+2) = conj(Z(1,j)); end; for i = 2:n1 for j = 2:n2 Z(n1-i+2,n2-j+2) = conj(Z(i,j));

Page 76: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

76 © 2001 – Gilberto E. Urroz All rights reserved

end; end; Z(1,1) = 10; Z(n1/2+1,1) = 5.0; Z(1,n2/2+1) = 3.0; Z(n1/2+1,n2/2+1) = 2.0; x=[1:1:n1]; y=[1:1:n2]; xset('window',1);plot3d(x,y,abs(Z)); xtitle('Magnitude of Fourier transform coefficients'); z=fft(Z,1); xset('window',2);plot3d(x,y,real(z)); xtitle('Real signal generated with fft'); To run the script use function exec. Thus, if the script is stored in SCILAB’s current working directory, you would use: -->exec(‘Zgen’) The results are the following two graphs, the first one showing a plot of the magnitude of the Fourier series coefficients and the second one showing the signal generated with function fft:

Page 77: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

77 © 2001 – Gilberto E. Urroz All rights reserved

EExxeerrcciisseess [1]. Verify the following formula using Bessel functions of the first kind, Jn(x), for values of n = 2, 3, and 4:

.)()1()('21)( 2

2

221

0

2

⋅−+=⋅⋅∫ λ

λλλ nnn JnJdxxJx

[2]. Verify the following formulae using Bessel functions of the second kind, Yn(x), for values of n = 2, 3, and 4 by plotting both sides of the equations in the same set of graphs for 0<x<1. (a) Yn+1(x) = (2n/x) Yn(x) - Yn-1(x) (b) Y’n(x) = (1/2) [Yn-1(x)-Yn+1(x)] [3]. Rodrigue’s formula defines the Legendre polynomials, Pn(x), as

.)1(!2

1)( 2 nn

n

nn xdxd

nxP −

⋅=

Generate a sequence of Legendre polynomials using this formula for n = 0, 1, 2, 3, 4, 5. Use SCILAB polynomials and the function derivat. [4]. Verify the following recurrence formula for Legendre polynomials for n = 2, 3, 4, by plotting both sides of the equation in the same set of axes for 0<x<1.

).(1

)(112)( 11 xP

nnxxP

nnxP nnn −+ +

−++=

For problems [5] through [14] generate a table showing the calculation of the integral of f(x) between the limits x=a and x=b using Gaussian quadrature for values of n = 2,3,4,…,20. [5] f(x) = 1/(1+ ln(x)), a = 1, b = 5 [6] f(x) = 1 + x2 + x3 + 1/x, a = 1, b= 3.5 [7] f(x) = exp(-x/5)/(1+x2), a = -2, b = 2 [8] f(x) = (x2+1)1/2, a = -1 , b = 1 [9] f(x) = (x2+1)1/3+2.5(x2+1)+1.5 , a = 0, b = 5 [10] f(x) = sinh(x)/(1+x3) , a = 0 , b = 3 [11] f(x) = exp(-x2/2)/(2π)1/2, a = -4, b = 4

Page 78: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

78 © 2001 – Gilberto E. Urroz All rights reserved

[12] f(x) = 2.5(1+x2+x3/3) cos(3x2+2), a = 0, b = 2π [13] f(x) = 4.2x1/3 + 3.245x1/2 + 1.4142, a = 0.25, b = 1.25 [14] f(x) = 0.33 ln((x+1)/(x2+5x+2)) , a = 0, b = 20 For problems [15] through [19] use Gauss-Laguerre quadrature to obtain the integral of the functions f(x) in the interval [0,∞). [15]. f(x) = 1/(x+1)2 [16]. f(x) = exp(-x2) [17]. f(x) = ln(1+e-x) [18]. f(x) = 1/(1+x2)2

[19]. f(x) = e-x/(x+100) For problems [20] through [24] use Gauss-Hermite to obtain the integral of the functions f(x) in the interval (-∞,∞). [20]. f(x) = exp(-x2) [21]. f(x) = exp(-x2)cos(x) [22]. f(x) = 1/(1+x2)2 [23]. f(x) = exp(-x4) [24]. f(x) = 1/x2 + 1/x4

For problems [25] through [29] use Filon’s formulae to calculate the integral of the functions f(x)⋅ cos(kx) and f(x)⋅ cos(kx) between the integration limits x=a and x=b: [25]. f(x) = x+1, a = 0, b = 2π [26]. f(x) = exp(x), a = 2, b = 10 [27]. f(x) = ln(1+x2), a = 0, b = π /2 [28]. f(x) = 1/(1+x), a = 0, b = 2 [29]. f(x) = |x|, a = -1, b = 1 [30]. Verify, using SCILAB, that Hermite polynomials satisfy the following formula:

=≠

=∫∞

∞−

nmnnm

dxxHxHe nnmx

,!2,0

)()(2

π

Use values of (n,m) = (2,3) and (n,m) = (3,3).

Page 79: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

79 © 2001 – Gilberto E. Urroz All rights reserved

[31]. Verify, using SCILAB, that Laguerre polynomials are orthogonal in the range [0,∞) with respect to the function e-x ,i.e., verify that

0)()(0

=∫∞ − dxxLxLe nm

x

Use values of (n,m) = (2,3), (2,4), (3,4). [32]. (a) Verify that the second-order, ordinary differential equation, y”+λy = 0, with boundary conditions y(0) = 0, y(1) = 0, is a Sturm-Liouville system. (b) Find the eigenvalues, λ, of the system by first obtaining a general solution of the equation and replacing the boundary conditions in this solution. (c) Find the corresponding eigenfunctions. (d) Verify that the eigenfunctions are orthogonal in the interval [0,1]. [33]. Determine and plot the sine series corresponding to the following functions: f(x) = 4, 0<x<2, and f(x) = -4, 2<x<4, period L = 4 f(x) = -x2, - 4 < x < 0, and f(x)= x2, 0 < x < 4, period L = 8 f(x) = sin(x2+x/2), 0 < x < 5, period L = 5 f(x) = 2 - x, 0 < x < 4, and f(x) = x - 6, 4 < x < 8, period L = 8 [34]. Determine and plot the cosine series corresponding to the functions of problem [33]. [35]. Determine and plot the Fourier series corresponding to the functions of problem [33]. [36]. Determine the Fourier series corresponding to the following functions: f(x) = tanh(x), 0 < x < π, period L = π f(x) = 1/(1+x+x2), 0 < x < 1, period L = 1 f(x) = x exp(-x), 0 < x < 2, period L = 2 f(x) = x(π -x), 0 < x < π, period L = π [37]. Solve the following differential equation replacing the function f(x) with the Fourier series of problem [16]. Plot the solution for 0 < x < 4L, where L is the period of the function as specified in problem [16]. Use as initial conditions y(0) = 0, y’(0) = ½:

).(21

31

2

2

xfydxdy

dxyd =++

[38]. Solve the following differential equation replacing for f(x) each of the Fourier series in problem [17]. Plot the solution for 0 < x < 4L, where L is the period of the function as specified in problem [17]. Use as initial conditions y(0) = 1, y’(0) = 1:

).(2

2

xfydx

yd =+

Page 80: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

80 © 2001 – Gilberto E. Urroz All rights reserved

[39]. Solve the following differential equation replacing the periodic signals f(t), of period T = 4, shown in the figure below with their corresponding Fourier series. Plot the solution for 0 < t < 20. Use as initial conditions, x(0) = 1/3, x’(0) = -1:

).(2

2

tfxdtdx

dtxd =++

[40]. SCILAB’s function rand can be used to generate random numbers that have a uniform distribution in [0,1]. Use the following commands to generate 1000 random numbers uniformly distributed in [-0.5,0.5]: -->rand(‘uniform’); dx = rand(1,1000)-0.5; The vector dx now contains those random numbers. A plot of the signal dxk vs. k, for k = 1,2,…1000, represents what is commonly known as random noise or white noise (reference to the frequency contents of white light in the electromagnetic spectrum). Next, produce a signal x by using the formula

,,1

111 ∑=

+ ==k

iik dxxdxx

for k = 1,2, …, 999. The signal thus generated, xk, represents what is known as a fractal Brownian signal.

Page 81: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

81 © 2001 – Gilberto E. Urroz All rights reserved

(a) Obtain and plot the magnitude of the Fourier transform coefficients, |DX(k)|, for the

white noise signal dx. What can be said about the dependence of |DX(k)|, on k, k = 1,2,…,1000?

(b) Obtain and plot the magnitude of the Fourier transform coefficients, |X(ω)|, for the fractal Brownian signal x. . What can be said about the dependence of |DX(k)|, on k, k = 1,2,…,1000?

[41]. Repeat problem [40] using a normally distributed set of random numbers with function rand. Use the following SCILAB commands to generate signal dx: -->rand(‘normal’); dx = rand(1,1000)-0.5; [42]. Fractal signals are characterized by having Fourier transform coefficients that vary inversely with the frequency, i.e., |X(k)| = b/k, where b = constant. Generate and plot a real signal x(t) corresponding to 1000 Fourier transform coefficients given by |X(k)| = b/k, for k = 1,2,…,1000, and b = 100. Is the signal generated similar in shape to signal x generated in problems [40] and [41]? [43]. Generate a two-dimensional signal by using the function

w(x,y) = sqrt(x^2+y^2) + (rand()-0.5) and ranges of values of [-2:0.1:2] in x and y. Obtain and plot the two-dimensional Fourier transform using (a) a three-dimensional histogram, and (b) a surface plot. [44]. A two-dimensional fractal signal can be generated by defining Fourier transform coefficients of the form |X(k,m)| = b/(k2+m2)1/2. Generate a 20x20 set of Fourier transform coefficients corresponding to a two-dimensional fractal signal, plot the magnitude of the coefficients and the resulting two-dimensional signal.

Page 82: 10 Fourier Analysis - UNSomorr/radovan_omorjan_003_prII/s...quadrature, and Fourier analysis Orthogonal functions are sets of functions of an independent variable, say x, given in

528 © 2001 – Gilberto E. Urroz All rights reserved