252 4.4 Computing π, ln 2 and e The approximations π ≈ 3.1415927, ln 2 ≈ 0.69314718, e ≈ 2.7182818 can be obtained by numerical methods applied to the following initial value problems: (1) (2) (3) 4 , y(0) = 0, π = y(1), 1 + x2 1 , y(0) = 0, ln 2 = y(1), y0 = 1+x y 0 = y, y(0) = 1, e = y(1). y0 = Equations (1)–(3) define the constants π, ln 2 and e through the corresponding initial value problems. The third problem (3) requires a numerical method like RK4, while the other two can be solved using Simpson’s quadrature rule. It is a fact that RK4 reduces to Simpson’s rule for y 0 = F (x), therefore, for simplicity, RK4 can be used for all three problems, ignoring speed issues. It will be seen that the choice of the DE-solver algorithm (e.g., RK4) affects computational accuracy. Computing π = R1 0 4(1 + x2 )−1 dx The easiest method is Simpson’s rule. It can be implemented in virtually every computing environment. The code below works in popular matlabcompatible numerical laboratories. It modifies easily to other computing platforms, such as maple and mathematica. To obtain the answer for π = 3.1415926535897932385 correct to 12 digits, execute the code on the right in Table 10, below the definition of f . R1 Table 10. Numerical integration of 0 4(1 + x2 )−1 dx. Simpson’s rule is applied, using matlab-compatible code. About 50 subdivisions are required. function ans = simp(x0,x1,n,f) h=(x1-x0)/n; ans=0; for i=1:n; ans1=f(x0)+4*f(x0+h/2)+f(x0+h); ans=ans+(h/6)*ans1; x0=x0+h; end function y = f(x) y = 4/(1+x*x); ans=simp(0,1,50,f) It is convenient in some laboratories to display answers with printf or fprintf, in order to show 12 digits. For example, scilab prints 3.1415927 by default, but 3.141592653589800 using printf. The results checked in maple give π ≈ 3.1415926535897932385, accurate to 20 digits, regardless of the actual maple numerical integration 4.4 Computing π, ln 2 and e 253 algorithm chosen (three were possible). The checks are invoked by evalf(X,20) where X is replaced by int(4/(1+x*x),x=0..1). The results for an approximation to π using numerical solvers for differential equations varied considerably from one algorithm to another, although all were accurate to 5 rounded digits. A summary for odepack routines appears in Table 11, obtained from the scilab interface. A selection of routines supported by maple appear in Table 12. Default settings were used with no special attempt to increase accuracy. The Gear routines refer to those in the 1971 textbook [?]. The Livermore stiff solver lsode can be found in reference [?]. The Runge-Kutta routine of order 7-8 called dverk78 appears in the 1991 reference of Enright [?]. The multistep routines of Adams-Moulton and Adams-Bashforth are described in standard numerical analysis texts, such as [?]. Taylor series methods are described in [?]. The Fehlberg variant of RK4 is given in [?]. Table 11. Differential equation numeric solver results for odepack routines, applied to the problem y 0 = 4/(1 + x2 ), y(0) = 0. Exact value of π Runge-Kutta 4 Adams-Moulton lsode Stiff Solver lsode Runge-Kutta-Fehlberg 45 3.1415926535897932385 3.1415926535910 3.1415932355842 3.1415931587318 3.1416249508084 20 10 6 5 4 digits digits digits digits digits Table 12. Differential equation numeric solver results for some maplesupported routines, applied to the problem y 0 = 4/(1 + x2 ), y(0) = 0. Exact value of π Classical RK4 Gear Dverk78 Taylor Series Runge-Kutta-Fehlberg 45 Multistep Gear Lsode stiff solver Computing ln 2 = 3.1415926535897932385 3.141592653589790 3.141592653688446 3.141592653607044 3.141592654 3.141592674191119 3.141591703761340 3.141591733742521 R1 0 dx/(1 20 15 11 11 10 8 7 6 digits digits digits digits digits digits digits digits + x) Like the problem of computing π, the formula for ln 2 arises from the method of Rquadrature applied to y 0 = 1/(1 + x), y(0) = 0. The solution is y(x) = 0x dt/(1 + t). Application of Simpson’s rule with 150 points gives ln 2 ≈ 0.693147180563800, which agrees with the exact value ln 2 = 0.69314718055994530942 through 12 digits. More robust numerical integration algorithms produce the exact answer for ln 2, within the limitations of machine representation of numbers. 254 Differential equation methods, as in the case of computing π, have results accurate to at least 5 digits, as is shown in Tables 13 and 14. Lower order methods such as classical Euler will produce results accurate to three digits or less. Table 13. Differential equation numeric solver results for odepack routines, applied to the problem y 0 = 1/(1 + x), y(0) = 0. Exact value of ln 2 Adams-Moulton lsode Stiff Solver lsode Runge-Kutta 4 Runge-Kutta-Fehlberg 45 0.69314718055994530942 0.69314720834637 0.69314702723982 0.69314718056011 0.69314973055488 20 7 6 11 5 digits digits digits digits digits Table 14. Differential equation numeric solver results for maplesupported routines, applied to the problem y 0 = 1/(1 + x), y(0) = 0. Exact value of ln 2 Classical Euler Classical Heun Classical RK4 Gear Gear Poly-extr Dverk78 Adams-Bashforth Adams-Bashforth-Moulton Taylor Series Runge-Kutta-Fehlberg 45 Lsode stiff solver Rosenbrock stiff solver 0.69314718055994530942 0.6943987430550621 0.6931487430550620 0.6931471805611659 0.6931471805646605 0.6931471805664855 0.6931471805696615 0.6931471793736268 0.6931471806484283 0.6931471806 0.6931481489496502 0.6931470754312113 0.6931473787603164 20 2 5 11 11 11 11 8 10 10 5 7 6 digits digits digits digits digits digits digits digits digits digits digits digits digits Computing e from y 0 = y, y(0) = 1 The initial attack on the problem uses classical RK4 with f (x, y) = y. After 300 steps, classical RK4 finds the correct answer for e to 12 digits: e ≈ 2.71828182846. In Table 15, the details appear of how to accomplish the calculation using matlab-compatible code. Corresponding maple code appears in Table 16 and in Table 17. Additional code for octave and scilab appear in Tables 18 and 19. 4.4 Computing π, ln 2 and e 255 Table 15. Numerical solution of y 0 = y, y(0) = 1. Classical RK4 with 300 subdivisions using matlab-compatible code. function [x,y]=rk4(x0,y0,x1,n,f) x=x0;y=y0;h=(x1-x0)/n; for i=1:n; k1=h*f(x,y); k2=h*f(x+h/2,y+k1/2); k3=h*f(x+h/2,y+k2/2); k4=h*f(x+h,y+k3); y=y+(k1+2*k2+2*k3+k4)/6; x=x+h; end function yp = ff(x,y) yp= y; [x,y]=rk4(0,1,1,300,ff) Table 16. Numerical solution of y 0 = y, y(0) = 1 by maple internal classical RK4 code. de:=diff(y(x),x)=y(x): ic:=y(0)=1: Y:=dsolve({de,ic},y(x), type=numeric,method=classical[rk4]): Y(1); Table 17. Numerical solution of y 0 = y, y(0) = 1 by classical RK4 with 300 subdivisions using maple-compatible code. rk4 := proc(x0,y0,x1,n,f) local x,y,k1,k2,k3,k4,h,i: x=x0: y=y0: h=(x1-x0)/n: for i from 1 to n do k1:=h*f(x,y):k2:=h*f(x+h/2,y+k1/2): k3:=h*f(x+h/2,y+k2/2):k4:=h*f(x+h,y+k3): y:=evalf(y+(k1+2*k2+2*k3+k4)/6,Digits+4): x:=x+h: od: RETURN(y): end: f:=(x,y)->y; rk4(0,1,1,300,f); A matlab m-file "rk4.m" is loaded into scilab-4.0 by getf("rk4.m") . Most scilab code is loaded by using default file extension .sci , e.g., rk4scilab.sci is a scilab file name. This code must obey scilab rules. An example appears below in Table 18. 256 Table 18. Numerical solution of y 0 = y, y(0) = 1 by classical RK4 with 300 subdivisions, using scilab-4.0 code. function [x,y]=rk4sci(x0,y0,x1,n,f) x=x0,y=y0,h=(x1-x0)/n for i=1:n k1=h*f(x,y) k2=h*f(x+h/2,y+k1/2) k3=h*f(x+h/2,y+k2/2) k4=h*f(x+h,y+k3) y=y+(k1+2*k2+2*k3+k4)/6 x=x+h end endfunction function yp = ff(x,y) yp= y endfunction [x,y]=rk4sci(0,1,1,300,ff) The popularity of octave as a free alternative to matlab has kept it alive for a number of years. Writing code for octave is similar to matlab and scilab, however readers are advised to look at sample code supplied with octave before trying complicated projects. In Table 19 can be seen some essential agreements and differences between the languages. Versions of scilab after 4.0 have a matlab to scilab code translator. Table 19. Numerical solution of y 0 = y, y(0) = 1 by classical RK4 with 300 subdivisions using octave-2.1. function [x,y]=rk4oct(x0,y0,x1,n,f) x=x0;y=y0;h=(x1-x0)/n; for i=1:n k1=h*feval(f,x,y); k2=h*feval(f,x+h/2,y+k1/2); k3=h*feval(f,x+h/2,y+k2/2); k4=h*feval(f,x+h,y+k3); y=y+(k1+2*k2+2*k3+k4)/6; x=x+h; endfor endfunction function yp = ff(x,y) yp= y; end [x,y]=rk4oct(0,1,1,300,’ff’) Exercises 4.4 Computing π . Compute π = y(1) from the initial value problem y 0 = 4/(1 + x2 ), y(0) = 0, using the given method. 1. Use the Rectangular integration rule. Determine the number of steps for 5-digit precision. 2. Use the Rectangular integration rule. Determine the number of steps for 8-digit precision. 3. Use the Trapezoidal integration rule. Determine the number of steps for 5-digit precision. 4. Use the Trapezoidal integration 4.4 Computing π, ln 2 and e rule. Determine the number of steps for 8-digit precision. 5. Use classical RK4. Determine the number of steps for 5-digit precision. 257 16. Use numerical workbench assist for RK4. Report the number of digits of precision using system defaults. Computing e. Compute e = y(1) from the initial value problem y 0 = y, 6. Use classical RK4. Determine the y(0) = 1, using the given computer asnumber of steps for 10-digit preci- sist. Report the number of digits of sion. precision using system defaults. 7. Use computer algebra system as- 17. Improved Euler method, sist for RK4. Report the number known as Heun’s method. of digits of precision using system defaults. 18. RK4 method. also 8. Use numerical workbench assist for RK4. Report the number of 19. RKF45 method. digits of precision using system 20. Adams-Moulton method. defaults. Computing ln(2). Compute ln(2) = Stiff Differential Equation. The y(1) from the initial value problem flame propagation equation y 0 = y 0 = 1/(1 + x), y(0) = 0, using the y 2 (1−y) is known to be stiff for initial conditions y(0) = y0 with y0 > 0 and given method. small. Use classical RK4 and then a 9. Use the Rectangular integration stiff solver to compute and plot the sorule. Determine the number of lution y(t) in each case. Expect 3000 steps for 5-digit precision. steps with RK4 versus 100 with a stiff 10. Use the Rectangular integration solver. rule. Determine the number of The exact solution of this equation can steps for 8-digit precision. be expressed in terms of the Lambert function w(u), defined by u = w(x) 11. Use the Trapezoidal integration if and only if ueu = x. For example, rule. Determine the number of y(0) = 0.01 gives steps for 5-digit precision. 1 12. Use the Trapezoidal integration . y(t) = 99−t w (99e )+1 rule. Determine the number of steps for 8-digit precision. See R.M. Corless, G.H. Gonnet, 13. Use classical RK4. Determine the D.E.G. Hare, D.J. Jeffrey, and D.E. number of steps for 5-digit preci- Knuth. “On The Lambert W Function,” Advances in Computational sion. Mathematics 5 (1996): 329-359. 14. Use classical RK4. Determine the number of steps for 10-digit preci- 21. y(0) = 0.01 sion. 22. y(0) = 0.005 15. Use computer algebra system assist for RK4. Report the number 23. y(0) = 0.001 of digits of precision using system 24. y(0) = 0.0001 defaults.