c19-6 (779621), страница 6
Текст из файла (страница 6)
The current value of the solutionu[1..n][1..n] is updated, using the right-hand side function rhs[1..n][1..n].{int i,ipass,isw,j,jsw=1;double foh2,h,h2i,res;h=1.0/(n-1);h2i=1.0/(h*h);foh2 = -4.0*h2i;for (ipass=1;ipass<=2;ipass++,jsw=3-jsw) {Red and black sweeps.isw=jsw;for (j=2;j<n;j++,isw=3-isw) {for (i=isw+1;i<n;i+=2) {res=h2i*(u[i+1][j]+u[i-1][j]+u[i][j+1]+u[i][j-1]4.0*u[i][j])+u[i][j]*u[i][j]-rhs[i][j];u[i][j] -= res/(foh2+2.0*u[i][j]);Newton Gauss-Seidel formula.}}}}Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use.
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).jm1=jj-1;rstrct(itemp[jm1],itemp[jj],nf);RLh (euh ).rstrct(iu[jm1],iu[jj],nf);Reuh .lop(itau[jm1],iu[jm1],nf);LH (Reuh ) stored temporarily in τeh.matsub(itau[jm1],itemp[jm1],itau[jm1],nf);Form τeh .if (jj == j)trerr=ALPHA*anorm2(itau[jm1],nf);Estimate truncation error τ .rstrct(irhs[jm1],irhs[jj],nf);fH .matadd(irhs[jm1],itau[jm1],irhs[jm1],nf); fH + τeh .19.6 Multigrid Methods for Boundary Value Problems887#include <math.h>fill0(u,3);fact=2.0/(h*h);disc=sqrt(fact*fact+rhs[2][2]);u[2][2] = -rhs[2][2]/(fact+disc);}void lop(double **out, double **u, int n)Given u[1..n][1..n], returns Lh (euh ) for equation (19.6.44) in out[1..n][1..n].{int i,j;double h,h2i;h=1.0/(n-1);h2i=1.0/(h*h);for (j=2;j<n;j++)Interior points.for (i=2;i<n;i++)out[i][j]=h2i*(u[i+1][j]+u[i-1][j]+u[i][j+1]+u[i][j-1]4.0*u[i][j])+u[i][j]*u[i][j];for (i=1;i<=n;i++)Boundary points.out[i][1]=out[i][n]=out[1][i]=out[n][i]=0.0;}void matadd(double **a, double **b, double **c, int n)Adds a[1..n][1..n] to b[1..n][1..n] and returns result in c[1..n][1..n].{int i,j;for (j=1;j<=n;j++)for (i=1;i<=n;i++)c[i][j]=a[i][j]+b[i][j];}void matsub(double **a, double **b, double **c, int n)Subtracts b[1..n][1..n] from a[1..n][1..n] and returns result in c[1..n][1..n].{int i,j;for (j=1;j<=n;j++)for (i=1;i<=n;i++)c[i][j]=a[i][j]-b[i][j];}#include <math.h>double anorm2(double **a, int n)Returns the Euclidean norm of the matrix a[1..n][1..n].{int i,j;double sum=0.0;for (j=1;j<=n;j++)for (i=1;i<=n;i++)sum += a[i][j]*a[i][j];return sqrt(sum)/n;}Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use.
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).void slvsm2(double **u, double **rhs)Solution of equation (19.6.44) on the coarsest grid, where h = 12 . The right-hand side is inputin rhs[1..3][1..3] and the solution is returned in u[1..3][1..3].{void fill0(double **u, int n);double disc,fact,h=0.5;888Chapter 19.Partial Differential EquationsCITED REFERENCES AND FURTHER READING:Brandt, A.
1977, Mathematics of Computation, vol. 31, pp. 333–390. [1]Hackbusch, W. 1985, Multi-Grid Methods and Applications (New York: Springer-Verlag). [2]Stuben, K., and Trottenberg, U. 1982, in Multigrid Methods, W. Hackbusch and U. Trottenberg,eds. (Springer Lecture Notes in Mathematics No. 960) (New York: Springer-Verlag), pp. 1–176. [3]Baker, L. 1991, More C Tools for Scientists and Engineers (New York: McGraw-Hill).Briggs, W.L. 1987, A Multigrid Tutorial (Philadelphia: S.I.A.M.).Jespersen, D. 1984, Multrigrid Methods for Partial Differential Equations (Washington: Mathematical Association of America).McCormick, S.F.
(ed.) 1988, Multigrid Methods: Theory, Applications, and Supercomputing (NewYork: Marcel Dekker).Hackbusch, W., and Trottenberg, U. (eds.) 1991, Multigrid Methods III (Boston: Birkhauser).Wesseling, P. 1992, An Introduction to Multigrid Methods (New York: Wiley).Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use.
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).Brandt, A. 1982, in Multigrid Methods, W. Hackbusch and U.
Trottenberg, eds. (Springer LectureNotes in Mathematics No. 960) (New York: Springer-Verlag). [4].















