Vous êtes sur la page 1sur 17

12/7/2011

6. Evaluation of functions
Titus Beu
University Babes-Bolyai
Department of Theoretical and Computational Physics
Cluj-Napoca, Romania
Titus Beu 2011

Bibliography
Evaluation of polynomials. Horner's scheme
Evaluation of analytical functions
Continued fractions
Orthogonal polynomials
Spherical harmonics. Associated Legendre functions
Spherical Bessel functions

Titus Beu 2011

12/7/2011

Abramowitz, M. and Stegun, I., Handbook of Mathematical Functions (Dover Publications,


New York, 1972).
Knuth, D.E. , The Art of Computer Programming, vol. 2: Seminumerical Algorithms (AddisonWesley, Reading, MA, 1981).
Acton, F.S. , Numerical Methods That Work (Mathematical Association of America,
Washington, 1990).
Beu, T. A., Numerical Calculus in C, Third Edition
(MicroInformatica Publishing House, Cluj-Napoca, 2004).

Titus Beu 2011

Polynomial of degree n:

P( x) = a0 x n + a1 x n 1 +  + an 1 x + an
Value for x = :
P ( ) = ( ((a0 + a1 ) + a2 ) +  + an 1 ) + an

Horner's scheme:

p0

pi

= a0
= pi 1 + ai , i = 1, 2, , n

P( ) = pn
Any rational fraction, R(x) = P(x)/Q(x), can be evaluated using Horner's scheme.

Titus Beu 2011

12/7/2011

//===========================================================================
float Polynomial(float x, float a[], int n)
//--------------------------------------------------------------------------Evaluate polynomial of order n with real coefficients a[] at point x
//--------------------------------------------------------------------------{
float p;
int i;
p = a[0];
for (i=1; i<=n; i++) p = p*x + a[i];
return p;
}

Titus Beu 2011

f(x) is called analytical in if in some neighbourhood |x | < R:

fx = f +

f
f
x +
x 2 +
1!
2!

Taylor polynomial (partial sum) of order n:


n

Fn ( x) =
i =0

n
f (i ) ( )
( x )i = ti ( x)
i!
i =0

Error estimates (absolute and relative):


( n ) =| tn ( x ) |, ( n ) =

tn ( x)
Fn ( x)

Convergence criterion for the relative error estimate:


tn ( x)
or | tn ( x) | | Fn ( x) |
Fn ( x )

Titus Beu 2011

12/7/2011

Example exponential functions:


ex = 1 + x +

x2
xi
+ =
2!
i=0 i !

Taylor polynomial (partial sum) of order n:


n

Fn ( x) = ti ( x ), ti ( x ) =
i=0

xi
i!

Recurrent scheme:

t 0 = 1,

F0 = 1

t i = x/i t i1 , F i = F i1 + t i , i = 1, 2, , n,
Convergence criterion for the relative error estimate:
tn
or | tn | | Fn |
Fn

Titus Beu 2011

//===========================================================================
float Exp0(float x)
//--------------------------------------------------------------------------Evaluates exponential for argument x from its series expansion
//--------------------------------------------------------------------------{
const float eps = 1e-6;
float f, t;
int i;
i = 1;
f = t = 1.0;
while (fabs(t/f) > eps) {
t *= x/(i++);
f += t;
}
return f;
}

Titus Beu 2011

12/7/2011

x < 0 alternating positive and negative terms accumulation of errors by subtraction


Convenient definition:

e| x|
e x = | x|
1/ e

if
if

x0
x<0

//===========================================================================
float Exp(float x)
//--------------------------------------------------------------------------Evaluates exponential for argument x from its series expansion
//--------------------------------------------------------------------------{
const float eps = 1e-6;
float absx, f, t;
int i;
i = 1;
f = t = 1.0;
absx = fabs(x);
while (fabs(t/f) > eps) {
t *= absx/(i++);
f += t;
}
return (x >= 0.0 ? f : 1.0/f);
}

Titus Beu 2011

Continued fraction expression of the form:

a0 +

b1
b
a 1 + a +2
2

b b
b
a0 ; a1 , a2 , , ai ,
1
2
i

where ai and bi can be real numbers, complex numbers or functions.


Convergents of a continued fraction:

b
R1 = a 0 + a 1 , R2 = a 0 +
1

b1
b
a1 + a2
2

A continued fraction is convergent if the limit exists:

A = lim Ri
i

Continued fractions are generally more rapidly converging than other infinite
representations

Titus Beu 2011

12/7/2011

Law of formation of convergents


The convergents of a continued fraction can be expressed as fractions

Ri =

Pi
, i = 1, 2,3,
Qi

with the numerator and denominator given by

Pi = ai Pi 1 + bi Pi 2
Qi = ai Qi 1 + bi Qi 2
and the starting values:

P0 = a0 ,

P1 = 1

Q0 = 1,

Q1 = 0

Titus Beu 2011

Proof (by induction)


For i = 1:

R1 =

P1
a P + b 1 P 1
b
= 1 0
= a0 + a1
1
Q1
a 1 Q0 + b 1 Q1

As per hypothesis:
Ri =

Pi
a P +bP
= i i 1 i i 2
Qi ai Qi 1 + biQi 2

Ri+1 results from Ri by replacing ai with ai + bi+1/ai+1

Ri+1 =

a i+1 P i + b i+1 P i1
P
= i+1
Qi+1
a i+1 Qi + b i+1 Qi1

Titus Beu 2011

12/7/2011

Example tangent function


Continued fraction representation:

x x2 x2

x2
tan x = 0; ,
,
, ,
,
5
2i 1
1 3
Coefficients:

a 0 = 0, a 1 = 1,

a i = a i1 + 2

b 1 = x, b i = x , i = 2, 3,
2

Initial values:
P0 = 0, P1 = x, a1 = 1
Q0 = 1, Q1 = 1, b = x 2

Recurrence relations:
ai = ai 1 + 2, i = 2,3,
Pi = ai Pi 1 + bPi 2
Qi = ai Qi 1 + bQi 2
Titus Beu 2011

Example tangent function


Convergence criterion:
1

Ri 1
P Q
= 1 i 1 i
Ri
Qi 1 Pi

//===========================================================================
float Tan(float x)
//--------------------------------------------------------------------------Evaluates the tangent from its continued fraction representation
//--------------------------------------------------------------------------{
const float eps = 1e-6;
float a, b, p, pm, pp, q, qm, qp;
if (x == 0.0) return 0.0;
pm = 0.0; p = x ; a = 1.0;
qm = 1.0; q = 1.0; b = -x*x;
while (fabs(1.0 - (pm*q)/(p*qm)) > eps) {
a += 2.0;
pp = a*p + b*pm; pm = p; p = pp;
qp = a*q + b*qm; qm = q; q = qp;
}
return p/q;
}

Titus Beu 2011

12/7/2011

{fn(x)} orthogonal system on [a,b] with respect to the weight function w(x) > 0 if
b

w( x ) f
a

( x) f m ( x )dx = N n nm (n, m = 0,1, 2,)

General relations
Second order differential equation:
g 2 ( x ) f n ( x) + g1 ( x) f n ( x) + hn f n ( x) = 0

Recurrence relation with respect to the order:


an f n ( x) = (bn + cn x) f n 1 ( x ) d n f n 2 ( x)

Functional form for first derivative:


g 2 ( x ) f n ( x ) = g1 ( x ) f n ( x) + g 0 ( x) f n 1 ( x)
Titus Beu 2011

Name

fn(x)

w(x)

Nn

Chebyshev
1st kind

Tn(x)

(1 x2)1/2

if n = 0
/2 if n 0

Legendre

Pn(x)

2/(2n + 1)

Laguerre

Ln(x)

ex

ex2

1/22nn!

Hermite

Hn(x)

fn(x)

an

bn

cn

dn

f0(x)

f1(x)

Tn(x)

Pn(x)

2n 1

n1

Ln(x)

2n 1

n1

1x

Hn(x)

2(n 1)

2x

Titus Beu 2011

12/7/2011

fn(x)

g2(x)

g1(x)

g0(x)

Tn(x)

x2

nx

Pn(x)

1 x2

nx

Ln(x)

Hn(x)

2n

Evaluation of orthogonal polynomial fn(x) for x =


1. One evaluates f0() and f1()
2. One applies for i = 2,3,4,... the recurrence relation:
f i ( ) =

1
[(bi + ci ) fi 1 ( ) di f i 2 ( )]
ai

Titus Beu 2011

Cheybyshev polynomials of 1st kind :


Converge much faster than any other
series expansion on [-1,1]
Recurrence relations:

T0 ( x) = 1, T1 ( x) = x
Ti ( x) = 2 xTi 1 ( x) Ti 2 ( x ), i = 2,3, , n
Tn ( x) = n [ xTn ( x ) Tn 1 ( x )] ( x 2 1)

Titus Beu 2011

12/7/2011

//===========================================================================
float Chebyshev(int n, float x, float *d)
//--------------------------------------------------------------------------Evaluates the Chebyshev polynomial of 1st kind and its first derivative (*d)
//--------------------------------------------------------------------------{
float f, fm1, fm2, x2;
int i;
if (n == 0) {
f = 1.0; *d = 0.0;
} else {
f = x; fm1 = 1.0; x2 = 2*x;
for (i=2; i<=n; i++) {
fm2 = fm1; fm1 = f;
f = x2*fm1 - fm2;
}
*d = (x*x-1.0) ? n*(x*f-fm1)/(x*x-1.0) : n*n*f/x;
}
return f;
}

Titus Beu 2011

//===========================================================================
float Legendre(int n, float x, float *d)
//--------------------------------------------------------------------------Evaluates the Legendre polynomial and its first derivative (*d)
//--------------------------------------------------------------------------{
float f, fm1, fm2;
int i;
if (n == 0) {
f = 1.0; *d = 0.0;
} else {
f = x; fm1 = 1.0;
for (i=2; i<=n; i++) {
fm2 = fm1; fm1 = f;
f = ((2*i-1)*x*fm1 - (i-1)*fm2)/i;
}
*d = (x*x-1.0) ? n*(x*f-fm1)/(x*x-1.0) : 0.5*n*(n+1)*f/x;
}
return f;
}

Titus Beu 2011

10

12/7/2011

//===========================================================================
float Laguerre(int n, float x, float *d)
//--------------------------------------------------------------------------Evaluates the Laguerre polynomial and its first derivative (*d)
//--------------------------------------------------------------------------{
float f, fm1, fm2;
int i;
if (n == 0) {
f = 1.0; *d = 0.0;
} else {
f = 1.0 - x; fm1 = 1.0;
for (i=2; i<=n; i++) {
fm2 = fm1; fm1 = f;
f = ((2*i-1-x)*fm1 - (i-1)*fm2)/i;
}
*d = x ? n*(f-fm1)/x : -n*f;
}
return f;
}

Titus Beu 2011

//===========================================================================
float Hermite(int n, float x, float *d)
//--------------------------------------------------------------------------Evaluates the Hermite polynomial and its first derivative (*d)
//--------------------------------------------------------------------------{
float f, fm1, fm2, x2;
int i;
if (n == 0) {
f = 1.0; *d = 0.0;
} else {
f = 2*x; fm1 = 1.0; x2 = 2*x;
for (i=2; i<=n; i++) {
fm2 = fm1; fm1 = f;
f = x2*fm1 - 2*(i-1)*fm2;
}
*d = 2*n*fm1;
}
return f;
}

Titus Beu 2011

11

12/7/2011

Separation of variables for certain differential equations in spherical coordinates


wave equation, Schrdinger's equation, Laplace's equation
Equation of spherical harmonics:

1 sin Ylm
sin

2
1 Ylm + ll + 1Y = 0,
lm
2
sin 2

l = 0, 1, 2, ,

l m l

Spherical harmonics:

Ylm , =

2l + 1 l m! P m cos e im
4 l + m! l

Yl,m , = 1 m Ylm , , m 0
Orthonormation relation:

d Yl m , Ylm , sind = l l m m
0

Titus Beu 2011

{Ylm(,)} complete othonormal basis set on the unity sphere L2(S1)


Any f L2(S1) can be uniquely expanded as a Fourier series:

f, =

a lm Ylm ,
l=0 m =l

Titus Beu 2011

12

12/7/2011

Equation of associated Legendre functions:


2
d 2 P ml
dP m
2x l + ll + 1 m 2 P ml = 0
2
dx
dx
1x
Associated Legendre functions :
m
P ml x = 1 m 1 x 2 m /2 d m P l x.
dx
Orthonormation relation:

1 x 2

1 P ml xP ml xdx =
1

l + m!
2
.
2l + 1 l m! l l

{Plm(x)} complete othonormal basis set on L2([-1,1])


Any f L2([-1,1]) can be uniquely expanded as a Fourier series:

fx =

a lm P ml x.
l=0 m =l

Titus Beu 2011

Recurrence relation:

i mP mi x = 2i 1xP mi1 x i + m 1P mi2 x,


i = m + 1, , l
Starting values:

P mm 1 = 0
P mm x = 1 m 2m 1!!1 x 2 m /2
m

2i 1 1 x 2

i=1

Titus Beu 2011

13

12/7/2011

//===========================================================================
float FLegendre(int l, int m, float x)
//--------------------------------------------------------------------------Evaluates the associated Legendre function of orders l and m
//--------------------------------------------------------------------------{
float p, pm1, pm2, sqx;
int i;
if (l < m) return 0.0;
p = 1.0; pm1 = 0.0;
if (m) {
sqx = -sqrt(1.0-x*x);
for (i=1; i<=m; i++) p *= (2*i-1) * sqx;
}

/* P(m,m,x), P(m-1,m,x) */

for (i=m+1; i<=l; i++) {


pm2 = pm1; pm1 = p;
p = ((2*i-1)*x*pm1 - (i+m-1)*pm2)/(i-m);
}
return p;

/* recurenta */

Titus Beu 2011

Solutions of the radial quantum equations of scattering


Differential equation:
x 2 f n x + 2xf n x + x 2 nn + 1f n x = 0,

n = 0, 1, 2,

Particular solutions:
Spherical Bessel functions (1st kind), jn(x) regular
Spherical Neumann functions (2nd kind), yn(x) non-regular
Particular orders:
sin x
,
x
1
j1 ( x) = [ j0 ( x) cos x ] ,
x
Recurrence relation:
j0 ( x) =

y0 ( x) =
y1 ( x) =

cos x
,
x

1
[ y0 ( x) sin x]
x

f i x = 2i x 1 f i1 x f i2 x
Titus Beu 2011

14

12/7/2011

Spherical Bessel functions (1st kind)


regular solution

Spherical Neumannfunctions (2nd kind)


non-regular solution

Titus Beu 2011

Spherical Neumann functions stable ascending recurrence

//===========================================================================
float SBessy(int n, float x)
//--------------------------------------------------------------------------Returns the spherical Neumann function y_n(x)
//--------------------------------------------------------------------------{
float y, y0, y1;
int i;
y0 = -cos(x)/x; if (n == 0) return y0;
y1 = (y0 - sin(x))/x; if (n == 1) return y1;
for (i=2; i<=n; i++) {
y = (2*i-1)/x*y1 - y0;
y0 = y1; y1 = y;
}
return y;
}

Titus Beu 2011

15

12/7/2011

Spherical Bessel functions conditionally stable ascending recurrence


For n > |x|, the truncation errors accumulate for ascending recurrence
Millers algorithm descending recurrence with arbitrary starting values
Ideea homogeneous differential equation solutions determined up to factor
jn(x) requested function consider an order N > n and the descending recurrence:

jN + 2 = 0, jN +1 = 1, N > n


ji ( x) = 2i + 3 ji +1 ( x) ji + 2 ( x), i = N , N 1, , 0
x
New functions are proportional to the genuine ones:
j n = kj n

Factor determined from n = 0:


j n = j 0 /j 0 j n

Titus Beu 2011

For n < |x| ascending recurrence is always stable


Under which conditions descending recurrence has to be employed?
Which is the order N from which descending recurrence has to be started?
For n > |x| only the first term in the recurrence relation is maintained:

ji ( x) 

2i 1
ji 1 ( x), i = n, n + 1, , N
x

Net increase factor of jN with respect to jn:


N

f =
i=n

2i 1
x

Criterion: if f > 108 descending recurrence ascending recurrence is unstable

Titus Beu 2011

16

12/7/2011

//===========================================================================
float SBessj(int n, float x)
//--------------------------------------------------------------------------Returns the spherical Besseln function y_n(x)
//--------------------------------------------------------------------------{
float j, j0, j1, j2, jn;
int i, nmax;
if (x == 0.0) return (n == 0 ? 1.0 : 0.0);
j0 = sin(x)/x; if (n == 0) return j0;
j1 = (j0 - cos(x))/x; if (n == 1) return j1;
nmax = 0;
// determine direction of stable recurrence
if ((float)n >= fabs(x)) {
jn = 1.;
for (i=n; i<=(n+50); i++) {
jn *= (2*i-1)/x;
// net increase factor
if (jn >= 1e8) {nmax = i + 10; break;}
}
}
if (nmax == 0) {
for (i=2; i<=n; i++) {
j = (2*i-1)/x*j1 - j0;
j0 = j1; j1 = j;
}
return j;
} else {
j2 = 0.; j1 = 1e-20;
for (i=nmax; i>=0; i--) {
j = (2*i+3)/x*j1 - j2;
j2 = j1; j1 = j;
if (i == n) jn = j;
}
return (j0/j)*jn;
}

// ascending recurrence

// descending recurrence

// unnormalized jn
// normalize jn

Titus Beu 2011

17

Vous aimerez peut-être aussi