0% found this document useful (0 votes)
49 views

Useful Algorithms: 1 Summation

This document summarizes several useful algorithms: - Summation formulas including Euler's summation formula and Bernoulli numbers - Approximations for harmonic numbers, factorials, and the gamma function - Numerical differentiation and integration techniques including the trapezoid rule, midpoint rule, and Simpson's rule - The Newton-Raphson method for solving equations - Numerical solutions to differential equations including Euler's method and Runge-Kutta - Symplectic integrators including the Verlet method, Yoshida methods, Ruth method, and Forest-Ruth method - The Candy-Rozmus and Position Extended Forest Ruth methods for symplectic integration - Recommendations for pseudorandom number generation
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Useful Algorithms: 1 Summation

This document summarizes several useful algorithms: - Summation formulas including Euler's summation formula and Bernoulli numbers - Approximations for harmonic numbers, factorials, and the gamma function - Numerical differentiation and integration techniques including the trapezoid rule, midpoint rule, and Simpson's rule - The Newton-Raphson method for solving equations - Numerical solutions to differential equations including Euler's method and Runge-Kutta - Symplectic integrators including the Verlet method, Yoshida methods, Ruth method, and Forest-Ruth method - The Candy-Rozmus and Position Extended Forest Ruth methods for symplectic integration - Recommendations for pseudorandom number generation
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Useful Algorithms

Fengning Ding November 5, 2011

Summation
zm z = z m! e 1

Dene the Bernoulli numbers Bm as Bm

1 1 Some values include B0 = 1, B1 = 1 2 , B2 = 6 , B3 = 0, B4 = 30 .

Then, Eulers summation formula states that


n1 n m

f (k ) =
k=1 1

f (x)dx +
k=1

Bk f (m1) (n) f (m1) (1) + O k!

1 nm

1.1

Harmonic Numbers
Hn = ln n + + 1 1 1 + 2n 12n2 120n4

with

<

1 256n .

When n > 100, the truncation error is on the same order as round-o error for double precision calculations.

1.2

Factorials and the Gamma Function

The Stirling approximation gives (x + 1) = x! = 2x x e


x

1+

1 1 139 571 + +O 2 3 12x 288x 51840x 2488320x4

1 x5

Dierentiation

We can approximate f (a) by using f (a) f (a + h) f (a h) . 2h 1

This is a second order approximation, compared to the naive f (a) f (a + h) f (a) h

which is a rst order approximation. Similarly, we get a second order approximation for the second derivative: f (a + h) + f (a h) 2f (a) f (a) . h2

Integration
ba . N

For the below algorithms, set h=

3.1

Endpoint Riemman Sums

This is the most crude approximations, and should be avoided.


b N

f (x)dx h
a i=1

f (a + ih) .

The error is on the order of

1 N.

3.2

Trapezoid Rule

Instead of rectangles, we can use trapezoids to estimate the integral.


b a

ba f (x)dx N
1 . N2

N i=1

1 [f (a + (i 1)h) + f (a + ih)] . 2

The error is on the order of

3.3

Midpoint Rule

Alternatively, we could use the midpoint rule.


b N

f (x)dx h
a i=1

a+ i

1 2

h .

The error is on the order of

1 . N2

3.4

Simpsons Rule

Simpsons Rule, which uses parabolic arcs, gives the best general integration algorithm:
b

f (x)dx
a

h 3

f (a + (i 1)h) + 4f
i=1

a+ i

1 2

h + f (a + ih) .

The error is on the order of

1 . N4

This method happens to integrate all degree 3 or less polynomials exactly, but if the degree of the polynomial is known, use the symbolic integral to evaluate the denite integral. Also, the above description of Simpsons rule is not the most ecient. For instance, many of the terms in the above sum are the same, and by rearranging the summation, less operations are required to calculate the sum on the right side of the equation.

Newton-Raphson

This algorithm solves the equation f (x) = 0. 1. Obtain guess 2. guess=guess-f(guess)/f_deriv(guess) 3. If f(guess)>tolerance, go to step 2 Let the solution obtained be r If f (r) = 0, the sequence of guesses converge linearly. If f (r) = 0, the sequence of guesses converge quadratically or better.

Numerical solutions of ordinary dierential equations

Of the below integrators, Eulers method, the improved Eulers method,and the Runge-Kunta 4 method are general integrators. The symplectic integrators solve a Hamiltonian system (such as Newtons second law without nonconservative forces) better, since they conserve energy, momentum, angular momenum, etc., better. This is because symplectic integrators solve a nearby Hamiltonian exactly. All energy losses are due to round-o error. All methods require a step-size, h.

5.1

Eulers Method

This is a rst order nonsymplectic method. 1. Find a(x[i],v[i],t) 2. Set x[i+1]=x[i]+v[i]*h 3. Set v[i+1]=v[i]+a[i]*h.

5.2

Improved Eulers Method

With little extra work, we can increase the accuracy (to order 2) by using 1. Find a(x[i],v[i],t) 2. Set x[i+1]=x[i]+v[i]*h+0.5*a[i]*h*h 3. Set v[i+1]=v[i]+a[i]*h.

5.3

Runge-Kunta 4

This is a fourth order nonsymplectic method. 1. Find a(x[i],v[i],t) 2. Set k1=h*a, j1=h*v 3. Find a(x[i]+0.5*k1,v[i]+0.5*j1,t+0.5*h) 4. Set k2=h*a, j2=h*v 5. Find a(x[i]+0.5*k2,v[i]+0.5*j2,t+0.5*h) 6. Set k3=h*a, j3=h*v 7. Find a(x[i]+k3,v[i]+j3,t+h) 8. Set k4=h*a, j4=h*v 9. Set x[i+1]=x[i]+1/6*(j1+2*j2+2*j3+j4) 10. Set v[i+1]=v[i]+1/6*(k1+2*k2+2*k3+k4).

5.4

Yoshida Methods (and Verlet)

The Yoshida methods are a general set of symplctic integrators of even order. The general outline is 1. Set x=x[i], v=v[i] 2. For i=1 to n-1: 2a. x=x+0.5*h*p[i]*v 2b. Find a(x,v) 2c. v=v+0.5*h*p[i]*a 2d. x=x+0.5*h*p[i]*v 3. x=x+0.5*h*p[n]*v 4. Find a(x,v) 5. v=v+0.5*h*p[n]*a 6. x=x+0.5*h*p[n]*v 7. For i=n-1 to 1: 7a. x=x+0.5*h*p[i]*v 7b. Find a(x,v) 7c. v=v+0.5*h*p[i]*a 7d. x=x+0.5*h*p[i]*v 8. x[i+1]=x 9. v[i+1]=v.

For the Yoshida order 2 method (otherwise known as the Verlet method), n = 1 and p1 = 1. For the Yoshida order 4 method, n = 2 and p1 = 1.351207191959657, p2 = 1.702414383919315. For the Yoshida order 6 method, n = 4 and

p1 = 0.784513610477560 p2 = 0.235573213359357 p3 = 1.17767998417887 p4 = 1.31518632068391. For the Yoshida order 8 method, n = 8 and

p1 = 1.04242620869991 p2 = 1.82020630970714 p3 = 0.157739928123617 p4 = 2.44002732616735 p5 = 0.00716989419708120 p6 = 2.44699182370524 p7 = 1.61582374150097 p8 = 1.7808286265894516.

Ruth Method

This is a third order symplectic integrator. Let p=[2/3,-2/3,1] and q=[7/24,3/4,-1/24] 1. Set x=x[i], v=v[i] 2. For i=1 to 3: 2a. Find a(x,v) 2b. Set v=v+p[i]*a*h 2c. Set x=x+q[i]*v*h 3. Set x[i+1]=x 4. Set v[i+1]=v.

Forest-Ruth

This is a fourth order symplectic integrator. Let f1 = 1.35120719195966, f2 = 1 f1 , f3 = 1 2 f1 .

1. Set x=x[i], v=v[i] 2. Set x=x+f1*v*h/2 3. Find a(x,v) 4. Set v=v+f1*a*h 5. Set x=x+f2*v*h/2 6. Find a(x,v) 7. Set v=v+f3*a*h 8. Set x=x+f2*v*h/2 9. Find a(x,v) 10. Set v[i]=v+f1*a*h 11. Set x[i]=x+f1*v*h/2.

Position Extended Forest Ruth Method

This is another fourth order Forest-Ruth algorithm. Let f1 = 0.1786178958448091, f2 = 0.2123418310626054, f3 = 0.0662645266981849. 1. Set x=x[i], v=v[i] 2. Set x=x+f1*v*h 3. Find a(x,v) 4. Set v=v+(1-2*f2)*a*h/2 5. Set x=x+f3*v*h 6. Find a(x,v) 7. Set v=v+f2*a*h 8. Set x=x+(1-2*f3-2*f1)*v*h 9. Find a(x,v) 10. Set v=v+f2*a*h 11. Set x=x+f3*v*h 12. Find a(x,v) 13. Set v[i+1]=v+(1-2*f2)*a*h/2 14. Set x[i+1]=x+f1*v*h.

Candy Rozmus
1 , 2(221/3 ) 121/3 , 2(221/3 ) 1 , 221/3 2 and q3 = 2 . 21/3
1/3

This is a fourth-order symplectic method. Let p1 = p4 = p2 = p3 = q1 = 0, q2 = q4 =

1. Set x=x[i], v=v[i] 2. For i=1 to 4: 2a. Find a(x,v) 2b. v=v+q[i]*a*h 2c. x=x+p[i]*v*h 6

3. x[i+1]=x 4. v[i+1]=v. We could also optimize this algorithm by setting p1 = 0.5153528374311229364 p2 = 0.085782019412973646 p3 = 0.4415830236164665242 p4 = 0.1288461583653841854 q1 = 0.1344961992774310892 q2 = 0.2248198030794208058 q3 = 0.7563200005156682911 q4 = 0.3340036032863214255.

10

Pseudorandom numbers

ISO-C standards recommends the following algorithm: 1. static rand 2. rand=rand*1103515245+12345 mod (m) 3. return rand/2^16 mod (RAND_MAX+1) for m = 2k so that m 1 is the largest unsigned int.

References
[1] Rodney Dunning, VPNBodySolar-System Dynamics for VPython. http://www.longwood.edu/sta/dunningrb/vpnbody/ [2] Donald Knuth, The Art of Computer Programming, Books 1 and 2. Addison Wesley Longman, 1997, Edition 3.

You might also like