正在加载图片...
17.4 A Worked Example:Spheroidal Harmonics 781 void derivs(float x,float y[],float dydx[]) Evaluates derivatives for odeint. dydx[1]=y[2]: dydx[2]=(2.0*x*(m+1.0)*y[2]-(y[3]-c2*x*x)*y[1])/(1.0-x*x); dydx[3]=0.0; Shooting to a Fitting Point For variety we illustrate shootf from $17.2 by integrating over the whole range 8 -1+dz <z<1-dz,with the fitting point chosen to be at x =0.The routine 鱼 derivs is identical to the one for shoot.Now,however,there are two load routines. The routine load1 for x =-1 is essentially identical to load above.At x 1, 100 load2 sets the function value y and the eigenvalue ya to their best current estimates, v2[1]and v2[2],respectively.If you quite sensibly make your initial guess of from NUMERICAL RECIPESI 18881892 the eigenvalue the same in the two intervals,then v1[1]will stay equal to v2[2] during the iteration.The function score simply checks whether all three function values match at the fitting point. (North Ameri computer, University Press. THE #include <stdio.h> #include <math.h> ART #include "nrutil.h" #define N1 2 #define N2 1 Programs #define NTOT (N1+N2) #define DXX 1.0e-4 int m,n; Communicates with load1,load2,score, float c2,dx,gmma; and derivs. int nn2,nvar; Communicates with shootf. float x1,x2,xf; 19881992 SCIENTIFIC COMPUTING(ISBN int main(void)/*Program sphfpt * Sample program using shootf.Computes eigenvalues of spheroidal harmonics Smn(z;c)for m >0 and n m.Note how the routine vecfunc for newt is provided by shootf ($17.2). Numerical Recipes 10-621 The routine derivs is the same as for sphoot. 43108 void newt(float x[],int n,int *check, void(*vecfunc)(int,f1oat☐,f1oat0)); void shootf(int n,float v[],float ) int check,i; (outside float q1,*v1,*v2,*v; Software. v=vector(1,NTOT); v1=V; Amer v2=v[N2]; nvar=NTOT; Number of equations. nn2=N2; dx=DXX; Avoid evaluating derivatives exactly at z for (;;) 士1. printf("input m,n,c-squared\n"); if (scanf("%d %d %f",&m,&n,&c2)==EOF)break; if (n m Il m 0)continue; gmma=1.0; Compute y of equation (17.4.20). q1=n; for(1=1;1<=m;i+)gmma*=-0.5*(n+1)*(q1--/1);17.4 A Worked Example: Spheroidal Harmonics 781 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 machine￾readable 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). void derivs(float x, float y[], float dydx[]) Evaluates derivatives for odeint. { dydx[1]=y[2]; dydx[2]=(2.0*x*(m+1.0)*y[2]-(y[3]-c2*x*x)*y[1])/(1.0-x*x); dydx[3]=0.0; } Shooting to a Fitting Point For variety we illustrate shootf from §17.2 by integrating over the whole range −1 + dx ≤ x ≤ 1 − dx, with the fitting point chosen to be at x = 0. The routine derivs is identical to the one for shoot. Now, however, there are two load routines. The routine load1 for x = −1 is essentially identical to load above. At x = 1, load2 sets the function value y1 and the eigenvalue y3 to their best current estimates, v2[1] and v2[2], respectively. If you quite sensibly make your initial guess of the eigenvalue the same in the two intervals, then v1[1] will stay equal to v2[2] during the iteration. The function score simply checks whether all three function values match at the fitting point. #include <stdio.h> #include <math.h> #include "nrutil.h" #define N1 2 #define N2 1 #define NTOT (N1+N2) #define DXX 1.0e-4 int m,n; Communicates with load1, load2, score, float c2,dx,gmma; and derivs. int nn2,nvar; Communicates with shootf. float x1,x2,xf; int main(void) /* Program sphfpt */ Sample program using shootf. Computes eigenvalues of spheroidal harmonics Smn(x; c) for m ≥ 0 and n ≥ m. Note how the routine vecfunc for newt is provided by shootf (§17.2). The routine derivs is the same as for sphoot. { void newt(float x[], int n, int *check, void (*vecfunc)(int, float [], float [])); void shootf(int n, float v[], float f[]); int check,i; float q1,*v1,*v2,*v; v=vector(1,NTOT); v1=v; v2 = &v[N2]; nvar=NTOT; Number of equations. nn2=N2; dx=DXX; Avoid evaluating derivatives exactly at x = for (;;) { ±1. printf("input m,n,c-squared\n"); if (scanf("%d %d %f",&m,&n,&c2) == EOF) break; if (n < m || m < 0) continue; gmma=1.0; Compute γ of equation (17.4.20). q1=n; for (i=1;i<=m;i++) gmma *= -0.5*(n+i)*(q1--/i);
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有