Main Content

integral

Numerical integration

Description

example

q = integral(fun,xmin,xmax)numerically integrates functionfunfromxmintoxmaxusing global adaptive quadrature and default error tolerances.

example

q = integral(fun,xmin,xmax,名称,值)specifies additional options with one or more名称,值pair arguments. For example, specify'WayPoints'followed by a vector of real or complex numbers to indicate specific points for the integrator to use.

Examples

collapse all

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

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

Evaluate the integral fromx=0tox=Inf.

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

Create the function f ( x ) = 1 / ( x 3 - 2 x - c ) with one parameter, c .

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

Evaluate the integral fromx=0tox=2atc=5.

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

SeeParameterizing Functionsfor more information on this technique.

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

fun = @(x)log(x);

Evaluate the integral fromx=0tox=1with the default error tolerances.

formatlongq1 = integral(fun,0,1)
q1 = -1.000000010959678

Evaluate the integral again, this time with 12 decimal places of accuracy. SetRelTolto zero so thatintegralonly attempts to satisfy the absolute error tolerance.

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

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

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

Integrate in the complex plane over the triangular path from0to1+1ito1-1ito0by specifying waypoints.

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

Create the vector-valued function f ( x ) = [ sin x , sin 2 x , sin 3 x , sin 4 x , sin 5 x ] 和中国ate fromx=0tox=1. Specify'ArrayValued',trueto evaluate the integral of an array-valued or vector-valued function.

fun = @(x)sin((1:5)*x); q = integral(fun,0,1,'ArrayValued',真正的)
q =1×50.4597 0.7081 0.6633 0.4134 0.1433

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

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

Evaluate the integral fromx=0tox=Inf, adjusting the absolute and relative tolerances.

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

Input Arguments

collapse all

Integrand, specified as a function handle, which defines the function to be integrated fromxmintoxmax.

For scalar-valued problems, the functiony = fun(x)must accept a vector argument,x, and return a vector result,y. This generally means thatfunmust use array operators instead of matrix operators. For example, use.*(times) rather than*(mtimes). If you set the'ArrayValued'option totrue, thenfunmust accept a scalar and return an array of fixed size.

Lower limit ofx, specified as a real (finite or infinite) scalar value or a complex (finite) scalar value. If eitherxminorxmaxare complex, thenintegralapproximates the path integral fromxmintoxmaxover a straight line path.

Data Types:double|single
Complex Number Support:Yes

Upper limit ofx, specified as a real number (finite or infinite) or a complex number (finite). If eitherxminorxmaxare complex,integralapproximates the path integral fromxmintoxmaxover a straight line path.

Data Types:double|single
Complex Number Support:Yes

Name-Value Pair Arguments

Specify optional comma-separated pairs of名称,值arguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:integral(fun,a,b,'AbsTol',1e-12)sets the absolute error tolerance to approximately 12 decimal places of accuracy.

Absolute error tolerance, specified as the comma-separated pair consisting of'AbsTol'和一个非负实数。integraluses the absolute error tolerance to limit an estimate of the absolute error, |qQ|, whereqis the computed value of the integral andQis the (unknown) exact value.integralmight provide more decimal places of precision if you decrease the absolute error tolerance.

Note

AbsTolandRelTolwork together.integralmight satisfy the absolute error tolerance or the relative error tolerance, but not necessarily both. For more information on using these tolerances, see theTipssection.

Example:integral(fun,a,b,'AbsTol',1e-12)sets the absolute error tolerance to approximately 12 decimal places of accuracy.

Data Types:single|double

Relative error tolerance, specified as the comma-separated pair consisting of'RelTol'和一个非负实数。integraluses the relative error tolerance to limit an estimate of the relative error, |qQ|/|Q|, whereqis the computed value of the integral andQis the (unknown) exact value.integralmight provide more significant digits of precision if you decrease the relative error tolerance.

Note

RelTolandAbsTolwork together.integralmight satisfy the relative error tolerance or the absolute error tolerance, but not necessarily both. For more information on using these tolerances, see theTipssection.

Example:integral(fun,a,b,'RelTol',1e-9)sets the relative error tolerance to approximately 9 significant digits.

Data Types:single|double

Array-valued function flag, specified as the comma-separated pair consisting of'ArrayValued'and a numeric or logical1(true) or0(false). Set this flag totrueor1to indicate thatfunis a function that accepts a scalar input and returns a vector, matrix, or N-D array output.

The default value offalseindicates thatfunis a function that accepts a vector input and returns a vector output.

Example:integral(fun,a,b,'ArrayValued',true)indicates that the integrand is an array-valued function.

Integration waypoints, specified as the comma-separated pair consisting of'Waypoints'and a vector of real or complex numbers. Use waypoints to indicate points in the integration interval that you would like the integrator to use in the initial mesh:

  • Add more evaluation points near interesting features of the function, such as a local extrema.

  • Integrate efficiently across discontinuities of the integrand by specifying the locations of the discontinuities.

  • Perform complex contour integrations by specifying complex numbers as waypoints. Ifxmin,xmax, or any entry of the waypoints vector is complex, then the integration is performed over a sequence of straight line paths in the complex plane. In this case, all of the integration limits and waypoints must be finite.

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

Example:integral(fun,a,b,'Waypoints',[1+1i,1-1i])specifies two complex waypoints along the interval of integration.

Data Types:single|double
Complex Number Support:Yes

Tips

  • Theintegralfunction attempts to satisfy:

    abs(q - Q) <= max(AbsTol,RelTol*abs(q))
    whereqis the computed value of the integral andQis 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 ifabs(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 are specifying single-precision limits of integration, or iffunreturns single-precision results, you might need to specify larger absolute and relative error tolerances.

References

[1] L.F. Shampine “Vectorized Adaptive Quadrature in MATLAB®,”Journal of Computational and Applied Mathematics, 211, 2008, pp.131–140.

Introduced in R2012a