intFcnTrig

../../_images/OpenMP.png

Integrates a function containing a sine or a cosine factor.

Synopsis

intFcnTrig (fcn, a, b, weight, omega)

Required Arguments

float fcn (float x) (Input)
User-supplied function to be integrated.
float a (Input)
Lower limit of integration.
float b (Input)
Upper limit of integration.
int weight and float omega (Input)
These two parameters are used to describe the trigonometric weight. The parameter weight can take on the two values described below, and the parameter omega = ω specifies the frequency of the trigonometric weighting function.
weight Integration Weight
COS cos (ωx)
SIN sin (ωx)

Return Value

The value of

\[\int_a^b \mathit{fcn}(x) \cos(\omega x) dx\]

is returned if weight = COS. If weight = SIN, then the cosine factor is replaced with a sine factor. If no value can be computed, NaN is returned.

Optional Arguments

errAbs, float (Input)

Absolute accuracy desired.

Default: \(\mathit{errAbs} = \sqrt{\varepsilon}\) where ɛ is the machine precision

errRel, float (Input)

Relative accuracy desired.

Default: \(\mathit{errRel} = \sqrt{\varepsilon}\) where ɛ is the machine precision

errEst, float errEst (Output)
An estimate of the absolute value of the error.
maxSubinter, int (Input)

Number of subintervals allowed.

Default: maxSubinter = 500

nSubinter (Output)
The number of subintervals generated.
nEvals (Output)
The number of evaluations of fcn.
maxMoments, int (Input)

This is an upper bound on the number of Chebyshev moments that can be stored. Increasing (decreasing) this number may increase (decrease) execution speed and space used.

Default: maxMoments = 21

Description

The function intFcnTrig is a special-purpose integrator that uses a globally adaptive scheme to reduce the absolute error. It computes integrals whose integrands have the special form w(x)f(x) where w(x) is either cos(ωx) or sin(ωx). Depending on the length of the subinterval in relation to the size of ω, either a modified Clenshaw-Curtis procedure or a Gauss-Kronrod 7∕15 rule is employed to approximate the integral on a subinterval. This function uses the general strategy of the function intFcnSing.

The function intFcnTrig is based on the subroutine QAWO by Piessens et al. (1983).

Examples

Example 1

The value of

\[\int_0^1 \ln (x) \sin (10 \pi x) dx\]

is computed. Notice that we have coded around the singularity at zero. This is necessary since this procedure evaluates the integrand at the two endpoints.

from __future__ import print_function
from numpy import *
from pyimsl.math.constant import constant
from pyimsl.math.intFcnTrig import intFcnTrig, SIN


def fcn(x):
    if(x == 0):
        res = 0.0
    else:
        res = log(x)
    return res


# Evaluate the integral
omega = 10 * constant("pi")
q = intFcnTrig(fcn, 0.0, 1.0, SIN, omega)

# Print the result and the exact answer
exact = -.1281316
print("integral  = %10.3f\nexact     = %10.3f" % (q, exact))

Output

integral  =     -0.128
exact     =     -0.128

Example 2

The value of

\[\int_0^1 \ln (x) \sin (10 \pi x) dx\]

is again computed. The values of the actual and estimated error are printed as well. Note that these numbers are machine dependent. Furthermore, it is usually the case that the error estimate is pessimistic. That is, the actual error is usually smaller than the error estimate as is the case in this example. The number of function evaluations are also printed.

from __future__ import print_function
from numpy import *
from pyimsl.math.constant import constant
from pyimsl.math.intFcnTrig import intFcnTrig, SIN


def fcn(x):
    if(x == 0):
        res = 0.0
    else:
        res = log(x)
    return res


# Evaluate the integral
omega = 10 * constant("pi")
err_est = []
n_evals = []
q = intFcnTrig(fcn, 0.0, 1.0, SIN, omega,
               errEst=err_est,
               nEvals=n_evals)

# Print the result and the exact answer
exact = -.1281316
exact_err = fabs(exact - q)
print("integral  = %10.3f\nexact     = %10.3f" % (q, exact))
print("error estimate   = %e\nexact error      = %e" % (err_est[0], exact_err))
print("The number of function evaluations  =  %d" % n_evals[0])

Output

integral  =     -0.128
exact     =     -0.128
error estimate   = 6.875028e-12
exact error      = 5.248399e-06
The number of function evaluations  =  305

Warning Errors

IMSL_ROUNDOFF_CONTAMINATION Roundoff error, preventing the requested tolerance from being achieved, has been detected.
IMSL_PRECISION_DEGRADATION A degradation in precision has been detected.
IMSL_EXTRAPOLATION_ROUNDOFF Roundoff error in the extrapolation table, preventing the requested tolerance from being achieved, has been detected.

Fatal Errors

IMSL_DIVERGENT Integral is probably divergent or slowly convergent.
IMSL_MAX_SUBINTERVALS The maximum number of subintervals allowed has been reached.
IMSL_STOP_USER_FCN

Request from user supplied function to stop algorithm.

User flag = “#”.