intFcn2d

../../_images/OpenMP.png

Computes a two-dimensional iterated integral.

Synopsis

intFcn2d (fcn, a, b, gcn hcn)

Required Arguments

float fcn (x, y) (Input)
User-supplied function to be integrated.
float a (Input)
Lower limit of outer integral.
float b (Input)
Upper limit of outer integral.
float gcn (x) (Input)
User-supplied function to evaluate the lower limit of the inner integral.
float hcn (x) (Input)
User-supplied function to evaluate the upper limit of the inner integral.

Return Value

The value of

\[\int_a^b \int_{\mathit{gcn}(x)}^{\mathit{hcn}(x)} \mathit{fcn}(x,y) dy dx\]

is returned. 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 (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.

Description

The function intFcn2d approximates the two-dimensional iterated integral

\[\int_a^b \int_{g(x)}^{h(x)} f(x,y) dy dx\]

An estimate of the error is returned in errEst. The lower-numbered rules are used for less smooth integrands while the higher-order rules are more efficient for smooth (oscillatory) integrands.

Examples

Example 1

In this example, compute the value of the integral

\[\int_0^1 \int_1^3 y \cos \left(x+y^2\right) dy dx\]
from __future__ import print_function
from numpy import *
from pyimsl.math.intFcn2d import intFcn2d


def fcn(x, y):
    return y * cos(x + y * y)


def gcn(x):
    return 1.0


def hcn(x):
    return 3.0


# Evaluate the integral
q = intFcn2d(fcn, 0.0, 1.0, gcn, hcn)

# Print the result and the exact answer
exact = 0.5 * (cos(9.0) + cos(2.0) - cos(10.0) - cos(1.0))
print("integral  = %10.3f\nexact     = %10.3f" % (q, exact))

Output

integral  =     -0.514
exact     =     -0.514

Example 2

In this example, compute the value of the integral

\[\int_0^1 \int_1^3 y \cos \left(x+y^2\right) dy dx\]

The values of the actual and estimated error are printed as well. Note that these numbers are machine dependent. Furthermore, the error estimate is usually 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 also is printed.

from __future__ import print_function
from numpy import *
from pyimsl.math.intFcn2d import intFcn2d


def fcn(x, y):
    return y * cos(x + y * y)


def gcn(x):
    return 1.0


def hcn(x):
    return 3.0


# Evaluate the integral
err_est = []
n_evals = []
q = intFcn2d(fcn, 0.0, 1.0, gcn, hcn, errEst=err_est, nEvals=n_evals)

# Print the result and the exact answer
exact = 0.5 * (cos(9.0) + cos(2.0) - cos(10.0) - cos(1.0))
exact_err = abs(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.514
exact     =     -0.514
error estimate   = 5.709366e-15
exact error      = 3.330669e-16
The number of function evaluations  =  441

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.

Fatal Errors

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 = “#”.