Main Content


Numerical integration using variable precision



vpaintegral(f,a,b) numerically approximates f from a to b. The default variable x in f is found by symvar.

vpaintegral(f,[a b]) is equal to vpaintegral(f,a,b).


vpaintegral(f,x,a,b) performs numerical integration using the integration variable x.


vpaintegral(___,Name,Value) uses additional options specified by one or more Name,Value pair arguments.


Numerically Integrate Symbolic Expression

Numerically integrate the symbolic expression x^2 from 1 to 2.

syms x
vpaintegral(x^2, 1, 2)
ans =

Numerically Integrate Symbolic Function

Numerically integrate the symbolic function y(x) = x2 from 1 to 2.

syms y(x)
y(x) = x^2;
vpaintegral(y, 1, 2)
ans =

High-Precision Numerical Integration

vpaintegral uses variable-precision arithmetic while the MATLAB® integral function uses double-precision arithmetic. Using the default values of tolerance, vpaintegral can handle values that cause the MATLAB integral function to overflow or underflow.

Integrate besseli(5,25*u).*exp(-u*25) by using both integral and vpaintegral. The integral function returns NaN and issues a warning while vpaintegral returns the correct result.

syms u x
f = besseli(5,25*x).*exp(-x*25);
fun = @(u)besseli(5,25*u).*exp(-u*25);

usingIntegral = integral(fun, 0, 30)
usingVpaintegral = vpaintegral(f, 0, 30)
Warning: Infinite or Not-a-Number value encountered. 
usingIntegral =

usingVpaintegral =

Increase Precision Using Tolerances

The digits function does not affect vpaintegral. Instead, increase the precision of vpainteral by decreasing the integration tolerances. Conversely, increase the speed of numerical integration by increasing the tolerances. Control the tolerance used by vpaintegral by changing the relative tolerance RelTol and absolute tolerance AbsTol, which affect the integration through the condition

|QI|max(AbsTol,|Q|·RelTol)whereQ=Calculated IntegralI=Exact Integral.

Numerically integrate besselj(0,x) from 0 to pi, to 32 significant figures by setting RelTol to 10^(-32). Turn off AbsTol by setting it to 0.

syms x
vpaintegral(besselj(0,x), [0 pi], 'RelTol', 1e-32, 'AbsTol', 0)
ans =

Using lower tolerance values increases precision at the cost of speed.

Complex Path Integration Using Waypoints

Integrate 1/(2*z-1) over the triangular path from 0 to 1+1i to 1-1i back to 0 by specifying waypoints.

syms z
vpaintegral(1/(2*z-1), [0 0], 'Waypoints', [1+1i 1-1i])
ans =
- 8.67362e-19 - 3.14159i

Reversing the direction of the integral, by changing the order of the waypoints and exchanging the limits, changes the sign of the result.

Multiple Integrals

Perform multiple integration by nesting calls to vpaintegral. Integrate


syms x y
vpaintegral(vpaintegral(x*y, x, [1 3]), y, [-1 2])
ans =

The limits of integration can be symbolic expressions or functions. Integrate over the triangular region 0 ≤ x ≤ 1 and |y| < x by specifying the limits of the integration over y in terms of x.

vpaintegral(vpaintegral(sin(x-y)/(x-y), y, [-x x]), x, [0 1])
ans =

Input Arguments

collapse all

Expression or function to integrate, specified as a symbolic number, variable, vector, matrix, multidimensional array, function, or expression.

Limits of integration, specified as a list of two numbers, symbolic numbers, symbolic variables, symbolic functions, or symbolic expressions.

Integration variable, specified as a symbolic variable. If x is not specified, the integration variable is found by symvar.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'RelTol',1e-20

Relative error tolerance, specified as a positive real number. The default is 1e-6. The RelTol argument determines the accuracy of the integration only if RelTol·|Q|>AbsTol, where Q is the calculated integral. In this case, vpaintegral satisfies the condition |QI|RelTol·|Q|, where I is the exact integral value. To use only RelTol and turn off AbsTol, set AbsTol to 0.

Example: 1e-8

Absolute error tolerance, specified as a non-negative real number. The default is 1e-10. AbsTol determines the accuracy of the integration if AbsTol>RelTol·|Q|, where Q is the calculated integral. In this case, vpaintegral satisfies the condition |QI|AbsTol, where I is the exact integral value. To turn off AbsTol and use only RelTol, set AbsTol to 0.

Example: 1e-12

Integration path, specified as a vector of numbers, or as a vector of symbolic numbers, expressions, or functions. vpaintegral integrates along the sequence of straight-line paths (lower limit to the first waypoint, from the first to the second waypoint, and so on) and finally from the last waypoint to the upper limit. For contour integrals, set equal lower and upper limits and define the contour using waypoints.

Maximum evaluations of input, specified as a positive integer or a positive symbolic integer. The default value is 10^5. If the number of evaluations of f is greater than MaxFunctionCalls, then vpaintegral throws an error. For unlimited evaluations, set MaxFunctionCalls to Inf.


  • Ensure that the input is integrable. If the input is not integrable, the output of vpaintegral is unpredictable.

  • The digits function does not affect vpaintegral. To increase precision, use the RelTol and AbsTol arguments instead.

Version History

Introduced in R2016b

See Also

| | |