Useful Algorithms: 1 Summation
Useful Algorithms: 1 Summation
Summation
zm z = z m! e 1
f (k ) =
k=1 1
f (x)dx +
k=1
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
1+
1 x5
Dierentiation
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
3.1
f (x)dx h
a i=1
f (a + ih) .
1 N.
3.2
Trapezoid Rule
ba f (x)dx N
1 . N2
N i=1
1 [f (a + (i 1)h) + f (a + ih)] . 2
3.3
Midpoint Rule
f (x)dx h
a i=1
a+ i
1 2
h .
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) .
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.
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
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
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
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
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.
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
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.