# Integrate on matlab

An integral is a mathematical measure that combines infinitesimal data points. Integrals have a broad range of applications in all engineering disciplines.

Types of Integrals

In general, integrals can be either definite or indefinite. Definite integrals represent functions with bounded upper and lower limits, whereas indefinite Integrals represent functions without limits.

The following example shows an indefinite integral:

$$I= \int3x^2 dx=x^3+c$$

where ‘c’ is a constant.

A definite integral for the same equation must have defined limits. For example, we can integrate the above equation with limits [-2, 2] as follows:

$$I= \int_{-2}^2 3x^2 dx=(2^3+c)-(-2^3+c)=16$$

You can use MATLAB® and Symbolic Math Toolbox™ to calculate integrals numerically and symbolically.

#### Examples of Integral Applications

1. Area under curves:

You can calculate the area under two curves using integrals. For example, we define two curves,

$$x1=y^2-1$$

$$x2=1- y^2$$

and calculate the area under the curves as follows:

$$A=∫(x2-x1) dy=\frac{(-2y(y^2-3))}{3}$$

The area under the curve$$'A’$$ here is a function of $$'y’$$ because we did not specify limits. If we define the limits as $$[-1, 1]$$, the integral returns a value of:

$$A=8/3.$$

1. Volume of objects:

You can use integrals to calculate the volume of objects. For example, you can derive the volume of a sphere by starting with a function:

$$f(x)=√(r^2-x^2 )$$

which depicts a semi-circle with radius ‘r.’ Rotating this semi-circle around the x-axis would yield a sphere.

The area of the semi-circle would be

$$A=πf(x)^2$$

Integrating this area with limits [-r, +r] would give us the volume of the sphere:

$$V= ∫_{-r}^{+r} A dx=\frac{(4πr^3)}{3}$$

1. Velocity of a moving object:

You can find the velocity of an object by finding the definite integral of the object’s acceleration with respect to time, because acceleration is simply defined as the rate of change of velocity over time.

$$∆Vel= ∫Acc \; dt$$

#### Techniques to Calculate Integrals

You can calculate integrals numerically using techniques such as:

For more information on the numeric and symbolic calculations of integrals, see MATLAB® and Symbolic Math Toolbox™.

Sours: https://www.mathworks.com/discovery/integral.html

## trapz

### Description

example

computes the approximate integral of via the trapezoidal method with unit spacing. The size of determines the dimension to integrate along:

• If is a vector, then is the approximate integral of .

• If is a matrix, then integrates over each column and returns a row vector of integration values.

• If is a multidimensional array, then integrates over the first dimension whose size does not equal 1. The size of this dimension becomes 1, and the sizes of other dimensions remain unchanged.

example

integrates with respect to the coordinates or scalar spacing specified by .

• If is a vector of coordinates, then must be equal to the size of the first dimension of whose size does not equal 1.

• If is a scalar spacing, then is equivalent to .

example

integrates along the dimension using any of the previous syntaxes. You must specify , and optionally can specify . If you specify , then it can be a scalar or a vector with length equal to . For example, if is a matrix, then integrates each row of .

collapse all

### Integrate Vector of Data with Unit Spacing

Calculate the integral of a vector where the spacing between data points is 1.

Create a numeric vector of data.

contains function values for in the domain [1, 5].

Use to integrate the data with unit spacing.

This approximate integration yields a value of . In this case, the exact answer is a little less, . The function overestimates the value of the integral because f(x) is concave up.

### Integrate Vector of Data with Nonunit Spacing

Calculate the integral of a vector where the spacing between data points is uniform, but not equal to 1.

Create a domain vector.

Calculate the sine of .

Integrate using .

When the spacing between points is constant, but not equal to 1, an alternative to creating a vector for is to specify the scalar spacing value. In that case, is the same as .

### Integrate Matrix with Nonuniform Spacing

Integrate the rows of a matrix where the data has a nonuniform spacing.

Create a vector of x-coordinates and a matrix of observations that take place at the irregular intervals. The rows of represent velocity data, taken at the times contained in , for three different trials.

X = [1 2.5 7 10]; Y = [5.2 7.7 9.6 13.2; 4.8 7.0 10.5 14.5; 4.9 6.5 10.2 13.8];

Use to integrate each row independently and find the total distance traveled in each trial. Since the data is not evaluated at constant intervals, specify to indicate the spacing between the data points. Specify since the data is in the rows of .

Q1 = 3×1 82.8000 85.7250 82.1250

The result is a column vector of integration values, one for each row in .

### Multiple Numerical Integrations

Create a grid of domain values.

x = -3:.1:3; y = -5:.1:5; [X,Y] = meshgrid(x,y);

Calculate the function on the grid.

integrates numeric data rather than functional expressions, so in general the expression does not need to be known to use on a matrix of data. In cases where the functional expression is known, you can instead use , , or .

Use to approximate the double integral

To perform double or triple integrations on an array of numeric data, nest function calls to .

I = trapz(y,trapz(x,F,2))

performs the integration over x first, producing a column vector. Then, the integration over y reduces the column vector to a single scalar. slightly overestimates the exact answer of 680 because f(x,y) is concave up.

collapse all

### — Numeric datavector | matrix | multidimensional array

Numeric data, specified as a vector, matrix, or multidimensional array. By default, integrates along the first dimension of whose size does not equal 1.

Data Types: |
Complex Number Support: Yes

### — Point spacing (default) | uniform scalar spacing | vector of coordinates

Point spacing, specified as (default), a uniform scalar spacing, or a vector of coordinates.

• If is a scalar, then it specifies a uniform spacing between the data points and is equivalent to .

• If is a vector, then it specifies x-coordinates for the data points and must be the same as the size of the integration dimension in .

Data Types: |

### — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array, :

• works on successive elements in the columns of and returns a row vector.

• works on successive elements in the rows of and returns a column vector.

If is greater than , then returns an array of zeros of the same size as .

collapse all

### Trapezoidal Method

performs numerical integration via the trapezoidal method. This method approximates the integration over an interval by breaking the area down into trapezoids with more easily computable areas. For example, here is a trapezoidal integration of the sine function using eight evenly-spaced trapezoids:

For an integration with evenly spaced points, the approximation is

where the spacing between each point is equal to the scalar value . By default MATLAB® uses a spacing of 1.

If the spacing between the points is not constant, then the formula generalizes to

where , and is the spacing between each consecutive pair of points.

### Tips

• Use and to perform numerical integrations on discrete data sets. Use , , or instead if a functional expression for the data is available.

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

### C/C++ Code GenerationGenerate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

### Thread-Based EnvironmentRun code in the background using MATLAB® or accelerate code with Parallel Computing Toolbox™ .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

### GPU ArraysAccelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

### Distributed ArraysPartition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Introduced before R2006a

You have a modified version of this example. Do you want to open this example with your edits?

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.

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

Select web site

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

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

Sours: https://www.mathworks.com/help/matlab/ref/trapz.html

## Select a Web Site

### Integration

If is a symbolic expression, then

attempts to find another symbolic expression, , so that   . That is, returns the indefinite integral or antiderivative of (provided one exists in closed form). Similar to differentiation,

uses the symbolic object as the variable of integration, rather than the variable determined by . See how works by looking at this table.

Mathematical Operation

MATLAB® Command

or

or

g  =  cos(at + b)

or

or

In contrast to differentiation, symbolic integration is a more complicated task. A number of difficulties can arise in computing the integral:

• The antiderivative, , may not exist in closed form.

• The antiderivative may define an unfamiliar function.

• The antiderivative may exist, but the software can't find it.

• The software could find the antiderivative on a larger computer, but runs out of time or memory on the available machine.

Nevertheless, in many cases, MATLAB can perform symbolic integration successfully. For example, create the symbolic variables

The following table illustrates integration of expressions containing those variables.

f

int(f)

ans = piecewise(n == -1, log(x), n ~= -1,... x^(n + 1)/(n + 1))
syms a b theta f = sin(a*theta+b);
ans = -cos(b + a*theta)/a
ans = (pi^(1/2)*erf(x))/2

In the last example, , there is no formula for the integral involving standard calculus expressions, such as trigonometric and exponential functions. In this case, MATLAB returns an answer in terms of the error function .

If MATLAB is unable to find an answer to the integral of a function , it just returns .

Definite integration is also possible.

Definite Integral

Command

Here are some additional examples.

f

a, b

int(f, a, b)

syms x f = log(x)*sqrt(x);
syms z f = besselj(1,z)^2;
ans = hypergeom([3/2, 3/2],... [2, 5/2, 3], -1)/12

For the Bessel function () example, it is possible to compute a numerical approximation to the value of the integral, using the function. The commands

syms z a = int(besselj(1,z)^2,0,1)

return

a = hypergeom([3/2, 3/2], [2, 5/2, 3], -1)/12

and the command

returns

### Integration with Real Parameters

One of the subtleties involved in symbolic integration is the “value” of various parameters. For example, if a is any positive real number, the expression

is the positive, bell shaped curve that tends to 0 as x tends to ±∞. You can create an example of this curve, for a = 1/2.

syms x a = sym(1/2); f = exp(-a*x^2); fplot(f)

However, if you try to calculate the integral

without assigning a value to a, MATLAB assumes that a represents a complex number, and therefore returns a piecewise answer that depends on the argument of a. If you are only interested in the case when a is a positive real number, use to set an assumption on :

Now you can calculate the preceding integral using the commands

syms x f = exp(-a*x^2); int(f, x, -inf, inf)

This returns

### Integration with Complex Parameters

To calculate the integral

for complex values of , enter

syms a x f = 1/(a^2 + x^2); F = int(f, x, -inf, inf)

Use to clear the all assumptions on variables. For more information about symbolic variables and assumptions on them, see Delete Symbolic Objects and Their Assumptions.

The preceding commands produce the complex output

The function is defined as:

To evaluate at , enter

### High-Precision Numerical Integration Using Variable-Precision Arithmetic

High-precision numerical integration is implemented in the function of the Symbolic Math Toolbox™. uses variable-precision arithmetic in contrast to the MATLAB function, which uses double-precision arithmetic.

Integrate by using both and . The function returns and issues a warning while returns the correct result.

syms u 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 = NaN usingVpaintegral = 0.688424

Sours: https://www.mathworks.com/help/symbolic/integration.html
Matlab Training - 57 - Indefinite and Definite Integrals

## MATLAB - Integration

Integration deals with two essentially different types of problems.

• In the first type, derivative of a function is given and we want to find the function. Therefore, we basically reverse the process of differentiation. This reverse process is known as anti-differentiation, or finding the primitive function, or finding an indefinite integral.

• The second type of problems involve adding up a very large number of very small quantities and then taking a limit as the size of the quantities approaches zero, while the number of terms tend to infinity. This process leads to the definition of the definite integral.

Definite integrals are used for finding area, volume, center of gravity, moment of inertia, work done by a force, and in numerous other applications.

### Finding Indefinite Integral Using MATLAB

By definition, if the derivative of a function f(x) is f'(x), then we say that an indefinite integral of f'(x) with respect to x is f(x). For example, since the derivative (with respect to x) of x2 is 2x, we can say that an indefinite integral of 2x is x2.

In symbols −

f'(x2) = 2x, therefore,

∫ 2xdx = x2.

Indefinite integral is not unique, because derivative of x2 + c, for any value of a constant c, will also be 2x.

This is expressed in symbols as −

∫ 2xdx = x2 + c.

Where, c is called an 'arbitrary constant'.

MATLAB provides an int command for calculating integral of an expression. To derive an expression for the indefinite integral of a function, we write −

int(f);

For example, from our previous example −

syms x int(2*x)

MATLAB executes the above statement and returns the following result −

ans = x^2

### Example 1

In this example, let us find the integral of some commonly used expressions. Create a script file and type the following code in it −

syms x n int(sym(x^n)) f = 'sin(n*t)' int(sym(f)) syms a t int(a*cos(pi*t)) int(a^x)

When you run the file, it displays the following result −

ans = piecewise([n == -1, log(x)], [n ~= -1, x^(n + 1)/(n + 1)]) f = sin(n*t) ans = -cos(n*t)/n ans = (a*sin(pi*t))/pi ans = a^x/log(a)

### Example 2

Create a script file and type the following code in it −

syms x n int(cos(x)) int(exp(x)) int(log(x)) int(x^-1) int(x^5*cos(5*x)) pretty(int(x^5*cos(5*x))) int(x^-5) int(sec(x)^2) pretty(int(1 - 10*x + 9 * x^2)) int((3 + 5*x -6*x^2 - 7*x^3)/2*x^2) pretty(int((3 + 5*x -6*x^2 - 7*x^3)/2*x^2))

Note that the pretty function returns an expression in a more readable format.

When you run the file, it displays the following result −

ans = sin(x) ans = exp(x) ans = x*(log(x) - 1) ans = log(x) ans = (24*cos(5*x))/3125 + (24*x*sin(5*x))/625 - (12*x^2*cos(5*x))/125 + (x^4*cos(5*x))/5 - (4*x^3*sin(5*x))/25 + (x^5*sin(5*x))/5 2 4 24 cos(5 x) 24 x sin(5 x) 12 x cos(5 x) x cos(5 x) ----------- + ------------- - -------------- + ------------ 3125 625 125 5 3 5 4 x sin(5 x) x sin(5 x) ------------- + ----------- 25 5 ans = -1/(4*x^4) ans = tan(x) 2 x (3 x - 5 x + 1) ans = - (7*x^6)/12 - (3*x^5)/5 + (5*x^4)/8 + x^3/2 6 5 4 3 7 x 3 x 5 x x - ---- - ---- + ---- + -- 12 5 8 2

### Finding Definite Integral Using MATLAB

By definition, definite integral is basically the limit of a sum. We use definite integrals to find areas such as the area between a curve and the x-axis and the area between two curves. Definite integrals can also be used in other situations, where the quantity required can be expressed as the limit of a sum.

The int function can be used for definite integration by passing the limits over which you want to calculate the integral.

To calculate

we write,

int(x, a, b)

For example, to calculate the value of we write −

int(x, 4, 9)

MATLAB executes the above statement and returns the following result −

ans = 65/2

Following is Octave equivalent of the above calculation −

pkg load symbolic symbols x = sym("x"); f = x; c = [1, 0]; integral = polyint(c); a = polyval(integral, 9) - polyval(integral, 4); display('Area: '), disp(double(a));

Octave executes the code and returns the following result −

Area: 32.500

An alternative solution can be given using quad() function provided by Octave as follows −

pkg load symbolic symbols f = inline("x"); [a, ierror, nfneval] = quad(f, 4, 9); display('Area: '), disp(double(a));

Octave executes the code and returns the following result −

Area: 32.500

### Example 1

Let us calculate the area enclosed between the x-axis, and the curve y = x3−2x+5 and the ordinates x = 1 and x = 2.

The required area is given by −

Create a script file and type the following code −

f = x^3 - 2*x +5; a = int(f, 1, 2) display('Area: '), disp(double(a));

When you run the file, it displays the following result −

a = 23/4 Area: 5.7500

Following is Octave equivalent of the above calculation −

pkg load symbolic symbols x = sym("x"); f = x^3 - 2*x +5; c = [1, 0, -2, 5]; integral = polyint(c); a = polyval(integral, 2) - polyval(integral, 1); display('Area: '), disp(double(a));

Octave executes the code and returns the following result −

Area: 5.7500

An alternative solution can be given using quad() function provided by Octave as follows −

pkg load symbolic symbols x = sym("x"); f = inline("x^3 - 2*x +5"); [a, ierror, nfneval] = quad(f, 1, 2); display('Area: '), disp(double(a));

Octave executes the code and returns the following result −

Area: 5.7500

### Example 2

Find the area under the curve: f(x) = x2 cos(x) for −4 ≤ x ≤ 9.

Create a script file and write the following code −

f = x^2*cos(x); ezplot(f, [-4,9]) a = int(f, -4, 9) disp('Area: '), disp(double(a));

When you run the file, MATLAB plots the graph −

The output is given below −

a = 8*cos(4) + 18*cos(9) + 14*sin(4) + 79*sin(9) Area: 0.3326

Following is Octave equivalent of the above calculation −

pkg load symbolic symbols x = sym("x"); f = inline("x^2*cos(x)"); ezplot(f, [-4,9]) print -deps graph.eps [a, ierror, nfneval] = quad(f, -4, 9); display('Area: '), disp(double(a));
Sours: https://www.tutorialspoint.com/matlab/matlab_integration.htm

## int

### Description

example

computes the indefinite integral of . uses the default integration variable determined by (). If is a constant, then the default integration variable is .

example

computes the indefinite integral of with respect to the symbolic scalar variable .

example

computes the definite integral of from to . uses the default integration variable determined by (). If is a constant, then the default integration variable is .

is equivalent to .

example

computes the definite integral of with respect to the symbolic scalar variable from to .

is equivalent to .

example

specifies additional options using one or more pair arguments. For example, specifies that applies additional simplifications to the integrand.

collapse all

### Indefinite Integral of Univariate Expression

Define a univariate expression.

syms x expr = -2*x/(1+x^2)^2;

Find the indefinite integral of the univariate expression.

### Indefinite Integrals of Multivariate Function

Define a multivariate function with variables and .

syms xz f(x,z) = x/(1+z^2);

Find the indefinite integrals of the multivariate expression with respect to the variables and .

Fx(x, z) =
Fz(x, z) =

If you do not specify the integration variable, then uses the first variable returned by as the integration variable.

### Definite Integrals of Symbolic Expressions

Integrate a symbolic expression from to .

syms x expr = x*log(1+x); F = int(expr,[0 1])

Integrate another expression from to .

syms t F = int(2*x,[sin(t) 1])

When cannot compute the value of a definite integral, numerically approximate the integral by using .

syms x f = cos(x)/sqrt(1 + x^2); Fint = int(f,x,[0 10])
Fint =
Fvpa =

To approximate integrals directly, use instead of . The function is faster and provides control over integration tolerances.

Fvpaint = vpaintegral(f,x,[0 10])

### Integrals of Matrix Elements

Define a symbolic matrix containing four expressions as its elements.

syms axtz M = [exp(t) exp(a*t); sin(t) cos(t)]
M =

Find indefinite integrals of the matrix element-wise.

F =

### Apply IgnoreAnalyticConstraints

Define a symbolic function and compute its indefinite integral.

syms f(x) f(x) = acos(cos(x)); F = int(f,x)
F(x) =

By default, uses strict mathematical rules. These rules do not let rewrite as .

If you want a simple practical solution, set to .

F = int(f,x,'IgnoreAnalyticConstraints',true)

### Ignore Special Cases

Define a symbolic expression and compute its indefinite integral with respect to the variable .

F =

By default, returns the general results for all values of the other symbolic parameter . In this example, returns two integral results for the case and .

To ignore special cases of parameter values, set to . With this option, ignores the special case and returns the solution for .

F = int(x^t,x,'IgnoreSpecialCases',true)

### Find Cauchy Principal Value

Define a symbolic function that has a pole at .

Compute the definite integral of this function from to . Since the integration interval includes the pole, the result is not defined.

However, the Cauchy principal value of the integral exists. To compute the Cauchy principal value of the integral, set to .

F = int(f,[0 2],'PrincipalValue',true)

### Unevaluated Integral and Integration by Parts

Find the integral of .

Define the integral without evaluating it by setting the option to .

syms xg(y) F = int(x*exp(x),'Hold',true)

You can apply integration by parts to by using the function. Use as the differential to be integrated.

G = integrateByParts(F,exp(x))

To evaluate the integral in , use the function to ignore the option.

Compare the result to the integration result returned by without setting the option.

### Approximate Indefinite Integrals

If cannot compute a closed form of an integral, then it returns an unresolved integral.

syms f(x) f(x) = sin(sinh(x)); F = int(f,x)
F(x) =

You can approximate the integrand function as polynomials by using the Taylor expansion. Apply to expand the integrand function as polynomials around . Compute the integral of the approximated polynomials.

fTaylor = taylor(f,x,'ExpansionPoint',0,'Order',10)
fTaylor(x) =
Fapprox(x) =

collapse all

### — Integrandsymbolic expression | symbolic function | symbolic vector | symbolic matrix | symbolic number

Integrand, specified as a symbolic expression, function, vector, matrix, or number.

### — Integration variablesymbolic variable

Integration variable, specified as a symbolic variable. If you do not specify this variable, uses the default variable determined by . If is a constant, then the default variable is .

### — Lower boundnumber | symbolic number | symbolic variable | symbolic expression | symbolic function

Lower bound, specified as a number, symbolic number, variable, expression, or function (including expressions and functions with infinities).

### — Upper boundnumber | symbolic number | symbolic variable | symbolic expression | symbolic function

Upper bound, specified as a number, symbolic number, variable, expression, or function (including expressions and functions with infinities).

### Name-Value Arguments

Specify optional comma-separated pairs of arguments. is the argument name and is the corresponding value. must appear inside quotes. You can specify several name and value pair arguments in any order as .

Example: specifies that applies purely algebraic simplifications to the integrand.

### — Indicator for applying purely algebraic simplifications to integrand (default) |

Indicator for applying purely algebraic simplifications to the integrand, specified as or . If the value is , apply purely algebraic simplifications to the integrand. This option can provide simpler results for expressions, for which the direct use of the integrator returns complicated results. In some cases, it also enables to compute integrals that cannot be computed otherwise.

Using this option can lead to results not generally valid. This option applies mathematical identities that are convenient, but the results do not always hold for all values of variables.

### — Indicator for ignoring special cases (default) |

Indicator for ignoring special cases, specified as or . This ignores cases that require one or more parameters to be elements of a comparatively small set, such as a fixed finite set or a set of integers.

### — Indicator for returning principal value (default) |

Indicator for returning the principal value, specified as or . If the value is , compute the Cauchy principal value of the integral. In live script, the Cauchy principal value of unevaluated integral shows as the symbol.

### — Indicator for unevaluated integration (default) |

Indicator for unevaluated integration, specified as or . If the value is , returns integrals without evaluating them.

### Tips

• In contrast to differentiation, symbolic integration is a more complicated task. If cannot compute an integral of an expression, check for these reasons:

• The antiderivative does not exist in a closed form.

• The antiderivative exists, but cannot find it.

If cannot compute a closed form of an integral, it returns an unresolved integral.

Try approximating such integrals by using one of these methods:

• For indefinite integrals, use series expansions. Use this method to approximate an integral around a particular value of the variable.

• For definite integrals, use numeric approximations.

• For indefinite integrals, does not return a constant of integration in the result. The results of integrating mathematically equivalent expressions may be different. For example, returns , while returns , which differs from the first result by .

• For indefinite integrals, implicitly assumes that the integration variable is real. For definite integrals, restricts the integration variable to the specified integration interval. If one or both integration bounds and are not numeric, assumes that unless you explicitly specify otherwise.

### Algorithms

When you use , applies these rules:

• log(a) + log(b) = log(a·b) for all values of a and b. In particular, the following equality is valid for all values of a, b, and c:

(a·b)c = ac·bc.

• log(ab) = b·log(a) for all values of a and b. In particular, the following equality is valid for all values of a, b, and c:

(ab)c = ab·c.

• If f and g are standard mathematical functions and f(g(x)) = x for all small positive numbers, then f(g(x)) = x is assumed to be valid for all complex values x. In particular:

• log(ex) = x

• asin(sin(x)) = x, acos(cos(x)) = x, atan(tan(x)) = x

• asinh(sinh(x)) = x, acosh(cosh(x)) = x, atanh(tanh(x)) = x

• Wk(x·ex) = x for all branch indices k of the Lambert W function.

Introduced before R2006a

You have a modified version of this example. Do you want to open this example with your edits?

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.

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

Select web site

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

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

Sours: https://www.mathworks.com/help/symbolic/int.html
Matlab Essential Skills Sect 41 Indefinite and Definite Integrals

## integral

### Description

example

numerically integrates function from to using global adaptive quadrature and default error tolerances.

example

specifies additional options with one or more pair arguments. For example, specify followed by a vector of real or complex numbers to indicate specific points for the integrator to use.

collapse all

### Improper Integral

Create the function .

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

Evaluate the integral from to .

### Parameterized Function

Create the function with one parameter, .

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

Evaluate the integral from to at .

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

See Parameterizing Functions for more information on this technique.

### Singularity at Lower Limit

Create the function .

Evaluate the integral from to with the default error tolerances.

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

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

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

### Complex Contour Integration Using Waypoints

Create the function .

Integrate in the complex plane over the triangular path from to to to by specifying waypoints.

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

### Vector-Valued Function

Create the vector-valued function and integrate from to . Specify 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

Create the function .

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

Evaluate the integral from to , adjusting the absolute and relative tolerances.

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

collapse all

### — Integrandfunction handle

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

For scalar-valued problems, the function must accept a vector argument, , and return a vector result, . This generally means that must use array operators instead of matrix operators. For example, use () rather than (). If you set the option to , then must accept a scalar and return an array of fixed size.

### — 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 or are complex, then approximates the path integral from to over a straight line path.

Data Types: |
Complex Number Support: Yes

### — Upper limit of xreal number | complex number

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

Data Types: |
Complex Number Support: Yes

### Name-Value Arguments

Specify optional comma-separated pairs of arguments. is the argument name and is the corresponding value. must appear inside quotes. You can specify several name and value pair arguments in any order as .

Example: sets the absolute error tolerance to approximately 12 decimal places of accuracy.

### — Absolute error tolerance (default) | nonnegative real number

Absolute error tolerance, specified as the comma-separated pair consisting of and a nonnegative real number. 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. might provide more decimal places of precision if you decrease the absolute error tolerance.

Note

and work together. 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: sets the absolute error tolerance to approximately 12 decimal places of accuracy.

Data Types: |

### — Relative error tolerance (default) | nonnegative real number

Relative error tolerance, specified as the comma-separated pair consisting of and a nonnegative real number. 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. might provide more significant digits of precision if you decrease the relative error tolerance.

Note

and work together. 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: sets the relative error tolerance to approximately 9 significant digits.

Data Types: |

### — Array-valued function flag or (default) | or

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

The default value of indicates that is a function that accepts a vector input and returns a vector output.

Example: indicates that the integrand is an array-valued function.

### — Integration waypointsvector

Integration waypoints, specified as the comma-separated pair consisting of 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 , , 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: specifies two complex waypoints along the interval of integration.

Data Types: |
Complex Number Support: Yes

### Tips

• The function attempts to satisfy:

abs(q - Q) <= max(AbsTol,RelTol*abs(q))
where is the computed value of the integral and 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 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 returns 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.

### Thread-Based EnvironmentRun code in the background using MATLAB® or accelerate code with Parallel Computing Toolbox™ .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Introduced in R2012a

You have a modified version of this example. Do you want to open this example with your edits?

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.

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

Select web site

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

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

Sours: https://www.mathworks.com/help/matlab/ref/integral.html

## Calculate integral in Matlab [closed]

A simple approach using and using plain vanilla settings.
Would only use more sophisticated numerical techniques if required for application. You can examine the and options in the documentation for .

Numerical Integration: (w/ linear interpolation)

ans = 91.9954

Alternate Methods of Interpolation:
Your results will depend on your method of interpolation (see code & graphic below).

Code for graphic:

A poor approximation: (to get rough order of magnitude estimate)

And you can see we're in the ballpark (makes me feel more comfortable at least).

Other viable MATLAB Functions: |

Functions that would require more work: |
Notice that and both require unit spacing; to use these would require first interpolating with unit spacing.

Related Posts: (found after answer already completed)
Matlab numerical integration
How to numerically integrate vector data in Matlab?

Sours: https://stackoverflow.com/questions/55246914/calculate-integral-in-matlab

47 48 49 50 51