You are on page 1of 7

(The difficulties of) Computing sin( x ), x

M ATHEMAGICA
Cornell University
March 18, 2014

We are asked to find the polynomial Pn ( x ) of degree n that minimizes


Z 1

min k Pn sin k2 = min

Pn n

Pn n

( Pn ( x ) sin(x ))dx

it turns out that by a famous theorem, suppose we have an orthonormal basis Un = {u0 , u1 , , un }
of polynomial functions (wrt to the k k defined above) where uk is a degree k polynomial, then
Pn Un and furthermore, let
Pn = ck uk
k

then by orthonormality,
ck = huk ( x ), sin(x )i
Now,
irregardless of which set Un you choose, at some point, youre going to need to be able to
evaluate x k , sin(x ) . I thought this was going to be easily done with a quadrature, but it turns
out that x k sin(x ) becomes extremely stiff and all of the conventional quadratures Ive tried have
been unable to evaluate this integral satisfactorily for large values of n. Most immediately holds on
to the solution of a nearby function (by slightly perturbing n)
to go to zero. Im going
R and attempts
n
to now discuss the various unforseen issues of computing x sin( x )dx.

A first attempt
I thought that I was rather clever when I poured through my high school AP calculus text book
and rediscovered integration by parts. I constructed two objects:
Sk =

Z  n
x

Now, consider Sk , we know that

sin( x )dx, and

Ck =

udv = uv

Z  n
x

vdu,

cos( x )dx.

so if we let u =


x k

, du = k
Sn =


x k 1
dx and

Z  n
x

 x n

dv = sin( x )dx, v = cos( x ), then well find that

sin( x )dx
Z


cos( x ) + n

n
= 2[n is odd] + Cn1

n
Cn = 0 Sn1

 x  n 1

cos( x )dx

which means that


Sn =

if n is even
if n = 1
n ( n 1)
S n 2
2

if n is odd

Notice here that if we let

(2n + 1)2n
Sn = 1
Sn1 , S0 = 1
2
then 2Sn = S2n+1 ! This is great because we only need to compute the odd Sn s anyways:
1
2
3
4
5

f u n c t i o n [ Sn ] = Snk ( n )
Sn = 2 ;
for k = 1: n
Sn = [ Sn ; 2 ( 2 k +1) ( 2 k ) /( pi pi ) Sn ( k ) ] ;
end

Figure 1: Uhoh...

Hmmm,
that looksa little suspicious. Recall that Sk is only defined for

 odd k, so we know that
x n
x n
x n
x n
sin
(
x
)
=
sin
(
x
)
.
Furthermore,
since
sin
(
x
)
<
between , , we can

bound this integral by


2
0 2Sk
2k + 1
Figure 2: 2Sn violates the theoretic upper bound!

Whats going on here? To analyze the error, suppose that there was just some error in the
computation of S1 so that we instead got S1 + 3!2 e, then lets look at how this error propagates:


54
3!
5!
0

S2 = 1 2 S1 + 2 e = S2 4 e



7

6
5!
7!
S30 = 1 2 S2 + 4 e = S3 + 6 e

8
S4 = S4 9! e
..
.
Sk0 = Sk (2k + 1)! 2k e
By stirlings approximation, our error is growing at the rate of O



2k +1
e

2k+1 

! This is clearly

unacceptable. We resorted to various techniques to overcome this difficulty: pre-computing a table


of Sn values using big-number packages, quadratures, but these methods all seem to fall short.
3

A second attempt
At this point, it dawned on us that we can transform this into a linear recurrence, which may give
us a more stable method to compute this integral by. Suppose we define
n

Xn =

(2k + 1)!
(2k + 1)2k
= (1)n
2

2k

then
(2n+1)2n
Sn
1
2
S
=

(2k+1)2k n1
n
Xn
Xn
k 2
2n
S
= (1)n
+ n 1
(2n + 1)! Xn1

so if we let Tn =

Sn
Xn ,

then
T0 = 1
Tn = (1)n

2n
+ Tn1
(2n + 1)!

2k

(1)k (2k + 1)!

k =0

Sn =

(1)

+1
(2n 2k)!(22n
(nk))

2( n k )

k =0

Which lends to the code

1
2
3
4
5
6
7
8
9
10
11

f u n c t i o n [ r ] = Snk ( nn )
r = [1];
f o r n = 1 : nn
a = 0;
f o r k = n : 1:0
a = a + ( 1) . ^ k . f a c t o r i a l ( 2 n+1) . / ( pi . ^ ( 2 ( nk ) ) . f a c t o r i a l ( 2 k +1) ) ;
end
r = [ r ; abs ( a ) ] ;
end
r = 2 r ;
end

Unfortunately, if we plot out the error again using

1
2
3

semilogy ( 0 : n , abs ( Sn ( n ) ) , s , 0 : n , 2 . / ( 2 ( 0 : n ) +1) , vr )


xlabel ( n )
legend ( Sn , t h e o r e t i c a l upperbound )

We find once again that this new series is still unstable!

This makes sense since were essentially adding up really large parts in the hope that it will
converge to something really small. Small amounts of relative error is guaranteed to doom our
endeavors as the error gets amplified. At this point, it seems like were doomed to failure :( Most of
us gave up at this point, but a sudden surge of brilliance ended up giving us the nudge that saved
the day.

Third times the charm


While playing on Wolfram|Alpha, one of our members realized that the partial series
n

Tn =

2k+1

(1)k (2k + 1)!

k =0

is just the truncated taylor expansion of sin( x ) evaluated at using just the first n + 1 terms! This
means that
lim Tn = sin( ) = 0
This may look not very important or obvious right now, but it opens up a whole new approach to
this problem. If we consider Tn as the truncated expansion of function x 1 sin( x ) at , then we
can view it in two ways:
1. as a forward series: basically what weve been looking at it as this entire time
5

2. as the residual: in other words, because Tn is the prefix sum of the first n terms of this series,
it is also the entire infinite sum minus the sum of the n + 1 term onwards.
using the second interpretation, we can say that
Tn =

sin( )
2k+1
(1)k

(2k + 1)!
k = n +1

(1)k

k = n +1

2k+1
(2k + 1)!

Sn = Xn Tn
let [nk] = k!(nk) be a short hand for k-permutations of a set of n items

2( k n )
i
(1)n+k h
2k +1
2( k n )

k = n +1





2


h

i
= (1)
2(n+)+1
=1
2

whats remarkable about this series is that unlike the finitary summation version of Sn , this infinite
sum is guaranteed to have monotonically decreasing terms in the series, therefore the series doesnt
diverge as long as we dont use a stupidly many set of terms. In fact, it seems extremely stable.

Figure 3: Stable computation of Sn.

1
2
3
4
5

6
7
8

f u n c t i o n [ r ] = Sn ( nn , k )
r = [1];
f o r n = 1 : nn
d = k: 1:1;
r = [ r ; abs (sum(( 1) . ^ d . pi . ^ ( 2 d ) . / ( f a c t o r i a l ( 2 ( n+d ) +1) . / f a c t o r i a l ( 2 n+1)
) )) ];
end
r = 2 r ;
end

After using an arbitrary precision solver in M ATHEMATICA, I plotted out the error using our
method.

Figure 4: Error of Sn.

this seems pretty nifty. Furthermore, we get the extra comfort in knowing also that


4 2
Sn = 2n+3 O 4 n4
( 2 )

You might also like