Documentation Center

  • Trials
  • Product Updates

integral

Numerically evaluate integral

Syntax

  • q = integral(fun,xmin,xmax) example
  • q = integral(fun,xmin,xmax,Name,Value) example

Description

example

q = integral(fun,xmin,xmax) approximates the integral of function fun from xmin to xmax using global adaptive quadrature and default error tolerances.

example

q = integral(fun,xmin,xmax,Name,Value) specifies additional options with one or more Name,Value pair arguments.

Examples

expand all

Evaluate Improper Integral

Create the anonymous function f(x) = e-x2(ln x)2.

fun = @(x) exp(-x.^2).*log(x).^2;

Evaluate the integral from x=0 to x=Inf.

q = integral(fun,0,Inf)
q =

    1.9475

Integrate Parameterized Function

Create the anonymous function f(x) = 1/(x3 – 2xc) with one parameter, c.

fun = @(x,c) 1./(x.^3-2*x-c);

Evaluate the integral from x=0 to x=2 at c=5.

q = integral(@(x)fun(x,5),0,2)
q =

   -0.4605

Evaluate Integral with Singularity at the Lower Limit of Integration

Create the anonymous function f(x) = ln(x).

fun = @(x)log(x);

Evaluate the integral from x=0 to x=1 with the default error tolerances.

format long
q1 = integral(fun,0,1)
q1 =

  -1.000000010959678

Evaluate the integral again, specifying approximately 12 decimal places of accuracy.

q2 = integral(fun,0,1,'RelTol',0,'AbsTol',1e-12)
q2 =

  -1.000000000000010

Complex Contour Integration Using Waypoints

Specify waypoints [1+1i,1-1i] to integrate over the triangular path: 0 to 1+1i to 1-1i to 0.

Create the anonymous function f(z) = 1/(2z – 1).

fun = @(z) 1./(2*z-1);

Integrate in the complex plane over the triangular path from 0 to 1+1i to 1-1i to 0.

q = integral(fun,0,0,'Waypoints',[1+1i,1-1i])
q =

        0 - 3.1416i

Integrate Vector-Valued Function

Specify 'ArrayValued',true to evaluate the integral of an array-valued or vector-valued function.

Create the anonymous vector-valued function f(x) = [sin x, sin 2x, sin 3x, sin 4x, sin 5x] and integrate from x=0 to x=1.

fun = @(x)sin((1:5)*x);
q = integral(fun,0,1,'ArrayValued',true)
q =

    0.4597    0.7081    0.6633    0.4134    0.1433

Improper Integral of an Oscillatory Function

Create the anonymous function f(x) = x5 e-x sin x.

fun = @(x)x.^5.*exp(-x).*sin(x);

Evaluate the integral from x=0 to x=Inf , adjusting the absolute and relative tolerances.

format long
q = integral(fun,0,Inf,'RelTol',1e-8,'AbsTol',1e-13)
q =

 -14.999999999998364

Input Arguments

expand all

fun — Integrandfunction handle

Integrand, specified as a function handle, defines the function to be integrated from xmin to xmax. For scalar-valued problems, the function y = fun(x) must accept a vector argument, x, and return a vector result, y. This generally means that fun must use array operators instead of matrix operators. For example, use .* (times) rather than * (mtimes). If you set the 'ArrayValued' option to true, fun must accept a scalar and return an array of fixed size.

xmin — Lower limit of xreal number | complex number

Lower limit of x, specified as a real (finite or infinite) scalar value or a complex (finite) scalar value. If either xmin or xmax are complex, integral approximates the path integral from xmin to xmax over a straight line path.

Data Types: double | single
Complex Number Support: Yes

xmax — Upper limit of xreal number | complex number

Upper limit of x, specified as a real (finite or infinite) scalar value or a complex (finite) scalar value. If either xmin or xmax are complex, integral approximates the path integral from xmin to xmax over a straight line path.

Data Types: double | single
Complex Number Support: Yes

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'AbsTol',1e-12 sets the absolute error tolerance to approximately 12 decimal places of accuracy.

'AbsTol' — Absolute error tolerancenonnegative real number

Absolute error tolerance, specified as the comma-separated pair consisting of 'AbsTol' and a nonnegative real number. integral uses the absolute error tolerance to limit an estimate of the absolute error, |qQ|, where q is the computed value of the integral and Q is the (unknown) exact value. integral might provide more decimal places of precision if you decrease the absolute error tolerance. The default value is 1e-10.

    Note:   AbsTol and RelTol work together. integral might satisfy the absolute error tolerance or the relative error tolerance, but not necessarily both. For more information on using these tolerances, see the Tips section.

Example: 'AbsTol',1e-12 sets the absolute error tolerance to approximately 12 decimal places of accuracy.

Data Types: single | double

'RelTol' — Relative error tolerancenonnegative real number

Relative error tolerance, specified as the comma-separated pair consisting of 'RelTol' and a nonnegative real number. integral uses the relative error tolerance to limit an estimate of the relative error, |qQ|/|Q|, where q is the computed value of the integral and Q is the (unknown) exact value. integral might provide more significant digits of precision if you decrease the relative error tolerance. The default value is 1e-6.

    Note:   RelTol and AbsTol work together. integral might satisfy the relative error tolerance or the absolute error tolerance, but not necessarily both. For more information on using these tolerances, see the Tips section.

Example: 'RelTol',1e-9 sets the relative error tolerance to approximately 9 significant digits.

Data Types: single | double

'ArrayValued' — Array-valued function flagfalse (default) | true | 0 | 1

Array-valued function flag, specified as the comma-separated pair consisting of 'ArrayValued' and either false, true, 0, or 1. Set this flag to true when you want to integrate over an array-valued function. The shape of fun(x) can be a vector, matrix, or N-D array.

Example: 'ArrayValued',true indicates that the integrand is an array-valued function.

'Waypoints' — Integration waypointsvector

Integration waypoints, specified as the comma-separated pair consisting of 'Waypoints' and a vector of real or complex numbers. Use waypoints to indicate any points in the integration interval you would like the integrator to use. You can use waypoints to integrate efficiently across discontinuities of the integrand. Specify the locations of the discontinuities in the vector you supply.

You can specify waypoints when you want to perform complex contour integration. If xmin, xmax, or any entry of the waypoints vector is complex, the integration is performed over a sequence of straight line paths in the complex plane.

Example: 'Waypoints',[1+1i,1-1i] specifies two complex waypoints along the interval of integration.

Data Types: single | double
Complex Number Support: Yes

Output Arguments

expand all

q — Computed integralnumeric value | numeric array

Computed integral of fun(x) between the limits xmin and xmax, returned as a numeric value or numeric array.

More About

expand all

Tips

  • Do not use waypoints to specify singularities. Instead, split the interval and add the results of separate integrations with the singularities at the endpoints.

  • The integral function attempts to satisfy:

    abs(q - Q) <= max(AbsTol,RelTol*abs(q))

    where q is the computed value of the integral and Q is the (unknown) exact value. The absolute and relative tolerances provide a way of trading off accuracy and computation time. Usually, the relative tolerance determines the accuracy of the integration. However if abs(q) is sufficiently small, the absolute tolerance determines the accuracy of the integration. You should generally specify both absolute and relative tolerances together.

  • If you specify a complex value for xmin, xmax, or any waypoint, all of your limits and waypoints must be finite.

  • If you are specifying single-precision limits of integration, or if fun returns single-precision results, you might need to specify larger absolute and relative error tolerances.

See Also

| | |

Was this topic helpful?