You are on page 1of 81

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
ORTHOGONAL FUNCTIONS, GAUSSIAN QUADRATURE, AND FOURIER ANALYSIS 3

Solutions to selected specific second-order differential equations 3


The Cauchy or Euler equation 3
Legendre’s equation 4
Gamma, log-gamma and di-gamma functions 4
Bessel’s equation 7
Chebyshev polynomials 11
Laguerre’s equation 12
Weber’s equation and Hermite polynomials 14

Integration by Gaussian quadrature 16


Gaussian quadrature 16
General form of Gaussian quadrature 18
User-defined SCILAB function for calculating Gaussian quadrature 19
Application of function gaussquad 19
Improper integrals 20
Gauss-Laguerre formulae for improper integrals 21
SCILAB user-defined function for calculating Gauss-Laguerre quadrature 21
Application of function laguerrequad to calculate improper integral 22
Gauss-Hermite formulae for improper integrals 23
SCILAB user-defined function for calculating Gauss-Hermite quadrature 24
Application of function hermitequad for calculating improper integrals 24
Integrals involving sine and cosine terms - Filon’s formulae 25
SCILAB user-defined function for calculating integrals with Filon’s formulae 26
Application of function filonquad 27

Orthogonal functions and series expansions 28


Series expansions with orthogonal functions 29
Orthonormal functions 30
Orthogonality with respect to a weight function 31
The Sturm-Liouville problem 33

Fourier Analysis 33
Fourier series 33
Fourier sine series 34
Fourier cosine series 37
Fourier Series 45
A user-defined SCILAB function to calculate Fourier series 46
Applications of function fourierseries 47
Fourier series for a piecewise periodic function 49
Calculating Fourier series coefficients with Filon’s sine-cosine formulae 52
An alternative for calculating the coefficients of the Fourier series is to use function filonquad, defined
earlier for calculating integrals of the form 52
Use of Fourier series in ordinary differential equations 55
Fourier series for a trapezoidal pulse 55
Solution to the damped oscillator equation with trapezoidal pulse and wave excitation 57
Fourier series for other periodic shapes 58
Solving the damped oscillator equation with a different periodic trapezoidal pulse 59

Fourier transforms 60
Fourier analysis of discrete data 61
SCILAB functions for discrete Fourier transform (DFT) and fast Fourier transform (FFT) 62
Scilab’s dft function 62

Download at InfoClearinghouse.com 1 © 2001 – Gilberto E. Urroz


Example of DFT applications: Obtaining the discrete Fourier transform 62
Example of DFT applications: Obtaining the inverse Fourier transform 63
SCILAB’s fft function 69
Example of FFT applications: Obtaining a discrete Fourier transform 69
Example of FFT applications: Obtaining an inverse Fourier transform 70
Discrete Fourier transform for two-dimensional functions 71
Example of FFT applications: Obtaining the discrete Fourier transform for a two-dimensional signal 71
Example of FFT applications: Obtaining a real two-dimensional inverse Fourier transform 72

Exercises 74

REFERENCES (FOR ALL SCILAB DOCUMENTS AT INFOCLEARINGHOUSE.COM) 79

Download at InfoClearinghouse.com 2 © 2001 – Gilberto E. Urroz


Orthogonal functions, Gaussian
quadrature, and Fourier analysis
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


b
(gn, gm ) = g n ( x ) ⋅ g m ( x ) ⋅ dx = 0 ,
a

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.

Solutions to selected specific second-order


differential equations
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:

n 2 + (a − 1) ⋅ n + b = 0.

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

Download at InfoClearinghouse.com 3 © 2001 – Gilberto E. Urroz


y(x) = K1⋅x n
1 + K2⋅x n
2.

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
M
(2n − 2m)!
Pn ( x) = ∑ (−1) m ⋅ ⋅x n− 2 m
m=0 2 ⋅ m!⋅(n − m)!⋅(n − 2m)!
n

(2n)! (2n − 2)!


= ⋅ xn − n ⋅ x n − 2 + ... − ...
2 ⋅ (n!)
n 2
2 ⋅ 1!⋅(n − 1)!(n − 2)!

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


Γ(α ) = ∫ e −t t α −1 dt ,
0

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

Γ(n+1) = n!, or Γ (n) = (n-1)!.

Download at InfoClearinghouse.com 4 © 2001 – Gilberto E. Urroz


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')

-->p = zeros(10,1);

-->for nn = 1:10
--> p(nn) = legendrepol(nn,'x');
-->end;

-->p
p =

! x !
! !

Download at InfoClearinghouse.com 5 © 2001 – Gilberto E. Urroz


! 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)')

The result is the following graph:

Download at InfoClearinghouse.com 6 © 2001 – Gilberto E. Urroz


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,

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 ν:

(−1) m ⋅ x 2 m
J ν ( x ) = xν ⋅ ∑ 2 m +ν
,
m=0 2 ⋅ m!⋅Γ(ν + m + 1)

Download at InfoClearinghouse.com 7 © 2001 – Gilberto E. Urroz


where ν is not an integer.

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


(−1) m ⋅ x 2 m
J n ( x) = x n ⋅ ∑ 2m+n
.
m =0 2 ⋅ m!⋅(n + m)!

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

2 x x n ∞ (−1) m−1 ⋅ (hm + hm + n ) 2 m


Yn ( x) = ⋅ J n ( x) ⋅ (ln + γ ) + ⋅∑ ⋅x
π 2 π m=0 2 2 m+ n ⋅ m!⋅(m + n)!

x −n n −1
(n − m − 1)! 2 m
− ⋅∑ ⋅x
π m =0 2
2 m− n
⋅ m!

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

1 1 1
γ = lim[1 + + + ... + − ln r ] ≈ 0.57721566490...,
r →∞ 2 3 r

and hm represents the harmonic series

Download at InfoClearinghouse.com 8 © 2001 – Gilberto E. Urroz


1 1 1
hm = 1 + + + ... + .
2 3 m

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

2  x ∞
(−1) m−1 ⋅ hm 2 m 
Y0 ( x) = ⋅  J 0 ( x) ⋅ (ln + γ ) + ∑ 2 m ⋅x .
π  2 m=0 2 ⋅ (m!) 2 

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:

y = bessely(nu,x))

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

y = besseli(nu,x)

Download at InfoClearinghouse.com 9 © 2001 – Gilberto E. Urroz


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)')

Download at InfoClearinghouse.com 10 © 2001 – Gilberto E. Urroz


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:

-->T0 = chepol(0,'x')
T0 =
1

Download at InfoClearinghouse.com 11 © 2001 – Gilberto E. Urroz


-->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)')

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.

Download at InfoClearinghouse.com 12 © 2001 – Gilberto E. Urroz


Laguerre polynomials, defined as

e x d n (x n ⋅ e−x )
L0 ( x) = 1, Ln ( x) = ⋅ , n = 1,2,...
n! dx n
are solutions to Laguerre’s equation. Laguerre’s polynomials can also be calculated with:

n
(−1) m n n(n − 1) 2 (−1) n n
Ln ( x ) = ∑ ⋅   ⋅ x m = 1 − n ⋅ x + ⋅ x − ... + .... + ⋅x .
m =0 m!  m 4 n!

The term
n n!
  = = C (n, m)
 m  m!(n − m)!
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')

-->L0 = laguerrepol(0,'x')
L0 =
1

-->L1 = laguerrepol(1,'x')
L1 =
1 - x

-->L2 = laguerrepol(2,'x')
L2 =

Download at InfoClearinghouse.com 13 © 2001 – Gilberto E. Urroz


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


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:

Download at InfoClearinghouse.com 14 © 2001 – Gilberto E. Urroz


d n − x2
H 0 * = 1, H n * ( x) = (−1) n e x (e ), n = 1,2,...
2

dx n
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')
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)')

Download at InfoClearinghouse.com 15 © 2001 – Gilberto E. Urroz


Integration by Gaussian quadrature
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

n
f ( x)dx ≈ ∑ Ai f ( xi ) .
b
∫a
i =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.

Download at InfoClearinghouse.com 16 © 2001 – Gilberto E. Urroz


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

n
f ( x)dx ≈ ∑ Ai f ( xi ) = A1 f ( x1 ) + A2 f ( x 2 ), n = 2
1
∫−1
i =1

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
For f ( x) = 1 ⇒ ∫−1
1 ⋅ dx = 2 = A1 + A2 ,
1
For f ( x) = x ⇒ ∫ −1
x ⋅ dx = 0 = A1 x1 + A2 x 2 ,
1 2
For f ( x) = x 2 ⇒ ∫
−1
x 2 ⋅ dx =
3
= A1 x12 + A2 x 22 ,
1
For f ( x) = x 3 ⇒ ∫ −1
x 3 ⋅ dx = 0 = A1 x13 + A2 x 23 .

The solution of the system of algebraic equations thus generated is

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

The required integral is now written as

1 1

1
f ( x)dx ≈ f (− )+ f( ).
−1
3 3
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

2x − a − b
t= ,
b−a

Download at InfoClearinghouse.com 17 © 2001 – Gilberto E. Urroz


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 1 1 b−a b+a
∫ a
f ( x)dx =
2
(b − a ) ⋅ ∫ f (
−1 2
⋅t +
2
)dt

1 1 b−a b+a 1 b−a b+a


≈ (b − a) ⋅ [ f (− ⋅ + )+ f( ⋅ + )].
2 3 2 2 3 2 2

General form of Gaussian quadrature


We can generalize the previous result for any integer n by writing

1 b−a b+a 1 n
b−a b+a
)dt ≈ (b − a ) ⋅ ∑ Ai ⋅ f (
b 1
∫a
f ( x)dx =
2
(b − a ) ⋅ ∫ f (
−1 2
⋅t +
2 2 i =1 2
⋅ xi +
2
).

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

n
f ( x)dx ≈ ∑ Ai f ( xi ) .
1
∫−1
i =1

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

∑Ax
1
i
k
i = ∫ x k dx, i = 1,2,..., n, k = 0,1,2,...,2 ⋅ n − 1.
−1
i =1

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

2
Ai = .
(1 − x )[ Pn ' ( xi )] 2
2
i

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


M
(2n − 2m)!
Pn ( x) = ∑ (−1) m ⋅ ⋅x n− 2 m
m=0 2 ⋅ m!⋅(n − m)!⋅(n − 2m)!
n

(2n)! (2n − 2)!


= ⋅ xn − n ⋅ x n −2 + ... − ...
2 ⋅ (n!)
n 2
2 ⋅ 1!⋅(n − 1)!(n − 2)!

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

Download at InfoClearinghouse.com 18 © 2001 – Gilberto E. Urroz


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));
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')

Download at InfoClearinghouse.com 19 © 2001 – Gilberto E. Urroz


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 !
! 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 dx ∞ dx 1 dx
I1 = ∫ , I = ∫0 x 2 , I = ∫− ∞ 1 + x 2 .
−∞ 1 + x 2
2 2

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.,

Download at InfoClearinghouse.com 20 © 2001 – Gilberto E. Urroz


1 dx 1 dx π π π 3π
I1 = ∫ = lim ∫ = lim arctan( x) |1ε = lim ( − arctan(ε )) = − (− ) = .
−∞ 1 + x 2 ε → −∞ ε 1 + x 2 ε → −∞ ε → −∞ 4 4 2 4

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:

∞ n

∫ e − x g ( x)dx ≈ ∑ Ai ⋅ g ( xi ).
0
i =1

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

xi 1
Ai = = .
(n + 1) [ Ln+1 ( x)]
2 2
xi ⋅ [ L' n ( xi )]2

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

n
(−1) m n n(n − 1) 2 (−1) n n
Ln ( x ) = ∑ ⋅   ⋅ x m = 1 − n ⋅ x + ⋅ x − ... + .... + ⋅x .
m =0 m!  m 4 n!

where
n n!
  = = C (n, m)
 m  m!(n − m)!
is the binomial coefficient.

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

∞ ∞ n

∫ f ( x)dx = ∫ e − x {e x f ( x)}dx ≈ ∑ Ai ⋅ e xi ⋅ f ( xi ).
0 0
i =1

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

Download at InfoClearinghouse.com 21 © 2001 – Gilberto E. Urroz


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');

//end function laguerrepol

Application of function laguerrequad to calculate improper integral

We use function laguerrequad to calculate the improper integral

∞ dx
I1 = ∫ .
01+ x2

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)')

Download at InfoClearinghouse.com 22 © 2001 – Gilberto E. Urroz


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 !

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:

∞ n

∫ e − x g ( x)dx ≈ ∑ Ai ⋅ g ( xi ).
2

−∞
i =1

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

Download at InfoClearinghouse.com 23 © 2001 – Gilberto E. Urroz


2 n−1 n! π 2 n +1 n! π
Ai = = .
n 2 [ H n −1 ( xi )] 2 [ H ' n ( xi )] 2

Hermite polynomials are defined as:

d n − x2
H 0 = 1, H n ( x) = (−1) n e x (e ), n = 1,2,...
2

dx n

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

∞ ∞ n

∫ f ( x)dx = ∫ e − x {e x f ( x)}dx ≈ ∑ Ai ⋅ e xi ⋅ f ( xi ).
2 2 2

−∞ −∞
i =1

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');
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))')

Download at InfoClearinghouse.com 24 © 2001 – Gilberto E. Urroz


-->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 !
! 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

xn
∫ x0
f ( x) ⋅ cos(kx)dx = ∆x ⋅ [α{ f ( x n ) sin( kx n ) − f ( x0 ) sin( kx0 )} + β ⋅ κ e + γ ⋅ κ o ],

xn
∫x0
f ( x) ⋅ sin( kx)dx = ∆x ⋅ [α{ f ( x 0 ) cos(kx0 ) − f ( x n ) cos(kx n )} + β ⋅ σ e + γ ⋅ σ o ],

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

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

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

Download at InfoClearinghouse.com 25 © 2001 – Gilberto E. Urroz


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

n− 2
1
κ e = { f ( x0 ) cos(kx0 ) + f ( x n ) cos(kxn )} +
2
∑ f ( x ) cos(kx ),
i = 2 , 4 , 6 ,...
i i

n −1
κo = ∑ f ( x ) cos(kx ),
i =1, 3, 5 ,...
i i

n− 2
1
σ e = { f ( x0 ) sin(kx0 ) + f ( x n ) sin(kxn )} +
2
∑ f ( x ) sin(kx ),
i = 2 , 4 , 6 ,...
i i

and
n −1
σo = ∑ f ( x ) sin(kx ).
i =1, 3, 5 ,...
i i

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
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))];

Download at InfoClearinghouse.com 26 © 2001 – Gilberto E. Urroz


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


-->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 !

Download at InfoClearinghouse.com 27 © 2001 – Gilberto E. Urroz


! - .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 !

Orthogonal functions and series expansions


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


b
(gn, gm ) = g n ( x ) ⋅ g m ( x ) ⋅ dx = 0 ,
a

for n ≠ m.

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

b
|| g n || 2 = ( g n , g n ) = ∫ [ g n ( x)]2 ⋅ dx ≠ 0.
a
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

1

 sin( n π x ) sin( m π x ) dx

0

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

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

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
∫ sin (nπx)dx
2
0

which evaluates to

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

If n is an integer, this result evaluates to ½.

Download at InfoClearinghouse.com 28 © 2001 – Gilberto E. Urroz


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)
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.,

f ( x) = ∑ C n ⋅ g n ( x),
n =1

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.,


f ( x) ⋅ g m ( x) = ∑ C n ⋅ g n ( x) ⋅ g m ( x).
n =1

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


f ( x) ⋅ g m ( x) ⋅ dx = ∑ C n ⋅∫ g n ( x) ⋅ g m ( x) ⋅ dx.
b b
∫a
n =1
a

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:

b b
∫ a
f ( x) ⋅ g m ( x) ⋅ dx = C m ⋅∫ [ g m ( x)] 2 ⋅ dx
a

Download at InfoClearinghouse.com 29 © 2001 – Gilberto E. Urroz


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

Cn =
∫ a
f ( x) ⋅ g n ( x) ⋅ dx
=
( f , gn )

b
∫ [g
a
n ( x)]2 ⋅ dx || g n ||

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

f ( x) = ∑ C n ⋅ sin( nπx),
n =1
and,

1 1

Cn =

0
f ( x) ⋅ sin( nπx) ⋅ dx
=
∫ 0
f ( x) ⋅ sin( nπx) ⋅ dx 1
= 2 ⋅ ∫ f ( x) ⋅ sin( nπx) ⋅ dx.
1
∫ sin (nπx) ⋅ dx 1/ 2
2 0
0

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

0, when m ≠ n
(φ n , φ m ) = ∫ g n ( x) ⋅ g m ( x) ⋅ dx = 
b

a
1, when m = n

Thus, the norm of an orthonormal function is ||φn|| = 1.0.

Download at InfoClearinghouse.com 30 © 2001 – Gilberto E. Urroz


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

 cos( n π x ) cos( m π x ) dx

0

evaluates to zero for m ≠ n. Therefore, the functions gn(x) are orthogonal.

The norm of the function gn(x) is calculated by using

1

 cos( n π x ) 2 dx


0

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


b
p ( x ) ⋅ g n ( x ) ⋅ g m ( x ) ⋅ dx = 0 .
a

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

b
|| g n || 2 = ∫ p ( x) ⋅ [ g n ( x)]2 ⋅ dx ≠ 0.
a

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


f ( x) = ∑ C n ⋅ g n ( x),
n =1

Download at InfoClearinghouse.com 31 © 2001 – Gilberto E. Urroz


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

C n=

a
p ( x) ⋅ f ( x) ⋅ g n ( x) ⋅ dx
=
1 b
⋅ ∫ p( x) ⋅ f ( x) ⋅ g n ( x) ⋅ dx ⋅
b
∫ a
p ( x) ⋅ [ g n ( x)] 2 ⋅ dx || g n || a

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

1

 2
 x (2 x − 1)
 dx


 1−x
2

-1

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')

Download at InfoClearinghouse.com 32 © 2001 – Gilberto E. Urroz


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


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

d  dy 
 r ( x) ⋅  + [q ( x) + λ ⋅ p ( x)] y = 0,
dx  dx 

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.

Fourier Analysis
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

Download at InfoClearinghouse.com 33 © 2001 – Gilberto E. Urroz


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:

-->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,

nπx
f( x ) = ∑ bn sin 
n=1  L 

iπx jπx
Because the sine functions sin  and sin  are orthogonal, i.e.,
 L   L 
L

 sin i π x  sin j π x  d x = 0 , for i ≠ j ,
    

  L   L 
⌡0
then

L L
⌠ ⌠ 2
 f( x ) sin n π x  d x = b  nπx L bn
 
    sin 
 L  d x = ,

  L 
n
   2
⌡0 ⌡0

Download at InfoClearinghouse.com 34 © 2001 – Gilberto E. Urroz


from which it follows that
L

2  f( x ) sin n π x  dx
bn =   
L 
  L 
⌡0

for n = 1, 2, 3,....

Consider the function f( x ) = x − x . This function is obviously not periodic, as shown in the
2

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

! .2122066 - .1788825 .1591549 - .1400554 .1273240 !

Download at InfoClearinghouse.com 35 © 2001 – Gilberto E. Urroz


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')

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

Download at InfoClearinghouse.com 36 © 2001 – Gilberto E. Urroz


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')

Fourier cosine series

Fourier cosine series are such that,

Download at InfoClearinghouse.com 37 © 2001 – Gilberto E. Urroz



nπx
f( x ) = ∑ an cos 
n=0  L  .

iπx jπx
Because the cosine function cos   and cos   are also orthogonal, i.e.,
 L   L 
L

 cos  i π x  cos  j π x  d x = 0 , for i ≠ j ,
    

  L   L 
⌡0
then
L L
⌠ ⌠ 2
 f( x ) cos  n π x  d x = a   n π x  dx
L an
 
 
  sin 
 
 = ,

  L 
n   L  2
⌡0 
⌡0
from which it follows that
L

2  f( x ) cos  n π x  dx
an =   
L 
  L 
⌡0

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


L
1 ⌠
a0 = f( x ) d x .
L 
⌡0

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)')

-->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

Download at InfoClearinghouse.com 38 © 2001 – Gilberto E. Urroz


! - .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
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')

Download at InfoClearinghouse.com 39 © 2001 – Gilberto E. Urroz


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')

_________________________________________________________________________________
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:
L
L 2
2

a0 =
1 ⌠
f( x ) d x , an =
2  f( x ) cos  2 n π x  dx ,
L    
⌡ L L 
  L 

2
⌡ L

2
and

Download at InfoClearinghouse.com 40 © 2001 – Gilberto E. Urroz


L
2

bn =
2  f( x ) sin 2 n π x  dx
  
L 
  L 
⌡ L

2

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


∞ ∞
 2 n π x  2nπx
f( x ) = a0 +  ∑ a n cos    and f( x ) = ∑ bn sin .
n = 1  L   n=1  L 

____________________________________________________________________________

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:

-->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);

Download at InfoClearinghouse.com 41 © 2001 – Gilberto E. Urroz


-->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.

The next example shows a cosine series fitting in the interval [-1,1], L = 2, for the function f(x)
= 1 − x . First, we define functions f(x) and g(x) and the generic coefficient of the Fourier
2

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;

Download at InfoClearinghouse.com 42 © 2001 – Gilberto E. Urroz


//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')

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
L L
1 ⌠ ⌠
L
a0 =
1 ⌠
f( x ) d x , a n =  f( x ) cos  n π x  dx , and b = 1  f( x ) sin n π x  dx ,
      
L ⌡−L L 
  L 
n L 
  L 
⌡−L ⌡−L
for n = 1,2,3, ...
The sine and cosine series will be calculated as:
a0  ∞ ∞
n π x  nπx
f( x ) = +  ∑ a n cos 
  and f ( x ) = ∑ bn sin L  .
2 n =1  L  n=1  
_______________________________________________________________________________

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)')

Download at InfoClearinghouse.com 43 © 2001 – Gilberto E. Urroz


-->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')

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')

Download at InfoClearinghouse.com 44 © 2001 – Gilberto E. Urroz


Fourier Series
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:

a0 ∞
2πnx  2 π n x 
f( x ) =
2
+ ∑  an cos  + b n sin  
n=1   L   L 

with the coefficients calculated as:

L
L 2
2

a0 =
2 ⌠
f( x ) d x , an =
2  f( x ) cos  2 n π x  dx ,
L    
⌡ L L 
  L 

2
⌡ L

2

and
L
2

bn =
2  f( x ) sin 2 n π x  dx
  
L 
  L 
⌡ L

2

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

c +L
c +L 0
0 ⌠ 2nπx
 f( x ) cos 
2 ⌠ 2
a0 = f( x ) d x , an =   dx ,
L 
⌡c L 
  L 
0 ⌡c
0

and

Download at InfoClearinghouse.com 45 © 2001 – Gilberto E. Urroz


c +L
0
⌠ 2nπx
 f( x ) sin
2
bn =   dx
L 
  L 
⌡c
0
.

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

[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)];

Download at InfoClearinghouse.com 46 © 2001 – Gilberto E. Urroz


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


//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 + x in the
2

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')

Download at InfoClearinghouse.com 47 © 2001 – Gilberto E. Urroz


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,:)'])


-->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')

Download at InfoClearinghouse.com 48 © 2001 – Gilberto E. Urroz


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;

-->getf('fourierseries')

Download at InfoClearinghouse.com 49 © 2001 – Gilberto E. Urroz


-->[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:

2 L/2  nπx  2 1/ 3  nπX  6  nπ 


an =
L ∫− L / 2
h ( x ) ⋅ cos
 L 
 ⋅ dx =
2 ∫−1 / 3
3 ⋅ cos
 L 
 ⋅ dx =

sin 
 3 
,

and

2 L/2  nπ x  2 1/ 3  nπ X 
bn = ∫
L −L / 2
h( x) ⋅ sin 
 L 
 ⋅ dx = ∫ 3 ⋅ sin 
2 −1 / 3  L 
 ⋅ dx = 0.

Also,

2 L/2 2 1/ 3
a0 =
L ∫− L / 2
h( x) ⋅dx = ∫ 3 ⋅ dx = 2.0.
2 −1 / 3

Download at InfoClearinghouse.com 50 © 2001 – Gilberto E. Urroz


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'])

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

Download at InfoClearinghouse.com 51 © 2001 – Gilberto E. Urroz


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

xm xm
∫x0
f ( x) ⋅ sin( kx)dx, ∫
x0
f ( x) ⋅ cos(kx)dx

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
--> ResCos(i,j)=(2/L)*filonquad('cos',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResCos
ResCos =

Download at InfoClearinghouse.com 52 © 2001 – Gilberto E. Urroz


! - .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 !

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;

Download at InfoClearinghouse.com 53 © 2001 – Gilberto E. Urroz


-->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 !
! - 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

Download at InfoClearinghouse.com 54 © 2001 – Gilberto E. Urroz


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):

∂ ∂
2

m  2 y( x )  + β  y( x )  + k y( x ) = F( x )
 ∂x  ∂
 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)

.
 3 x, 0 < x < 1/ 3
1, 1/ 3 < x < 2 / 3

f ( x) =   2
 1 − 3 x − 3 , 2 / 3 < x < 1
  
 0, elsewhere

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';...
--> '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')

Download at InfoClearinghouse.com 55 © 2001 – Gilberto E. Urroz


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')

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')

Download at InfoClearinghouse.com 56 © 2001 – Gilberto E. Urroz


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.

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);

Download at InfoClearinghouse.com 57 © 2001 – Gilberto E. Urroz


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')

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.

Download at InfoClearinghouse.com 58 © 2001 – Gilberto E. Urroz


-->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')

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.

Download at InfoClearinghouse.com 59 © 2001 – Gilberto E. Urroz


Fourier transforms
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 T /2 2 T /2 2 T /2
T ∫−T / 2 T ∫−T / 2 T ∫−T / 2
a0 = f (t ) dt , a n = f (t ) cos(ϖ n t ), b n = f (t ) sin(ϖ n t ).

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

a0 ∞  2 T / 2   2 T /2  
f (t ) = + ∑  ∫ f (t ) cos(ϖ n t )dt  cos(ϖ n t ) +  ∫ f (t ) sin(ϖ n t ) sin(ϖ n t )dt .
2 n =1 T −T / 2   T −T / 2  

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

Download at InfoClearinghouse.com 60 © 2001 – Gilberto E. Urroz


a0 ∞   dϖ ∞   dϖ ∞  
f (t ) = + ∫  ∫ f (t ) cos(ϖ n t )dt  cos(ϖ n t ) +  ∫ f (t ) sin(ϖ n t )dt  sin(ϖ n t ).
2 0
 π −∞
 π −∞
 

If we define

1 ∞ 1 ∞
A(ϖ ) =
2π ∫−∞
f (t ) cos(ϖt )dt , B(ϖ ) =
2π ∫ −∞
f (t ) sin(ϖt )dt ,

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

+ 2 ∫ [ A(ϖ ) cos(ϖt ) + B (ϖ ) sin(ϖt )]dϖ .


a0 ∞
f (t ) =
2 0

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
1 ∞
F (ω ) =
2π ∫
−∞
f (t ) ⋅ e −iωt dt.

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


f (t ) = ∫ F (ϖ ) ⋅ e iωt dϖ .
−∞

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 n −1 1 n −1
X k = ∑ xr e −i ( 2πk / T )( r ⋅∆t )
∆t = ∑ x r e −i ( 2πkr / n ) , k = 0,1,..., n − 1 .
T r =0 n r =0

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:

Download at InfoClearinghouse.com 61 © 2001 – Gilberto E. Urroz


n −1
x r = ∑ X k e i ( 2πkr / n ) , r = 0,1,..., n − 1.
k =0

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)',...
-->['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'])

Download at InfoClearinghouse.com 62 © 2001 – Gilberto E. Urroz


-->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| = (Ak2+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));
-->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)|')

Download at InfoClearinghouse.com 63 © 2001 – Gilberto E. Urroz


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)')

Download at InfoClearinghouse.com 64 © 2001 – Gilberto E. Urroz


-->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)')

Download at InfoClearinghouse.com 65 © 2001 – Gilberto E. Urroz


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

+ 2 ∫ [ A(ϖ ) cos(ϖt ) + B (ϖ ) sin(ϖt )]dϖ .


a0 ∞
f (t ) =
2 0

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])


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

Download at InfoClearinghouse.com 66 © 2001 – Gilberto E. Urroz


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])

Download at InfoClearinghouse.com 67 © 2001 – Gilberto E. Urroz


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')

Download at InfoClearinghouse.com 68 © 2001 – Gilberto E. Urroz


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)’,’’)

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)|')

Download at InfoClearinghouse.com 69 © 2001 – Gilberto E. Urroz


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)|')

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')

Download at InfoClearinghouse.com 70 © 2001 – Gilberto E. Urroz


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

n1 −1 n2 −1
1 k ⋅r m⋅s
Z km =
n1 n2
∑∑y
r =0 s =0
rs exp[−i ⋅ 2π (
n1
+
n2
)],

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

n1 −1 n2 −1
k ⋅r m⋅s
z rs = ∑ ∑ Ykm ⋅ exp[−i ⋅ 2π ( + )],
r =0 s =0 n1 n2

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)

Download at InfoClearinghouse.com 71 © 2001 – Gilberto E. Urroz


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)

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.

Download at InfoClearinghouse.com 72 © 2001 – Gilberto E. Urroz


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));
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:

Download at InfoClearinghouse.com 73 © 2001 – Gilberto E. Urroz


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

1 1 2 n2 
∫ x ⋅ J n2 (λ ⋅ x)dx = λ + − ⋅ λ
2
 n
J ' ( ) (1 ) J ( ) .
λ2
n
0 2 

[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 dn 2
Pn ( x) = ( x − 1) n .
2 ⋅ n! dx
n n

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.

2n + 1 n
Pn +1 ( x) = xPn ( x) − Pn −1 ( x).
n +1 n +1
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.

Download at InfoClearinghouse.com 74 © 2001 – Gilberto E. Urroz


[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

[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

Download at InfoClearinghouse.com 75 © 2001 – Gilberto E. Urroz


[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:

∞  0, m ≠ n 
∫ e − x H m ( x) H n ( x)dx =  n
2


−∞
2 n! π , m = n 
Use values of (n,m) = (2,3) and (n,m) = (3,3).

[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
e − x Lm ( x) Ln ( x)dx = 0

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) = ½:

d 2 y 1 dy 1
+ + y = f ( x).
dx 2 3 dx 2

Download at InfoClearinghouse.com 76 © 2001 – Gilberto E. Urroz


[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:

d2y
+ y = f ( x).
dx 2
[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:

d 2 x dx
+ + x = f (t ).
dt 2 dt

[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

Download at InfoClearinghouse.com 77 © 2001 – Gilberto E. Urroz


k
x1 = dx1 , x k +1 = ∑ dxi ,
i =1

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

(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.

Download at InfoClearinghouse.com 78 © 2001 – Gilberto E. Urroz


REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.

Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.

Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.

Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.

Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.

Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.

Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.

Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.

Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.

Gomez, C. (editor), 1999, “Engineering and Scientific Computing with Scilab,” Birkhäuser, Boston.

Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.

Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB® - Second edition,"
Brooks/Cole - Thompson Learning, Australia.

Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.

Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.

Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.

Julien, P.Y., 1998,”Erosion and Sedimentation,” Cambridge University Press, Cambridge CB2 2RU, U.K.

Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.

Kitanidis, P.K., 1997,”Introduction to Geostatistics - Applications in Hydogeology,” Cambridge University Press,


Cambridge CB2 2RU, U.K.

Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.

Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.

Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.

Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.

Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab®," Prentice Hall, Upper Saddle River, New Jersey.

Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB®", Prentice Hall, Upper Saddle River, N.J., U.S.A.

McCuen, R.H., 1989,”Hydrologic Analysis and Design - second edition,” Prentice Hall, Upper Saddle River, New Jersey.

Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab®," Prentice Hall, Upper Saddle River, New
Jersey.

Download at InfoClearinghouse.com 79 © 2001 – Gilberto E. Urroz


Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.

Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.

Nicols, G., 1995, “Introduction to Nonlinear Science,” Cambridge University Press, Cambridge CB2 2RU, U.K.

Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems,” 1989, Springer-Verlag, New York.

Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.

Peitgen, H-O., H. Jürgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.

Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, “Numerical Recipes - The Art of Scientific
Computing (FORTRAN version),” Cambridge University Press, Cambridge CB2 2RU, U.K.

Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.

Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.

Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.

Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.

Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.

Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.

Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.

Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.

Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.

Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.

Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.

Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.

Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate


Engineering Students," John Wiley & Sons, Inc., New York.

Download at InfoClearinghouse.com 80 © 2001 – Gilberto E. Urroz

You might also like