Orthogonal Polynomials (In Matlab) : Walter Gautschi
Orthogonal Polynomials (In Matlab) : Walter Gautschi
Orthogonal Polynomials (In Matlab) : Walter Gautschi
Walter Gautschi
Abstract. A suite of Matlab programs has been developed as part of the book
“Orthogonal Polynomials: Computation and Approximation” expected to be pub-
lished in 2004. The package contains routines for generating orthogonal polyno-
mials as well as routines dealing with applications. In this paper, a brief review
is given of the first part of the package, dealing with procedures for generating
the three-term recurrence relation for orthogonal polynomials and more general
recurrence relations for Sobolev orthogonal polynomials. Moment-based methods
and discretization methods, and their implementation in Matlab, are among the
principal topics discussed.
Keywords: Orthogonal polynomials; recurrence relations; Matlab.
1. Introduction
http://www.cs.purdue.edu/archives/2002/wxg/codes.
The purpose of the work in [13] is twofold: (i) to present various procedures for
generating the coefficients of the recurrence relations satisfied by orthogonal
polynomials on the real line and by Sobolev orthogonal polynomials; and (ii) to
discuss selected applications of these recurrence relations, including numerical
quadrature, least squares and moment-preserving spline approximation, and
the summation of slowly convergent series. All is to be implemented in the
1
form of Matlab scripts. In the present article we wish to give a brief account
of the first part of [13]: the generation of recurrence coefficients for orthogonal
polynomials and related Matlab programs. All Matlab routines mentioned in
this paper, and many others, are downloadable individually from the above
Web site.
2. Orthogonal polynomials
We begin with some basic facts about orthogonal polynomials on the real line
and introduce appropriate notation as we go along. Suppose dλ is a positive
measure supported on an interval (or a set of disjoint intervals) on the real
line such that all moments µr = R tr dλ(t) exist and are finite. Then the inner
R
product Z
(p, q)dλ = p(t)q(t)dλ(t) (1)
R
is well defined for any polynomials p, q and gives rise to a unique system
πr (t) = tr + · · · , r = 0, 1, 2, . . . , of monic orthogonal polynomials
=
0, k 6= `,
πk ( · ) = πk ( · ; dλ) : (πk , π` )dλ (2)
>
0, k = `.
where αk = αk (dλ) and βk = βk (dλ) are real resp. positive Rconstants which de-
pend on the measure dλ. For convenience, we define β0 = R dλ(t). Associated
with the recurrence relation (3) is the Jacobi matrix
√
α β 0
0 1
√β α √β
1 1 2
√ ..
J(dλ) =
β2 α2 . , (4)
.. ..
. .
0
As already indicated in §1, the basic problem is this: for a given measure
dλ and for given integer n ≥ 1, generate the first n coefficients αk (dλ), k =
2
0, 1, 2, . . . , n − 1, and the first n coefficients βk (dλ), k = 0, 1, 2, . . . , n − 1, that
is, the Jacobi matrix J n (dλ) of order n and β0 .
For these, the recurrence coefficients are explicitly known. In Matlab, the first
N recurrence coefficients are always stored in an N × 2 array ab as shown in
Fig. 2.1.
α0 β0
α1 β1
.. ..
. .
αN −1 βN −1
The Matlab command to compute them has the syntax ab=r name(parameters),
where name identifies the weight function, and parameters is a list of parame-
ters including N. Thus, for example, in the case of the Jacobi weight function,
the Matlab command is
ab=r jacobi(N,a,b).
Demo#1. The first ten recurrence coefficients for the Jacobi polynomials with
parameters α = − 21 , β = 23 .
The Matlab command, followed by the output, is shown in the box below.
3
>> ab=r jacobi(10,-.5,1.5)
ab =
6.666666666666666e-01 4.712388980384690e+00
1.333333333333333e-01 1.388888888888889e-01
5.714285714285714e-02 2.100000000000000e-01
3.174603174603174e-02 2.295918367346939e-01
2.020202020202020e-02 2.376543209876543e-01
1.398601398601399e-02 2.417355371900826e-01
1.025641025641026e-02 2.440828402366864e-01
7.843137254901961e-03 2.455555555555556e-01
6.191950464396285e-03 2.465397923875433e-01
5.012531328320802e-03 2.472299168975069e-01
Classical weight functions are not the only ones for which the recurrence co-
efficients are explicitly known. For example, the logistic weight function
e−t
w(t) = , t ∈ R,
(1 + e−t )2
Many nonclassical weight functions and measures, however, are such that their
recurrence relations are not explicitly known. In these cases, numerical tech-
niques must be used, some of which are to be described in the next four
subsections.
where pr are monic polynomials of degree r “close” in some sense to the desired
polynomials πr . In particular, they are assumed to also satisfy a three-term
recurrence relation
4
but this time with known coefficients ak ∈ R, bk ≥ 0. (We allow for zero
coefficients bk , since ak = bk = 0 yields the ordinary moments.) There is then
a unique map
R2n 7→ R2n : [mk ]k=0
2n−1 n−1
7→ [αk , βk ]k=0 (9)
that takes the first 2n modified moments into the desired n recurrence coef-
ficients αk and βk . An algorithm implementing this map has been developed
by Sack and Donovan [21], and in more definitive form, by Wheeler [26]. In
the case of ordinary moments (ak = bk = 0), it reduces to an algorithm alrady
developed (for discrete measures) by Chebyshev [2]. We called it, therefore,
the Modified Chebyshev Algorithm. It is implemented in the Matlab procedure
ab=chebyshev(N,mom,abm),
Since the weight function reduces to the Chebyshev weight function when
ω = 0, it seems natural to use as modified moments those relative to the
monic Chebyshev polynomials,
Z 1 1 Z 1
m0 = w(t)dt, mk = Tk (t)w(t)dt, k ≥ 1.
−1 2k−1 −1
5
The routine works well even for ω 2 quite close to 1, as is shown by the output
below (displayed only partially) for N=40, om2=.999.
ab =
0 9.682265121100620e+00
0 7.937821421385184e-01
0 1.198676724605757e-01
0 2.270401183698990e-01
0 2.410608787266061e-01
0 2.454285325203698e-01
··· ··················
0 2.499915376529289e-01
0 2.499924312667191e-01
0 2.499932210069769e-01
2.3. Discrete Stieltjes and Lanczos algorithm. Partly in preparation for the
next subsection, we now consider a discrete N-point measure
N
X
dλN (t) = wk δ(t − xk ), wk > 0, (10)
k=1
with the recurrence relation (3). In (12), the πk are the (as yet unknown)
discrete orthogonal polynomials πk ( · ; dλN ). Stieltjes’s Procedure consists in
starting with k = 0 and successively increasing k by 1 until k = n − 1. Thus,
when k = 0, we have π0 = 1, so that α0 can be computed by the top relation in
6
(12) with k = 0 and β0 by β0 = N k=1 wk . With α0 , β0 at hand, we can go into
P
(3) with k = 0 and compute π1 (xk ) for all the support points xk . This then
in turn allows us to reapply (12) with k = 1 and compute α1 and β1 . Going
back to (3) with k = 1, we compute π2 (xk ), whereupon (12) with k = 2 yields
α2 , β2 , etc. In this manner we continue until αn−1 , βn−1 have been computed.
Here n ≤ N.
where xw is the N × 2 array of the support points and weights of the given
discrete measure (10); see Fig. 2.2.
x1 w1
x2 w2
.. ..
. .
xN wN
2.4. Discretization methods. The basic idea, first advanced in [7] and more fully
developed in [9], is very simple: One first approximates the given measure dλ
by a discrete N-point measure,
7
typically by applying some appropriate quadrature scheme. Thereafter, the
desired recurrence coefficients are approximated by those of the discrete mea-
sure,
αk (dλ)
≈ αk (dλN ),
(14)
βk (dλ)
≈ βk (dλN ).
If necessary, the integer N is increased to improve the approximation. For each
N, the approximate recurrence coefficients on the right of (14) are computed
by one of the methods described in §2.3. To come up with a good discretization
(13) that yields fast convergence as N → ∞ may require skill and inventiveness
on the part of the user. But if implemented intelligently, the method is one of
the most effective ones for generating orthogonal polynomials.
It would be very difficult to find a single quadrature scheme that would ad-
equately approximate an integral with respect to the weight function w by a
finite sum. However, by considering w as a 2-component weight function, the
first component consisting of the Chebyshev weight, and the second of a con-
stant weight function, a natural discretization is obtained by applying Gauss-
Chebyshev quadrature to the first component, and Gauss-Legendre quadrature
to the second. Thus, the inner product with respect to the weight function w
is approximated by
Z 1 Z 1
(p, q)w = p(t)q(t)(1 − t2 )−1/2 dt + c p(t)q(t)dt
−1 −1
M M (15)
wkCh p(xCh Ch
wkLp(xLk )q(xLk ),
X X
≈ k )q(xk ) + c
k=1 k=1
where xCh
k , wk
Ch
are the nodes and weights of the M-point Gauss-Chebyshev
quadrature formula, and xLk , wkL those of the M-point Gauss-Legendre quadra-
ture formula. This in effect approximates the measure dλ(t) = w(t)dt by a
discrete N-point measure dλN , where N = 2M. Since M-point Gauss quadra-
ture integrates polynomials of degree 2M − 1 exactly and all inner products
in the Darboux formulae (12) involve polynomials of degree at most 2n − 1,
the choice M = n will insure that αk (dλ) = αk (dλN ) for all k ≤ n − 1, and
8
similarly for the βk . Thus, Stieltjes’s procedure, and therefore also Lanczos’s
algorithm, produces exact results. There is no need to increase N any further.
which may or may not be disjoint. The integral of a polynomial f against the
measure dλ(t) = w(t)dt is then represented somehow in the form
Z b m Z
X bµ
f (t)w(t)dt = fµ (t)wµ (t)dt, (16)
a µ=1 aµ
where in the most general case fµ will differ from f (and in fact may no longer
be a polynomial) and wµ is a positive weight function which, too, may be
different from w. The Multicomponent Discretization Method uses (16) with
f (t) = p(t)q(t) to approximate the inner product (p, q)w by applying an ap-
propriate M-point quadrature rule to each constituent integral on the right of
(16). This yields an approximation dλ ≈ dλN with N = mM. If the given mea-
sure dλ, in addition to the absolutely continuous component, contains also a
discrete p-point component, then the latter is simply added to the (mM)-point
approximation to yield an N-point approximation dλN with N = mM + p.
Using either Stieltjes’s procedure or Lanczos’s algorithm, we then compute the
approximations αk (dλN ), βk (dλN ) of αk (dλ), βk (dλ) for k = 0, 1, . . . , n − 1.
The integer M (and with it N) may be successively increased in an attempt
to obtain sufficient accuracy.
[ab,Mcap,kount]=mcdis(n,eps0,quad,Mmax).
9
The details of the discretization must be specified prior to calling the proce-
dure. They are embodied in the following global parameters:
R1
where β0J = −1 (1 − t)α (1 + t)β dt.
The variables a and b are declared global since they are used in the quadra-
ture routine quadjp.m, which is shown in the next box. Note also the choice
Mmax=n+1, which is legitimate since the discretization parameter M = n yields
exact results.
10
function xw=quadjp(N,mu)
global a b
ab=r jacobi(N,a,b); ab(1,2)=1;
xw=gauss(N,ab);
The integer mu in the routine quadjp (in the present case mu=1) specifies
the muth component interval. The call to gauss(N,ab) generates the N-point
Gaussian quadrature rule for the measure identified via the N×2 array ab of
its recurrence coefficients.
The results can be compared with analytic answers (cf. [11, p. 43]) and are
found to be accurate to all digits shown.
The discretization of the measure dλ(t) = w(t)dt should be done with due
regard to the properties of the weight function, especially its behavior for
11
small and large t. This behavior is determined by
R(t) + I0 (t) ln(1/t)
if 0 < t ≤ 1,
K0 (t) =
t−1/2 e−t S(t)
if 1 ≤ t < ∞,
where I0 is the “regular” modified Bessel function and R, S are smooth func-
tions for which good rational approximations are known [19]. This suggests
the decomposition [0, ∞] = [0, 1] ∪ [0, 1] ∪ [0, ∞] and the representation
Z ∞ Z 1 Z 1
f (t)w(t)dt = [R(t)f (t)]tα dt + [I0 (t)f (t)]tα ln(1/t)dt
0 Z ∞
0 0 (17)
+ e−1 [(1 + t)α−1/2 S(1 + t)f (1 + t)]e−t dt.
0
f3 (t) = e−1 (1 + t)α−1/2 S(1 + t)f (1 + t), w3 (t) = e−t on [0, ∞].
12
Demo#4. Compute
√
∞ π Γ2 (α + 1)
Z
−t α
e t K0 (t)dt = .
0 2α+1 Γ(α + 3/2)
The routine in the box below applies n-point Gauss quadrature of e−t relative
to the weight function w(t) = tα K0 (t) and determines the smallest n for which
the relative error is less than eps0.
>> global a
>> a=-1/2; N=20; Mmax=200; eps0=1e4*eps;
>> exact=sqrt(pi)*(gamma(a+1))^2/(2^(a+1)*gamma(a+3/2));
>> ab=r modbess(N,a,Mmax,eps0); s=0; n=0;
>> while abs(s-exact)>abs(exact)*eps0
n=n+1;
xw=gauss(n,ab);
s=sum(xw(:,2).*exp(-xw(:,1)));
end
>> n, s, abs(s-exact)/abs(exact)
For the choices made of a, N, Mmax, and eps0=2.22×10−12 , the routine yields
n = 12, s = 3.937402486427721, with a relative error of 7.32 × 10−13 .
13
whereupon the factors on the right are interchanged and the shift cI added
back. Discarding the last row and column of the resulting matrix yields the
desired Jacobi matrix of order n,
ab=chri1(N,ab0,c),
Here,
m
(t − xµ )2 ,
Y
r(t) =
µ=1
where xµ are the zeros of πm . This calls for m successive applications of the
routine chri7.m with x = xµ , µ = 1, 2, . . . , m. The routine indop.m shown in
the box below implements this.
14
function ab=indop(N,m,ab0)
N0=size(ab0,1);
if N0<N+m, error(’input array ab0 too short’), end
ab=ab0;
if m==0, return, end
zw=gauss(m,ab0);
for imu=1:m
mi=N+m-imu;
for n=1:mi+1
ab1(n,1)=ab(n,1);
ab1(n,2)=ab(n,2);
end
x=zw(imu,1);
ab=chri7(mi,ab1,x);
end
The routine shown in the next box generates the first 20 recurrence coefficients
of selected induced orthogonal polynomials when dλ is the Legendre measure.
By symmetry, all the α-coefficients are zero. Selected values of the β-coefficients
returned by the routine (rounded to 10 decimal places) are shown in Table
2.2.
k βk,0 βk,2 βk,6 βk,11
0 2.0000000000 0.1777777778 0.0007380787 0.0000007329
1 0.3333333333 0.5238095238 0.5030303030 0.5009523810
6 0.2517482517 0.1650550769 0.2947959861 0.2509913424
12 0.2504347826 0.2467060415 0.2521022519 0.1111727541
19 0.2501732502 0.2214990335 0.2274818789 0.2509466619
The procedure is remarkably stable, not only for the Legendre measure, but
also for other classical measures, and for n and m as large as 320; see [11,
Tables X and XI].
15
3. Sobolev orthogonal polynomials
These are polynomials orthogonal with respect to an inner product that in-
volves derivatives in addition to function values, each derivative having asso-
ciated with it its own (positive) measure. Thus,
Z Z Z
(p, q)S = p(t)q(t)dλ0 (t) + 0 0
p (t)q (t)dλ1 (t) + · · · + p(s) (t)q (s) (t)dλs (t).
R R R
(18)
The Sobolev polynomials {πk ( · ; S)} are monic polynomials of degree k or-
thogonal with respect to the inner product of (18),
(
= 0, k 6= `,
(πk , π` )S (19)
> 0, k = `.
0 0 0 ··· 1 β0n−1
For simplicity, we use the same set of polynomials {pk } for each measure and
assume, as in (8), that they satisfy a three-term recurrence relation. In analogy
to (9), there is now a unique map that takes the first 2n modified moments of
16
all the measures dλσ into the recurrence coefficients βjk ,
(σ)
2n−1
[mk ]k=0 , σ = 0, 1, . . . , s 7→ [βjk ], k = 0, 1, . . . n − 1; j = 0, 1, . . . , k. (23)
The conditioning of this map has been studied in [28], and an algorithm,
analogous to the modified Chebyshev algorithm, developed (for s = 1) in [15].
The corresponding routine in Matlab is
[B,normsq]=chebyshev sob(N,mom,abm).
Here, N is the n in (23), mom the 2×2N array of the first 2N modified moments
corresponding to dλ0 and dλ1 , and abm the (2N −1)×2 array of the recurrence
coefficients in (8). The output variable B is the N ×N matrix of the recurrence
coefficients βjk , k = 0, 1, . . . , N − 1, 0 ≤ j ≤ k, where βjk occupies the position
B(j + 1, k + 1) of the matrix B; all remaining elements of B are zero. The
routine also returns the optional N-vector normsq of the squared norms kπk k2S
of the Sobolev orthogonal polynomials. If abm is absent in the list of input
parameters, then ordinary moments are assumed (ak = bk = 0).
These are the Sobolev orthogonal polynomials with s = 1 and dλ0 (t) = dt,
dλ1 (t) = γdt on [−1, 1], where γ > 0. There is a fairly obvious choice of
the polynomials {pk } for defining the modified moments, namely the monic
Legendre polynomials. All modified moments in this case, by orthogonality,
are zero except for
(0) (1)
m0 = 2, m0 = 2γ.
In Matlab, the recurrence matrix B for the Althammer polynomials is gener-
ated as shown in the box below (where N=n and g=γ).
The routine in the box below generates and plots the Sobolev polynomial of
degree N = 20 corresponding to s = 1 and γ = 0 (Legendre polynomial) resp.
γ = 1 (Althammer polynomial). It is assumed that the matrix B has already
been generated by the routine for Althammer polynomials shown above with
N=20 and g=0 resp. g=1.
17
>> N=20;
>> pi=zeros(N+1,1); np=500; y=zeros(np+1,1);
>> for it=0:np
t=-1+2*it/np;
pi(1)=1;
for k=1:N
temp=0;
for l=1:k
temp=temp+B(l,k)*pi(k-l+1);
end
pi(k+1)=t*pi(k)-temp;
end
y(it+1)=pi(N+1);
end
>> x=linspace(-1,1,np+1);
>> hold on
>> plot(x’,y)
>> plot([-1 1],[0 0],’--’)
>> hold off
The plot for the Legendre polynomial is shown in Fig. 3.1 in the left frame,
and the one for the Althammer polynomial in the right frame.
−6 −6
x 10 x 10
8 3
6 2
4 1
2 0
0 −1
−2 −2
−4 −3
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
Interestingly, for the Legendre polynomial the envelope of the extreme points
is convex on top and concave at the bottom, whereas for the Althammer
polynomial it is the other way around. Note also that π20 (±1) = .7607 × 10−5
for the Legendre, and π20 (±1) = 0 for the Althammer polynomial.
18
3.2. Discretization algorithm. The analogue for Sobolev orthogonal polynomi-
als of the Darboux formulae (12) is
(tπk , πk−j )S
βjk = , j = 0, 1, . . . , k, (24)
(πk−j , πk−j )S
with the inner product ( · , · )S defined as in (18). The Discretized Stieltjes Al-
gorithm, similarly as for ordinary orthogonal polynomials, consists in combin-
ing the formulae (24) with the recurrence relation (20), discretizing the inner
products in (24) by suitable quadrature schemes. We chose to approximate
the absolutely continuous component of each measure dλσ by a Gauss-type
quadrature rule,
nσ
X (σ) (σ) (σ)
(p, q)dλσ ≈ wk p(xk )q(xk ), σ = 0, 1, . . . , s, (25)
k=1
and to add on any discrete component of dλσ if present. In Matlab, the quadra-
ture schemes are identified by an md × 2(s + 1) array xw,
B=stieltjes sob(N,s,nd,xw,a0,same),
The box below shows the generation of the recurrence matrix B for the Al-
thammer polynomials using the routine stieltjes sob.m.
19
>> N=20; g=1;
>> nd=[N N]; s=1; a0=0; same=1;
>> ab=r jacobi(N);
>> zw=gauss(N,ab);
>> xw=[zw(:,1) zw(:,1) zw(:,2) g*zw(:,2)];
>> B=stieltjes sob(N,s,nd,xw,a0,same);
The results are identical with those produced by the routine chebyshev sob.m.
There is no restriction, however, on the parameter s when using the routine
stieltjes sob.m.
3.3. Zeros. If π(t) is the vector of the first n Sobolev orthogonal polynomials,
then the recurrence relation (20) can be written in matrix form as follows,
function z=sobzeros(n,N,B)
H=zeros(n);
for i=1:n
for j=1:n
if i==1
H(i,j)=B(j,j);
elseif j==i-1
H(i,j)=1;
elseif j>=i
H(i,j)=B(j-i+1,j);
end
end
end
z=sort(eig(H));
Here B is the recurrence matrix of order N for the Sobolev orthogonal poly-
nomials, and n≤N. The zeros are arranged in increasing order.
Assuming that the matrix B has already been generated by either the modified
20
Chebyshev algorithm or the Stieltjes procedure as described in §§3.1 and 3.2,
the box below shows the Matlab commands and output (only the positive
zeros are shown, rounded to 12 decimals).
Judging from how well the symmetry of the roots is satisfied, the results appear
to be accurate to all digits shown except the last, which may be in error by one
or two units. Generating the matrix B by the modified Chebyshev algorithm
or Stieltjes’s procedure produces the same results to this accuracy, but the
Stieltjes procedure is considerably slower (by a factor of about 14) than the
modified Chebyshev algorithm.
References
[4] Fischer, Bernd and Golub, Gene H. 1992. How to generate unknown
orthogonal polynomials out of known orthogonal polynomials. J. Comput.
Appl. Math. 43, pp. 99–115.
[5] Freud, Géza 1971. Orthogonal polynomials. Pergamon Press, New York.
(English translation of Orthogonale Polynome, Birkhäuser, Basel, 1969.)
21
[6] Galant, David 1971. An implementation of Christoffel’s theorem in the
theory of orthogonal polynomials. Math. Comp. 25, pp. 111–113.
[12] Gautschi, Walter 2002. The interplay between classical analysis and
(numerical) linear algebra — a tribute to Gene H. Golub. Electron. Trans.
Numer. Anal. 13, pp. 119–147 (electronic).
[14] Gautschi, Walter and Li, Shikang 1993. A set of orthogonal polynomials
induced by a given orthogonal polynomial. Aequationes Math. 46, pp. 174–198.
[18] Lanczos, Cornelius 1950. An iteration method for the solution of the
eigenvalue problem of linear differential and integral operators. J. Research
Nat. Bur. Standards 45, pp. 255–282. Also in Collected Published Papers with
Commentaries, Vol. V, pp. 3-9–3-36.
22
[21] Sack, R. A. and Donovan, A. F. 1972. An algorithm for Gaussian
quadrature given modified moments. Numer. Math. 18, pp. 465–478.
[22] Stahl, Herbert and Totik, Vilmos 1992. General orthogonal polynomials.
Encyclopedia of Mathematics and Its Applications 43, Cambridge University
Press, Cambridge.
[25] Szegö, Gabor 1975. Orthogonal polynomials (Fourth ed.). AMS Colloquium
Publications 23, American Mathematical Society, Providence, RI.
23