772 Chapter 17.Two Point Boundary Value Problems for (j-jz1;j<-jz2;j++){ Loop over columns to be zeroed. for(1=jm1:1<=jm2:1+) Loop over columns altered. vx-c[ic][l+loff][kc]; for(i=1z1;i<=1z2;i+)s[1][1]-=s[1][j]*vx; Loop over rows. vx=c[ic][jcf][kc]; for(i=iz1;1<=iz2;1++)s[i][jmf]-=s[1][j]*vx; Plus final element. 1c+=1; "Algebraically Difficult"Sets of Differential Equations 8= Relaxation methods allow you to take advantage of an additional opportunity that,while not obvious,can speed up some calculations enormously.It is not necessary that the set of variables y.correspond exactly with the dependent variables of the original differential equations.They can be related to those variables through algebraic equations.Obviously,it is necessary only that the solution variables allow us to evaluate the functions y,g,B,C that are used to construct the FDEs from the ODEs.In some problems g depends on functions of y that are known only implicitly,so that iterative solutions are necessary to evaluate functions in the ODEs.Often one can dispense with this "internal"nonlinear problem by defining a new set of variables from which both y,g and the boundary conditions can be obtained directly.A typical example occurs in physical problems where the equations require solution of a complex equation of state that can be expressed in more convenient terms using variables s是%ad的 2 Press. other than the original dependent variables in the ODE.While this approach is analogous to performing an analytic change of variables directly on the original ODEs,such an analytic transformation might be prohibitively complicated.The change of variables in the relaxation method is easy and requires no analytic manipulations. SCIENTIFIC CITED REFERENCES AND FURTHER READING: Eggleton,P.P.1971,Monthly Notices of the Royal Astronomical Society,vol.151,pp.351-364.[1] 6 Keller,H.B.1968,Numerical Methods for Two-Point Boundary-Value Problems (Waltham,MA: Blaisdell). Kippenhan,R.,Weigert,A.,and Hofmeister,E.1968,in Methods in Computational Physics, vol.7 (New York:Academic Press),pp.129ff. Numerica 10621 17.4 A Worked Example:Spheroidal Harmonics 431 Recipes The best way to understand the algorithms of the previous sections is to see (outside them employed to solve an actual problem.As a sample problem,we have selected the computation of spheroidal harmonics.(The more common name is spheroidal North angle functions,but we prefer the explicit reminder of the kinship with spherical harmonics.)We will show how to find spheroidal harmonics,first by the method of relaxation ($17.3),and then by the methods of shooting (817.1)and shooting to a fitting point ($17.2). Spheroidal harmonics typically arise when certain partial differential equations are solved by separation of variables in spheroidal coordinates.They satisfy the following differential equation on the interval-1 <x<1: a-+(-x-)s=0 (17.4.1)
772 Chapter 17. Two Point Boundary Value Problems Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). for (j=jz1;j<=jz2;j++) { Loop over columns to be zeroed. for (l=jm1;l<=jm2;l++) { Loop over columns altered. vx=c[ic][l+loff][kc]; for (i=iz1;i<=iz2;i++) s[i][l] -= s[i][j]*vx; Loop over rows. } vx=c[ic][jcf][kc]; for (i=iz1;i<=iz2;i++) s[i][jmf] -= s[i][j]*vx; Plus final element. ic += 1; } } “Algebraically Difficult” Sets of Differential Equations Relaxation methods allow you to take advantage of an additional opportunity that, while not obvious, can speed up some calculations enormously. It is not necessary that the set of variables yj,k correspond exactly with the dependent variables of the original differential equations. They can be related to those variables through algebraic equations. Obviously, it is necessary only that the solution variables allow us to evaluate the functions y, g,B, C that are used to construct the FDEs from the ODEs. In some problems g depends on functions of y that are known only implicitly, so that iterative solutions are necessary to evaluate functions in the ODEs. Often one can dispense with this “internal” nonlinear problem by defining a new set of variables from which both y, g and the boundary conditions can be obtained directly. A typical example occurs in physical problems where the equations require solution of a complex equation of state that can be expressed in more convenient terms using variables other than the original dependent variables in the ODE. While this approach is analogous to performing an analytic change of variables directly on the original ODEs, such an analytic transformation might be prohibitively complicated. The change of variables in the relaxation method is easy and requires no analytic manipulations. CITED REFERENCES AND FURTHER READING: Eggleton, P.P. 1971, Monthly Notices of the Royal Astronomical Society, vol. 151, pp. 351–364. [1] Keller, H.B. 1968, Numerical Methods for Two-Point Boundary-Value Problems (Waltham, MA: Blaisdell). Kippenhan, R., Weigert, A., and Hofmeister, E. 1968, in Methods in Computational Physics, vol. 7 (New York: Academic Press), pp. 129ff. 17.4 A Worked Example: Spheroidal Harmonics The best way to understand the algorithms of the previous sections is to see them employed to solve an actual problem. As a sample problem, we have selected the computation of spheroidal harmonics. (The more common name is spheroidal angle functions, but we prefer the explicit reminder of the kinship with spherical harmonics.) We will show how to find spheroidal harmonics, first by the method of relaxation (§17.3), and then by the methods of shooting (§17.1) and shooting to a fitting point (§17.2). Spheroidal harmonics typically arise when certain partial differential equations are solved by separation of variables in spheroidal coordinates. They satisfy the following differential equation on the interval −1 ≤ x ≤ 1: d dx (1 − x2) dS dx + λ − c2x2 − m2 1 − x2 S =0 (17.4.1)
17.4 A Worked Example:Spheroidal Harmonics 773 Here m is an integer,c is the"oblateness parameter,"and A is the eigenvalue.Despite the notation,c2 can be positive or negative.For c2>0 the functions are called “prolate,.”while if c2<0 they are called“oblate.”The equation has singular points at x=+1 and is to be solved subject to the boundary conditions that the solution be regular at x =+1.Only for certain values of A,the eigenvalues,will this be possible. If we consider first the spherical case,where c=0.we recognize the differential equation for Legendre functions P().In this case the eigenvalues are Amn- n(n +1),n =m,m+1,....The integer n labels successive eigenvalues for fixed m:When n =m we have the lowest eigenvalue,and the corresponding eigenfunction has no nodes in the interval-1<x<1;when n =m+1 we have 8 the next eigenvalue,and the eigenfunction has one node inside(-1,1);and so on. A similar situation holds for the general case c20.We write the eigenvalues of (17.4.1)as Amn(c)and the eigenfunctions as Smn(r;c).For fixed m,n m,m +1,...labels the successive eigenvalues. ICAL The computation ofmn(c)and Smn(;c)traditionally has been quite difficult. Complicated recurrence relations,power series expansions,etc.,can be found in references [1-31.Cheap computing makes evaluation by direct solution of the RECIPES differential equation quite feasible. The first step is to investigate the behavior of the solution near the singular 9 points z =+1.Substituting a power series expansion of the form S=(1±x)ak(1士x) (17.4.2) 起g合9 9 k=0 in equation (17.4.1),we find that the regular solution has a =m/2.(Without loss of generality we can take m >0 since m --m is a symmetry of the equation.) We get an equation that is numerically more tractable if we factor out this behavior. Accordingly we set S=(1-x2)m/2y (17.4.3) We then find from(17.4.1)that y satisfies the equation Numerica 10621 (1-x2) 2-2m+1)z Py dx +(4-c2x2)y=0 (17.4.4) where μ≡入-m(m+1) (17.4.5) Both equations (17.4.1)and (17.4.4)are invariant under the replacement --Thus the functions S and y must also be invariant,except possibly for an overall scale factor.(Since the equations are linear,a constant multiple of a solution is also a solution.Because the solutions will be normalized,the scale factor can only be +1.Ifn-m is odd,there are an odd number ofzeros in the interval (-1,1). Thus we must choose the antisymmetric solution y(-)=-y(x)which has a zero at =0.Conversely,if n-m is even we must have the symmetric solution.Thus ymn(-x)=(-1)-mymn(x) (17.4.6
17.4 A Worked Example: Spheroidal Harmonics 773 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Here m is an integer, c is the “oblateness parameter,” and λ is the eigenvalue. Despite the notation, c2 can be positive or negative. For c2 > 0 the functions are called “prolate,” while if c2 < 0 they are called “oblate.” The equation has singular points at x = ±1 and is to be solved subject to the boundary conditions that the solution be regular at x = ±1. Only for certain values of λ, the eigenvalues, will this be possible. If we consider first the spherical case, where c = 0, we recognize the differential equation for Legendre functions P m n (x). In this case the eigenvalues are λmn = n(n + 1), n = m, m + 1,... . The integer n labels successive eigenvalues for fixed m: When n = m we have the lowest eigenvalue, and the corresponding eigenfunction has no nodes in the interval −1 <x< 1; when n = m + 1 we have the next eigenvalue, and the eigenfunction has one node inside (−1, 1); and so on. A similar situation holds for the general case c2 = 0. We write the eigenvalues of (17.4.1) as λmn(c) and the eigenfunctions as Smn(x; c). For fixed m, n = m, m + 1,... labels the successive eigenvalues. The computation of λmn(c) and Smn(x; c) traditionally has been quite difficult. Complicated recurrence relations, power series expansions, etc., can be found in references [1-3]. Cheap computing makes evaluation by direct solution of the differential equation quite feasible. The first step is to investigate the behavior of the solution near the singular points x = ±1. Substituting a power series expansion of the form S = (1 ± x) α∞ k=0 ak(1 ± x) k (17.4.2) in equation (17.4.1), we find that the regular solution has α = m/2. (Without loss of generality we can take m ≥ 0 since m → −m is a symmetry of the equation.) We get an equation that is numerically more tractable if we factor out this behavior. Accordingly we set S = (1 − x2) m/2y (17.4.3) We then find from (17.4.1) that y satisfies the equation (1 − x2) d2y dx2 − 2(m + 1)x dy dx + (µ − c2x2)y =0 (17.4.4) where µ ≡ λ − m(m + 1) (17.4.5) Both equations (17.4.1) and (17.4.4) are invariant under the replacement x → −x. Thus the functions S and y must also be invariant, except possibly for an overall scale factor. (Since the equations are linear, a constant multiple of a solution is also a solution.) Because the solutions will be normalized, the scale factor can only be ±1. If n− m is odd, there are an odd number of zeros in the interval (−1, 1). Thus we must choose the antisymmetric solution y(−x) = −y(x) which has a zero at x = 0. Conversely, if n − m is even we must have the symmetric solution. Thus ymn(−x)=(−1)n−mymn(x) (17.4.6)
774 Chapter 17.Two Point Boundary Value Problems and similarly for Smn The boundary conditions on (17.4.4)require that y be regular at x=+1.In other words,near the endpoints the solution takes the form y=a0+a1(1-x2)+a2(1-x2)2+. (17.4.7) Substituting this expansion in equation(17.4.4)and letting x-1,we find that 4-c2 a1=-4m+0 (17.4.8) Equivalently, 离 0-0 (17.4.9) A similar equation holds at z=-1 with a minus sign on the right-hand side. RECIPES I The irregular solution has a different relation between function and derivative at the endpoints. Instead of integrating the equation from-1 to 1,we can exploit the symmetry Press. (17.4.6)to integrate from 0 to 1.The boundary condition at x=0 is R y(0)=0,n-m odd Progra (0)=0,n-m even (17.4.10) OF SCIENTIFIC( A third boundary condition comes from the fact that any constant multiple of a solution y is a solution.We can thus normalize the solution.We adopt the normalization that the function Smn has the same limiting behavior as Pm at =1: lim(1-22)-m/2Smn(;c)=lim(1-22)-m/2p() (17.4.11) 10621 Various normalization conventions in the literature are tabulated by Flammer [1]. Numerical Imposing three boundary conditions for the second-order equation(17.4.4) 431 turns it into an eigenvalue problem for A or equivalently for u.We write it in the Recipes standard form by setting 1=y (17.4.12) 2=y (17.4.13) 3=4 (17.4.14) Then =2 (17.4.15) 1 =12 2=(m+1)v2-(u3-)] (17.4.16) 5=0 (17.4.17)
774 Chapter 17. Two Point Boundary Value Problems Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). and similarly for Smn. The boundary conditions on (17.4.4) require that y be regular at x = ±1. In other words, near the endpoints the solution takes the form y = a0 + a1(1 − x2) + a2(1 − x2) 2 + ... (17.4.7) Substituting this expansion in equation (17.4.4) and letting x → 1, we find that a1 = − µ − c2 4(m + 1)a0 (17.4.8) Equivalently, y (1) = µ − c2 2(m + 1)y(1) (17.4.9) A similar equation holds at x = −1 with a minus sign on the right-hand side. The irregular solution has a different relation between function and derivative at the endpoints. Instead of integrating the equation from −1 to 1, we can exploit the symmetry (17.4.6) to integrate from 0 to 1. The boundary condition at x = 0 is y(0) = 0, n − m odd y (0) = 0, n − m even (17.4.10) A third boundary condition comes from the fact that any constant multiple of a solution y is a solution. We can thus normalize the solution. We adopt the normalization that the function Smn has the same limiting behavior as P m n at x = 1: limx→1 (1 − x2) −m/2Smn(x; c) = limx→1 (1 − x2) −m/2P m n (x) (17.4.11) Various normalization conventions in the literature are tabulated by Flammer [1]. Imposing three boundary conditions for the second-order equation (17.4.4) turns it into an eigenvalue problem for λ or equivalently for µ. We write it in the standard form by setting y1 = y (17.4.12) y2 = y (17.4.13) y3 = µ (17.4.14) Then y 1 = y2 (17.4.15) y 2 = 1 1 − x2 2x(m + 1)y2 − (y3 − c2x2)y1 (17.4.16) y 3 =0 (17.4.17)
17.4 A Worked Example:Spheroidal Harmonics 775 The boundary condition at z=0 in this notation is y1=0,n-m odd (17.4.18) y2 =0,n-m even At x =1 we have two conditions: 2 3-c2 2(m+1) (17.4.19) h-1im(1-x2)m/2P(a)= (-1)m(m+m)! 2mm!(n-m)! 三Y (17.4.20) We are now ready to illustrate the use of the methods of previous sections on this problem. Relaxation g经 If we just want a few isolated values of A or S,shooting is probably the quickest University 2 method.However,if we want values for a large sequence of values of c,relaxation is better.Relaxation rewards a good initial guess with rapid convergence,and the Press. THE previous solution should be a good initial guess if c is changed only slightly. For simplicity,we choose a uniform grid on the interval 0 <z<1.For a total of M mesh points,we have Program 1 h=M-1 (17.4.21) OF SCIENTIFIC( xk=(k-1)h, k=1,2,.,M (17.4.22) At interior points k =2,3,...,M,equation (17.4.15)gives COMPUTING (ISBN h 19200 E1.k =y1.k-y1.k-1-v2k+y2k-1) (17.4.23) 10-521 Equation (17.4.16)gives 43106 E2.k=2,k-2,k-1-Bk [k+k-m+12k+2k-1-ank+1k- (17.4.24) (outside + North Software. where a4=%k+3-1-2(%+xk-1)2 (17.4.25) 2 4 h 脉=1-+k- (17.4.26) Finally,equation (17.4.17)gives E3.k=3,k-3,k-1 (17.4.27)
17.4 A Worked Example: Spheroidal Harmonics 775 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). The boundary condition at x = 0 in this notation is y1 = 0, n − m odd y2 = 0, n − m even (17.4.18) At x = 1 we have two conditions: y2 = y3 − c2 2(m + 1)y1 (17.4.19) y1 = limx→1 (1 − x2) −m/2P m n (x) = (−1)m(n + m)! 2mm!(n − m)! ≡ γ (17.4.20) We are now ready to illustrate the use of the methods of previous sections on this problem. Relaxation If we just want a few isolated values of λ or S, shooting is probably the quickest method. However, if we want values for a large sequence of values of c, relaxation is better. Relaxation rewards a good initial guess with rapid convergence, and the previous solution should be a good initial guess if c is changed only slightly. For simplicity, we choose a uniform grid on the interval 0 ≤ x ≤ 1. For a total of M mesh points, we have h = 1 M − 1 (17.4.21) xk = (k − 1)h, k = 1, 2,...,M (17.4.22) At interior points k = 2, 3,...,M, equation (17.4.15) gives E1,k = y1,k − y1,k−1 − h 2 (y2,k + y2,k−1) (17.4.23) Equation (17.4.16) gives E2,k = y2,k − y2,k−1 − βk × (xk + xk−1)(m + 1)(y2,k + y2,k−1) 2 − αk (y1,k + y1,k−1) 2 (17.4.24) where αk = y3,k + y3,k−1 2 − c2(xk + xk−1)2 4 (17.4.25) βk = h 1 − 1 4 (xk + xk−1)2 (17.4.26) Finally, equation (17.4.17) gives E3,k = y3,k − y3,k−1 (17.4.27)
776 Chapter 17.Two Point Boundary Value Problems Now recall that the matrix of partial derivatives Si of equation(17.3.8)is defined so that i labels the equation and j the variable.In our case,j runs from I to 3 for yi at k-1 and from 4 to 6 for yi at k.Thus equation(17.4.23)gives h S1,1=-1, 512=-21 S1.3=0 (17.4.28) S1,4=1, 515=-2 S1,6=0 Similarly equation (17.4.24)yields S2,1=ak3k/2, S2,2=-1-k(xk+xk-1)(m+1)/2, S2,3=Bk(1,k+1,k-1)/4 S2,4=S2.1, S2,5=2+S2,2 S2,6=S2.3 (17.4.29) while from equation (17.4.27)we find RECIPES S3.1=0, S32=0, S3,3=-1 2 (17.4.30) S3.4=0, S3.5=0, S3.6=1 At z =0 we have the boundary condition Press. E3.1= ∫h,1,n-modd (17.4.31) Programs 22.1,n-m even Recall the convention adopted in the solvde routine that for one boundary condition at k =1 only S3.i can be nonzero.Also,j takes on the values 4 to 6 since the 6 boundary condition involves only yk,not yk-1.Accordingly,the only nonzero values of S3.j at x =0 are S3.4=1, n-m odd (17.4.32) S3.5=1, n-m even At z =1 we have 然 Numerical 10621 431 Recipes E1,M+1=欢.M- 3M-c2 2m+万h,M (17.4.33) (outside E2,M+1=1,M-Y (17.4.34) Thus 3M-c2 51,4=-2m+丁可 S1,5=1, 1,M S1,6=-2m+可 (17.4.35) S2,4=1, S2,5=0, S2.6=0 (17.4.36 Here now is the sample program that implements the above algorithm.We need a main program,sfroid,that calls the routine solvde,and we must supply the function difeq called by solvde.For simplicity we choose an equally spaced
776 Chapter 17. Two Point Boundary Value Problems Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Now recall that the matrix of partial derivatives Si,j of equation (17.3.8) is defined so that i labels the equation and j the variable. In our case, j runs from 1 to 3 for yj at k − 1 and from 4 to 6 for yj at k. Thus equation (17.4.23) gives S1,1 = −1, S1,2 = −h 2 , S1,3 = 0 S1,4 = 1, S1,5 = −h 2 , S1,6 = 0 (17.4.28) Similarly equation (17.4.24) yields S2,1 = αkβk/2, S2,2 = −1 − βk(xk + xk−1)(m + 1)/2, S2,3 = βk(y1,k + y1,k−1)/4 S2,4 = S2,1, S2,5 =2+ S2,2, S2,6 = S2,3 (17.4.29) while from equation (17.4.27) we find S3,1 = 0, S3,2 = 0, S3,3 = −1 S3,4 = 0, S3,5 = 0, S3,6 = 1 (17.4.30) At x = 0 we have the boundary condition E3,1 = y1,1, n − m odd y2,1, n − m even (17.4.31) Recall the convention adopted in the solvde routine that for one boundary condition at k = 1 only S3,j can be nonzero. Also, j takes on the values 4 to 6 since the boundary condition involves only y k, not yk−1. Accordingly, the only nonzero values of S3,j at x = 0 are S3,4 = 1, n − m odd S3,5 = 1, n − m even (17.4.32) At x = 1 we have E1,M+1 = y2,M − y3,M − c2 2(m + 1) y1,M (17.4.33) E2,M+1 = y1,M − γ (17.4.34) Thus S1,4 = −y3,M − c2 2(m + 1) , S1,5 = 1, S1,6 = − y1,M 2(m + 1) (17.4.35) S2,4 = 1, S2,5 = 0, S2,6 = 0 (17.4.36) Here now is the sample program that implements the above algorithm. We need a main program, sfroid, that calls the routine solvde, and we must supply the function difeq called by solvde. For simplicity we choose an equally spaced