home *** CD-ROM | disk | FTP | other *** search
-
- (* :Name: NumericalMath`GaussianQuadrature` *)
-
- (* :Title: Coefficients for Gaussian Quadrature *)
-
- (* :Author: Jerry B. Keiper *)
-
- (* :Summary:
- Calculates the weights and abscissae for the elementary Gaussian
- quadrature rule with n points on the interval a to b. Calculates
- the error in the elementary Gaussian quadrature formula with n
- points on the interval a to b.
- *)
-
- (* :Context: NumericalMath`GaussianQuadrature` *)
-
- (* :Package Version: Mathematica 2.0 *)
-
- (* :Copyright: Copyright 1990, Wolfram Research, Inc.
- Permission is hereby granted to modify and/or make copies of
- this file for any purpose other than direct profit, or as part
- of a commercial product, provided this copyright notice is left
- intact. Sale, other than for the cost of media, is prohibited.
-
- Permission is hereby granted to reproduce part or all of
- this file, provided that the source is acknowledged.
- *)
-
- (* :History:
- Revised by Jerry B. Keiper, December, 1990.
- Original version by Jerry B. Keiper, May, 1990.
- *)
-
- (* :Keywords: Gaussian quadrature, abscissae, weights *)
-
- (* :Source: Any elementary numerical analysis textbook. *)
-
- (* :Mathematica Version: 2.0 *)
-
- (* :Limitation: *)
-
- BeginPackage["NumericalMath`GaussianQuadrature`"]
-
- GaussianQuadratureWeights::usage =
- "GaussianQuadratureWeights[n, a, b, prec] gives a list of the pairs
- {abscissa, weight} to prec digits precision for the elementary n-point
- Gaussian quadrature formula for quadrature on the interval a to b. The
- argument prec is optional."
-
- GaussianQuadratureError::usage =
- "GaussianQuadratureError[n, f, a, b, prec] gives the leading term in the
- error in the elementary n-point Gaussian quadrature formula to prec digits
- precision for the function f on an interval from a to b. The argument prec
- is optional."
-
- Begin["NumericalMath`GaussianQuadrature`Private`"]
-
- GaussianQuadratureWeights[n_Integer, a_, b_, prec_Integer:$MachinePrecision] :=
- GQW[n, a, b, prec] /; (n > 0 && prec > 0)
-
- GaussianQuadratureError[n_Integer, f_, a_, b_, prec_Integer:$MachinePrecision] :=
- GQE[n, f, a, b, prec] /; (n > 0 && prec > 0)
-
-
- GQW[n_, a_, b_, prec_] :=
- Module[{i, w, x, m, rhs, legn, t},
- legn = Numerator[LegendreP[n,t]];
- If[ n == 1, Return[N[{{0, 2}}, prec]]];
- x = Apply[List, Map[#[[2]]&, NRoots[legn == 0, t, prec]]];
- m = Table[x^i, {i,n-1}];
- PrependTo[m, Table[1, {n}]];
- rhs = Table[(1+Sign[(-1)^i])/(i+1), {i,0,n-1}];
- w = Expand[LinearSolve[m, rhs](b-a)/2];
- x = Expand[(x (b-a) + (b+a))/2];
- Transpose[{x, w}]
- ]
-
- GQE[n_, f_, a_, b_, prec_] :=
- Module[{t, fint, fs, x, w, len},
- fs = t^(2n)/(2n)!;
- {x, w} = Transpose[GQW[n, -len, len, prec]];
- fs = Expand[fs /. t->x] . w;
- fint = 2 len^(2n+1)/(2n+1)!;
- (Expand[fs - fint] /. len -> (b-a)/2) Derivative[2n][f]
- ]
-
- End[] (* NumericalMath`GaussianQuadrature`Private` *)
-
- Protect[GaussianQuadratureWeights, GaussianQuadratureError];
-
- EndPackage[] (* NumericalMath`GaussianQuadrature` *)
-
-