Numerical integration - MATLAB integral (2024)

Numerical integration

collapse all in page

Syntax

q = integral(fun,xmin,xmax)

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

Description

example

q = integral(fun,xmin,xmax) numericallyintegrates function fun from xmin to xmax usingglobal adaptive quadrature and default error tolerances.

example

q = integral(fun,xmin,xmax,Name,Value) specifiesadditional options with one or more Name,Value pairarguments. For example, specify 'WayPoints' followedby a vector of real or complex numbers to indicate specific pointsfor the integrator to use.

Examples

collapse all

Improper Integral

Open Live Script

Create the function f(x)=e-x2(lnx)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

Parameterized Function

Open Live Script

Create the function f(x)=1/(x3-2x-c) 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

See Parameterizing Functions for more information on this technique.

Singularity at Lower Limit

Open Live Script

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

fun = @(x)log(x);

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

format longq1 = integral(fun,0,1)
q1 = -1.000000010959678

Evaluate the integral again, this time with 12 decimal places of accuracy. Set RelTol to zero so that integral only attempts to satisfy the absolute error tolerance.

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

Complex Contour Integration Using Waypoints

Open Live Script

Create the 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 by specifying waypoints.

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

Vector-Valued Function

Open Live Script

Create the vector-valued function f(x)=[sinx,sin2x,sin3x,sin4x,sin5x] and integrate from x=0 to x=1. Specify 'ArrayValued',true to evaluate the integral of an array-valued or vector-valued function.

fun = @(x)sin((1:5)*x);q = integral(fun,0,1,'ArrayValued',true)
q = 1×5 0.4597 0.7081 0.6633 0.4134 0.1433

Improper Integral of Oscillatory Function

Open Live Script

Create the function f(x)=x5e-xsinx.

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

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

Input Arguments

collapse all

funIntegrand
function handle

Integrand, specified as a function handle, which defines thefunction 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, then fun must accept a scalar and return an array of fixed size.

xminLower limit of x
real number | complex number

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

Data Types: double | single
Complex Number Support: Yes

xmaxUpper limit of x
real number | complex number

Upper limit of x, specified as a real number(finite or infinite) or a complex number (finite). If either xmin or xmax arecomplex, integral approximates the path integralfrom xmin to xmax over a straightline path.

Data Types: double | single
Complex Number Support: Yes

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: integral(fun,a,b,'AbsTol',1e-12) sets the absolute error tolerance to approximately 12 decimal places of accuracy.

AbsTolAbsolute error tolerance
1e-10 (default) | nonnegative 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.

Note

AbsTol and RelTol worktogether. integral might satisfy the absoluteerror tolerance or the relative error tolerance, but not necessarilyboth. For more information on using these tolerances, see the Tips section.

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

Data Types: single | double

RelTolRelative error tolerance
1e-6 (default) | nonnegative 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.

Note

RelTol and AbsTol worktogether. integral might satisfy the relativeerror tolerance or the absolute error tolerance, but not necessarilyboth. For more information on using these tolerances, see the Tips section.

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

Data Types: single | double

ArrayValuedArray-valued function flag
false or 0 (default) | true or 1

Array-valued function flag, specified as the comma-separated pair consisting of 'ArrayValued' and a numeric or logical 1 (true) or 0 (false). Set this flag to true or 1 to indicate that fun is a function that accepts a scalar input and returns a vector, matrix, or N-D array output.

The default value of false indicates that fun is 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.

WaypointsIntegration waypoints
vector

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. If xmin, 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

  • 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 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.

References

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

Extended Capabilities

Version History

Introduced in R2012a

See Also

integral2 | integral3 | trapz

Topics

  • Integration of Numeric Data
  • Integration to Find Arc Length
  • Complex Line Integrals
  • Create Function Handle
  • Parameterizing Functions

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Numerical integration - MATLAB integral (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Numerical integration - MATLAB integral (2024)

FAQs

How do you do an integral in MATLAB? ›

q = integral( fun , xmin , xmax ) numerically integrates function fun from xmin to xmax using global adaptive quadrature and default error tolerances. q = integral( fun , xmin , xmax , Name,Value ) specifies additional options with one or more Name,Value pair arguments.

What is the MATLAB program for definite integral? ›

F = int( expr , a , b ) computes the definite integral of expr from a to b . int uses the default integration variable determined by symvar ( expr,1 ). If expr is a constant, then the default integration variable is x . int(expr,[a b]) is equivalent to int(expr,a,b) .

How do you do numerical integration? ›

The most commonly used techniques for numerical integration are the midpoint rule, trapezoidal rule, and Simpson's rule. The midpoint rule approximates the definite integral using rectangular regions whereas the trapezoidal rule approximates the definite integral using trapezoidal approximations.

What is the difference between Trapz and Cumtrapz in MATLAB? ›

trapz reduces the size of the dimension it operates on to 1, and returns only the final integration value. cumtrapz also returns the intermediate integration values, preserving the size of the dimension it operates on.

How do you insert an integral? ›

On a Windows PC, click to the place in your document where you would like to insert the integral symbol. Next, hold the Alt key and, using your numeric keypad, type the numbers 8747. You can also insert the integral symbol into a Microsoft Word document using the Advanced Symbols library.

How to plot an integral equation in MATLAB? ›

Like so:
  1. K= @(t) (sin(t)-t.*cos(t)).*sin(t);
  2. P = 0:0.1:3.1416;
  3. for i = 1:numel(P)
  4. V(i) = integral(K,0,P(i));
  5. end.
  6. plot(P,V),grid.
Mar 3, 2022

What is the integrator in MATLAB? ›

The Integrator block calculates the state value before the output value in each time step. Because the state value is available before the output value, you can use the state value to create a self-resetting Integrator block without creating an algebraic loop.

How to do differentiation and integration in MATLAB? ›

Integrate and Differentiate Polynomials
  1. Use polyder to obtain the derivative of the polynomial p ( x ) = x 3 - 2 x - 5 . ...
  2. Similarly, use polyint to integrate the polynomial p ( x ) = 4 x 3 - 3 x 2 + 1 . ...
  3. polyder also computes the derivative of the product or quotient of two polynomials.

How to calculate integral image in MATLAB? ›

J — Integral image

The class of the output is double . The resulting size of the output integral image equals: size(J) = size(I) + 1 . Such sizing facilitates easy computation of pixel sums along all image boundaries. The integral image, J , is essentially a padded version of the value c*msum(c*msum(c*msum(I),2),3) .

What is the best numerical integration method? ›

If the functions are known analytically instead of being tabulated at equally spaced intervals, the best numerical method of integration is called Gaussian quadrature. By picking the abscissas at which to evaluate the function, Gaussian quadrature produces the most accurate approximations possible.

What is the difference between integration and numerical integration? ›

Ordinary integration is usually done with pencil and paper and it requires analytical functions. In numerical integration, the functions don't even need to be functions. They are just strings of numbers and it is all done by computer.

Is numerical integration hard? ›

As we start to see that integration 'by formulas' is a much more difficult thing than differentiation, and sometimes is impossible to do in elementary terms, it becomes reasonable to ask for numerical approximations to definite integrals.

What is Trapz definite integral in MATLAB? ›

Q = trapz( X , Y ) integrates Y with respect to the coordinates or scalar spacing specified by X . If X is a vector of coordinates, then length(X) must be equal to the size of the first dimension of Y whose size does not equal 1. If X is a scalar spacing, then trapz(X,Y) is equivalent to X*trapz(Y) .

How to use convolution integral in MATLAB? ›

Convolution takes two functions and “slides” one of them over the other, multiplying the function values at each point where they overlap, and adding up the products to create a new function. This process creates a new function that represents how the two original functions interact with each other.

What is trapez in MATLAB? ›

trapz (MATLAB Functions) Z = trapz(Y) computes an approximation of the integral of Y via the trapezoidal method (with unit spacing). To compute the integral for spacing other than one, multiply Z by the spacing increment. If Y is a vector, trapz(Y) is the integral of Y .

How do you create an integer in MATLAB? ›

Float and double data can be changed to integer using int32 and int64 depending on how large the float/double number is.
  1. Ex: If X is the number to be converted, then X can be converted to integer in this way :
  2. X = int32(X) or X= int64(X)
  3. (X is converted to integer and then the converted integer is stored back in X)
Jun 27, 2022

What is the Integrator in MATLAB? ›

The Integrator block calculates the state value before the output value in each time step. Because the state value is available before the output value, you can use the state value to create a self-resetting Integrator block without creating an algebraic loop.

How do you write an integral function? ›

  1. We define integrals as the function of the area bounded by the curve y = f(x), a ≤ x ≤ b, the x-axis, and the ordinates x = a and x =b, where b>a. ...
  2. A(x) = b∫af(x)dx ∫ a b f ( x ) d x for all x ≥ a, where the function is continuous on [a,b].

References

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 5924

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.