nag 1d quad gauss 1 (d01tac)

Similar documents
nag 1d quad gauss (d01bac)

NAG Library Routine Document D01BAF.1

NAG Library Function Document nag_quad_md_gauss (d01fbc)

NAG Library Function Document nag_1d_quad_inf_1 (d01smc)

nag opt one var no deriv (e04abc)

D01ARF NAG Fortran Library Routine Document

NAG Library Function Document nag_quad_2d_fin (d01dac)

NAG Library Function Document nag_1d_quad_wt_alglog_1 (d01spc)

NAG Library Function Document nag_multid_quad_monte_carlo_1 (d01xbc)

NAG C Library Function Document nag_multid_quad_monte_carlo_1 (d01xbc)

NAG Library Function Document nag_zero_nonlin_eqns_deriv_1 (c05ubc)

nag opt simplex (e04ccc)

SYDE 312 UNIT 4: EXTRA QUADRATURE PROBLEMS

nag opt check 2nd deriv (e04hdc)

NAG Library Function Document nag_ode_bvp_fd_nonlin_fixedbc (d02gac)

NAG Library Function Document nag_ode_bvp_fd_lin_gen (d02gbc)

nag simple linear regression (g02cac)

NAG Library Function Document nag_ode_ivp_rk_interp (d02pxc)

Binary floating point encodings

NAG Library Function Document nag_1d_quad_wt_alglog (d01apc)

nag 1d spline deriv (e02bcc)

nag opt lsq no deriv (e04fcc)

NAG Library Routine Document D04AAF.1

nag sparse nsym sol (f11dec)

NAG Library Function Document nag_opt_one_var_deriv (e04bbc)

nag mv orthomax (g03bac)

nag tsa spectrum univar (g13cbc)

NAG Library Function Document nag_zero_cont_func_brent_rcomm (c05azc)

NAG Library Function Document nag_exp_integral (s13aac)

A MIXED QUADRATURE FORMULA USING RULES OF LOWER ORDER

NAG Library Function Document nag_legendre_p (s22aac)

NAG C Library Function Document nag_superlu_condition_number_lu (f11mgc)

NAG Fortran Library Routine Document D01AJF.1

PHYSICS 115/242 Homework 2, Solutions

nag tsa noise spectrum bivar (g13cgc)

2.1.1 Fixed-Point (or Integer) Arithmetic

CS321 Introduction To Numerical Methods

nag anova random (g04bbc)

nag complex svd (f02xec)

nag 2d spline eval rect (e02dfc)

NAG Library Function Document nag_durbin_watson_stat (g02fcc)

NAG Library Function Document nag_rgsn_matrix_multi_normal (g05lyc)

NAG Library Function Document nag_chain_sort (m01cuc)

NAG Library Function Document nag_specfun_1f1_real (s22bac)

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

NAG C Library Function Document nag_binary_factor_service (g11sbc)

nag tsa cross spectrum bivar (g13cec)

Floating-point numbers. Phys 420/580 Lecture 6

PHYSICS 115/242 Homework 5, Solutions X = and the mean and variance of X are N times the mean and variance of 12/N y, so

Introduction to Computer Programming with MATLAB Calculation and Programming Errors. Selis Önel, PhD

2 Computation with Floating-Point Numbers

NAG Library Function Document nag_ode_bvp_ps_lin_cheb_eval (d02uzc)

2 Computation with Floating-Point Numbers

NAG Library Function Document nag_1d_cheb_intg (e02ajc)

NAG Library Function Document nag_zeros_complex_poly (c02afc)

Numerical Methods with Matlab: Implementations and Applications. Gerald W. Recktenwald. Chapter 11 Numerical Integration

NAG Library Function Document nag_sum_convcorr_real (c06fkc)

nag mv kmeans cluster analysis (g03efc)

nag regsn mult linear upd model (g02ddc)

NAG Library Function Document nag_prob_durbin_watson (g01epc)

NAG Library Function Document nag_asian_geom_price (s30sac)

ANSI C Programming Simple Programs

NAG Library Function Document nag_2_sample_ks_test (g08cdc)

Scientific Computing. Error Analysis

NAG C Library Function Document nag_kruskal_wallis_test (g08afc)

nag regsn mult linear add var (g02dec)

Cubature Rules for High-dimensional Adaptive Integration

1.00 Lecture 19. Numerical Methods: Root Finding

Introduction to numerical algorithms

NAG Library Function Document nag_1d_spline_interpolant (e01bac)

NAG Library Function Document nag_stable_sort (m01ctc)

NAG Library Function Document nag_opt_check_deriv (e04hcc)

CS 450 Numerical Analysis. Chapter 7: Interpolation

NAG Library Function Document nag_search_vector (m01fsc)

Contents. Hilary Term. Summary of Numerical Analysis for this term. Sources of error in numerical calculation. Solving Problems

NAG Library Function Document nag_2_sample_ks_test (g08cdc)

Review Questions 26 CHAPTER 1. SCIENTIFIC COMPUTING

NAG Library Function Document nag_bsm_price (s30aac)

9.1 Bracketing and Bisection

Remember Java Data Types

NAG C Library Function Document nag_pde_parab_1d_euler_hll (d03pwc)

NAG Library Function Document nag_sum_fft_real_3d (c06pyc)

1.00 Lecture 32. Integration. Reading for next time: Numerical Recipes Packaging Functions in Objects

(Type your answer in radians. Round to the nearest hundredth as needed.)

NAG Library Function Document nag_sum_fft_cosine (c06rfc)

nag regsn mult linear delete var (g02dfc)

NAG Library Function Document nag_wfilt (c09aac)

Catholic Central High School

NAG Library Function Document nag_dwt (c09cac)

NAG Library Function Document nag_zero_nonlin_eqns_rcomm (c05qdc)

NAG Library Function Document nag_opt_simplex_easy (e04cbc)

Numerical Aspects of Special Functions

NAG Library Function Document nag_5d_shep_eval (e01tnc)

NAG Library Function Document nag_prob_lin_non_central_chi_sq (g01jcc)

NAG Library Function Document nag_tsa_cp_pelt_user (g13nbc)

Wiswall, Applied Microeconometrics, Lecture Notes 1. In this section we focus on three very common computational tasks in applied

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG Library Function Document nag_real_gen_matrix_exp (f01ecc)

& Control Flow of Program

Transcription:

1. Purpose nag 1d quad gauss 1 () nag 1d quad gauss 1 () computes an estimate of the definite integral of a function of known analytical form, using a Gaussian quadrature formula with a specified number of abscissae. Formulae are provided for a finite interval (Gauss-Legendre), a semi-infinite interval (Gauss- Laguerre, Gauss-Rational), and an infinite interval (Gauss-Hermite). 2. Specification #include <nag.h> #include <nagd01.h> double nag_1d_quad_gauss_1(nag_gaussformulae quadrule, double (*f)(double x, Nag_User *comm), double a, double b, Integer npts, Nag_User *comm, NagError *fail) 3. Description 3.1. General This function evaluates an estimate of the definite integral of a function f(x), over a finite or infinite interval, by n-point Gaussian quadrature (see Davis and Rabinowitz (1967), Froberg (1965), Ralston (1965) or Stroud and Secrest (1966)). The integral is approximated by a summation n ω i f(x i ) i=1 where ω i are called the weights, and the x i the abscissae. A selection of values of n is available. (See Section 4.) 3.2. Both Limits Finite b a f(x) dx The Gauss-Legendre weights and abscissae are used, and the formula is exact for any function of the form: f(x) = 2n 1 c i x i. The formula is appropriate for functions which can be well approximated by such a polynomial over [a, b]. It is inappropriate for functions with algebraic singularities at one or both ends of the 1 interval, such as on [ 1, 1]. (1+x) 3.3. One Limit Infinite a f(x) dx or a f(x) dx. Two quadrature formulae are available for these integrals. (a) The Gauss-Laguerre formula is exact for any function of the form: 2n 1 f(x) =e bx c i x i. This formula is appropriate for functions decaying exponentially at infinity; the parameter b should be chosen if possible to match the decay rate of the function. 3..1

nag 1d quad gauss 1 NAG C Library Manual (b) The Gauss-Rational formula is exact for any function of the form: f(x) = 2n+1 i=2 c i (x + b) i = 2n 1 c 2n+1 i (x + b) i (x + b) 2n+1 This formula is likely to be more accurate for functions having only an inverse power rate of decay for large x. Here the choice of a suitable value of b may be more difficult; unfortunately apoorchoiceofb can make a large difference to the accuracy of the computed integral. 3.4. Both Limits Infinite f(x) dx. The Gauss-Hermite weights and abscissae are used, and the formula is exact for any function of the form: 2n 1 f(x) =e b(x a)2 c i x i. Again, for general functions not of this exact form, the parameter b should be chosen to match if possible the decay rate at ±. 4. Parameters quadrule Members of this enum type indicate the quadrature formula: Nag Legendre, for Gauss-Legendre quadrature on a finite interval; Nag Rational, for Gauss-Rational quadrature on a semi-infinite interval; Nag Laguerre, for Gauss-Laguerre quadrature on a semi-infinite interval; Nag Hermite, for Gauss-Hermite quadrature on an infinite interval. f The function f, supplied by the user, must return the value of the integrand f at a given point. The specification of f is: double f(double x, Nag_User *comm) x Input: the point at which the integrand f must be evaluated. comm Input/Output: pointer to a structure of type Nag User with the following member: p -Pointer Input/Output: the pointer comm->p should be cast to the required type, e.g. struct user *s = (struct user *)comm->p, to obtain the original object s address with appropriate type. (See the argument comm below.) Some points to bear in mind when coding f are mentioned in Section 6. a b Input: the parameters a and b which occur in the integration formulae: Gauss-Legendre: a is the lower limit and b is the upper limit of the integral. It is not necessary that a<b. 3..2

Gauss-Rational: b must be chosen so as to make the integrand match as closely as possible the exact form given in Section 3.3(b). The interval of integration is [a, ) ifa + b>0, and (,a]if a + b<0. Gauss-Laguerre: b must be chosen so as to make the integrand match as closely as possible the exact form given in Section 3.3(a). The interval of integration is [a, ) ifb>0, and (,a]ifb<0. Gauss-Hermite: a and b must be chosen so as to make the integrand match as closely as possible the exact formgiveninsection3.4. Constraints: Gauss-Rational: a + b 0 Gauss-Laguerre: b 0 Gauss-Hermite: b > 0. npts Input: the number of abscissae to be used, n. Constraint: npts = 1,2,3,4,5,6,8,10,12,14,16,20,24,32,48 or 64. comm Input/Output: pointer to a structure of type Nag User with the following member: p -Pointer Input/Output: the pointer p, of type Pointer, allows the user to communicate information to and from the user-defined function f(). An object of the required type should be declared by the user, e.g. a structure, and its address assigned to the pointer p by means of a cast to Pointer in the calling program, e.g. comm.p = (Pointer)&s. The type pointer will be void * with a C compiler that defines void * and char * otherwise. fail The NAG error parameter, see the Essential Introduction to the NAG C Library. Users are recommended to declare and initialize fail and set fail.print = TRUE for this function. 5. Error Indications and Warnings NE BAD PARAM On entry, parameter quadrule had an illegal value. NE QUAD GAUSS NPTS RULE The N-point rule is not among those stored. The answer is evaluated for value, the largest possible value of npts less than the requested value, value. NE QUAD GAUSS CONS Gauss-Rational input is invalid with a + b =0. Constraint: a + b 0. Gauss-Laguerre input is invalid with b = 0. Constraint: b 0. Gauss-Hermite input is invalid with b 0. Constraint: b > 0. The answer is returned as zero. 6. Further Comments The time taken by the routine depends on the complexity of the expression for the integrand and on the number of abscissae required. 6.1. Accuracy The accuracy depends on the behaviour of the integrand, and on the number of abscissae used. No tests are carried out in the routine to estimate the accuracy of the result. If such an estimate 3..3

nag 1d quad gauss 1 NAG C Library Manual is required, the routine may be called more than once, with a different number of abscissae each time, and the answers compared. It is to be expected that for sufficiently smooth functions a larger number of abscissae will give improved accuracy. Alternatively, the interval of integration may be subdivided, the integral estimated separately for each sub-interval, and the sum of these estimates compared with the estimate over the whole interval. The coding of the function f may also have a bearing on the accuracy. For example, if a high-order Gauss-Laguerre formula is used, and the integrand is of the form f(x) =e bx g(x) it is possible that the exponential term may underflow for some large abscissae. Depending on the machine, this may produce an error, or simply be assumed to be zero. In any case, it would be better to evaluate the expression as: f(x) =exp( bx +lng(x)). Another situation requiring care is exemplified by e x2 x m dx =0, m odd. The integrand here assumes very large values; for example, for m = 63, the peak value exceeds 3 10 33. Now, if the machine holds floating-point numbers to an accuracy of k significant decimal digits, we could not expect such terms to cancel in the summation leaving an answer of much less than 10 33 k (the weights being of order unity); that is instead of zero, we obtain a rather large answer through rounding error. Fortunately, such situations are characterised by great variability in the answers returned by formulae with different values of n. In general, the user should be aware of the order of magnitude of the integrand, and should judge the answer in that light. 6.2. References Davis P J and Rabinowitz P (1967) Numerical Integration Blaisdell Publishing Company pp. 33 52. Froberg C E (1965) Introduction to Numerical Analysis Addison-Wesley pp. 181 187. Ralston A (1965) A First Course in Numerical Analysis McGraw-Hill pp. 87 90. Stroud A H and Secrest D (1966) Gaussian Quadrature Formulas Prentice-Hall. 7. See Also None. 8. Example This example program evaluates the integrals 1 4 0 1+x 2 dx = π by Gauss-Legendre quadrature; 1 2 x 2 dx =0.378671 ln x by Gauss-Rational quadrature with b =0; e x dx =0.048901 2 x by Gauss-Laguerre quadrature with b =1;and e 3x2 4x 1 dx = e 3(x+1)2 e 2x+2 dx =1.428167 by Gauss-Hermite quadrature with a = 1 andb =3. The formulae with n =4, 8, 16 are used in each case. 3..4

8.1. Program Text /* nag_1d_quad_guass_1() Example Program * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. */ #include <nag.h> #include <stdio.h> #include <nag_stdlib.h> #include <math.h> #include <nagd01.h> static double fun1(double x, Nag_User *comm); static double fun1(); static double fun2(double x, Nag_User *comm); static double fun2(); static double fun3(double x, Nag_User *comm); static double fun3(); static double fun4(double x, Nag_User *comm); static double fun4(); main() static Integer nstor[3] = 4, 8, 16; double a, b; Integer i; static NagError fail; double ans; Nag_GaussFormulae gaussformula; Boolean success = TRUE; Nag_User comm; fail.print = TRUE; Vprintf(" Example Program Results\n"); Vprintf("\nGauss-Legendre example\n\n"); a = 0.0; b = 1.0; gaussformula = Nag_Legendre; ans = (gaussformula, fun1, a, b, nstor[i], &comm, &fail); Vprintf("\nGauss-Rational example\n\n"); 3..5

nag 1d quad gauss 1 NAG C Library Manual a = 2.0; b = 0.0; gaussformula = Nag_Rational; ans = (gaussformula, fun2, a, b, nstor[i], &comm, &fail); Vprintf("\nGauss-Laguerre example\n\n"); a = 2.0; b = 1.0; gaussformula = Nag_Laguerre; ans = (gaussformula, fun3, a, b, nstor[i], &comm, &fail); Vprintf("\nGauss-Hermite example\n\n"); a = -1.0; b = 3.0; gaussformula = Nag_Hermite; ans = (gaussformula, fun4, a, b, nstor[i], &comm, &fail); if (success) exit(exit_success); exit(exit_failure); /* main */ static double fun1(double x, Nag_User *comm) static double fun1(x, comm) return 4.0/(x*x+1.0); static double fun2(double x, Nag_User *comm) static double fun2(x, comm) 3..6

return 1.0/(x*x*log(x)); static double fun3(double x, Nag_User *comm) static double fun3(x, comm) return exp(-x)/x; static double fun4(double x, Nag_User *comm) static double fun4(x, comm) return exp(x*(-3.0)*x-x*4.0-1.0); 8.2. Program Data None. 8.3. Program Results Example Program Results Gauss-Legendre example 4 Points Answer = 3.14161 8 Points Answer = 3.14159 16 Points Answer = 3.14159 Gauss-Rational example 4 Points Answer = 0.37910 8 Points Answer = 0.37876 16 Points Answer = 0.37869 Gauss-Laguerre example 4 Points Answer = 0.04887 8 Points Answer = 0.04890 16 Points Answer = 0.04890 Gauss-Hermite example 4 Points Answer = 1.42803 8 Points Answer = 1.42817 16 Points Answer = 1.42817 3..7