Académique Documents
Professionnel Documents
Culture Documents
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
12/7/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.
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;
}
fx = f +
f
f
x +
x 2 +
1!
2!
Fn ( x) =
i =0
n
f (i ) ( )
( x )i = ti ( x)
i!
i =0
tn ( x)
Fn ( x)
12/7/2011
x2
xi
+ =
2!
i=0 i !
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
//===========================================================================
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;
}
12/7/2011
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);
}
a0 +
b1
b
a 1 + a +2
2
b b
b
a0 ; a1 , a2 , , ai ,
1
2
i
b
R1 = a 0 + a 1 , R2 = a 0 +
1
b1
b
a1 + a2
2
A = lim Ri
i
Continued fractions are generally more rapidly converging than other infinite
representations
12/7/2011
Ri =
Pi
, i = 1, 2,3,
Qi
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
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 =
a i+1 P i + b i+1 P i1
P
= i+1
Qi+1
a i+1 Qi + b i+1 Qi1
12/7/2011
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
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;
}
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
General relations
Second order differential equation:
g 2 ( x ) f n ( x) + g1 ( x) f n ( x) + hn f n ( x) = 0
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
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
1
[(bi + ci ) fi 1 ( ) di f i 2 ( )]
ai
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)
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;
}
//===========================================================================
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;
}
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;
}
//===========================================================================
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;
}
11
12/7/2011
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
f, =
a lm Ylm ,
l=0 m =l
12
12/7/2011
1 x 2
1 P ml xP ml xdx =
1
l + m!
2
.
2l + 1 l m! l l
fx =
a lm P ml x.
l=0 m =l
Recurrence relation:
P mm 1 = 0
P mm x = 1 m 2m 1!!1 x 2 m /2
m
2i 1 1 x 2
i=1
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) */
/* recurenta */
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
//===========================================================================
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;
}
15
12/7/2011
ji ( x)
2i 1
ji 1 ( x), i = n, n + 1, , N
x
f =
i=n
2i 1
x
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
17