Main Content

Integration

Iff是一个象征性的表达呢

int(f)

attempts to find another symbolic expression,F, so thatdiff(F)=f. That is,int(f)returns the indefinite integral or antiderivative off(provided one exists in closed form). Similar to differentiation,

int(f,v)

uses the symbolic objectvas the variable of integration, rather than the variable determined bysymvar. See howintworks by looking at this table.

Mathematical Operation

MATLAB®Command

x n d x = { log ( x ) if n = 1 x n + 1 n + 1 otherwise .

int(x^n)orint(x^n,x)

0 π / 2 sin ( 2 x ) d x = 1

int(sin(2*x), 0, pi/2)orint(sin(2*x), x, 0, pi/2)

g= cos(at+b)

g ( t ) d t = sin ( a t + b ) / a

g = cos(a*t + b) int(g)orint(g, t)

J 1 ( z ) d z = J 0 ( z )

int(besselj(1, z))orint(besselj(1, z), z)

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

  • The antiderivative,F, 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

symsabthetaxynuz

The following table illustrates integration of expressions containing those variables.

f

int(f)

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

In the last example,exp(-x^2), 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 functionerf.

If MATLAB is unable to find an answer to the integral of a functionf, it just returnsint(f).

Definite integration is also possible.

Definite Integral

Command

a b f ( x ) d x

int(f, a, b)

a b f ( v ) d v

int(f, v, a, b)

Here are some additional examples.

f

a、b

int(f, a, b)

syms x f = x^7;
a = 0; b = 1;
int(f, a, b)
ans = 1/8
syms x f = 1/x;
a = 1; b = 2;
int(f, a, b)
ans = log(2)
syms x f = log(x)*sqrt(x);
a = 0; b = 1;
int(f, a, b)
ans = -4/9
syms x f = exp(-x^2);
a = 0; b = inf;
int(f, a, b)
ans = pi^(1/2)/2
syms z f = besselj(1,z)^2;
a = 0; b = 1;
int(f, a, b)
ans = hypergeom([3/2, 3/2],... [2, 5/2, 3], -1)/12

For the Bessel function (besselj) example, it is possible to compute a numerical approximation to the value of the integral, using thedoublefunction. 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

a = double(a)

returns

a = 0.0717

Integration with Real Parameters

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

e a x 2

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

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

Figure contains an axes object. The axes object contains an object of type functionline.

However, if you try to calculate the integral

e a x 2 d x

without assigning a value toa, MATLAB assumes thatarepresents a complex number, and therefore returns a piecewise answer that depends on the argument ofa. If you are only interested in the case whenais a positive real number, useassumeto set an assumption ona:

syms a assume(a > 0)

Now you can calculate the preceding integral using the commands

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

This returns

ans = pi^(1/2)/a^(1/2)

Integration with Complex Parameters

To calculate the integral

1 a 2 + x 2 d x

for complex values ofa, enter

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

Usesymsto clear the all assumptions on variables. For more information about symbolic variables and assumptions on them, seeDelete Symbolic Objects and Their Assumptions.

The preceding commands produce the complex output

F = (pi*signIm(1i/a))/a

The functionsignImis defined as:

signIm ( z ) = { 1 if Im ( z ) > 0 , or Im ( z ) = 0 and z < 0 0 if z = 0 -1 otherwise .

To evaluateFata = 1 + i, enter

g = subs(F, 1 + i)
g = pi*(1/2 - 1i/2)
double(g)
ans = 1.5708 - 1.5708i

High-Precision Numerical Integration Using Variable-Precision Arithmetic

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

Integratebesseli(5,25*u).*exp(-u*25)by using bothintegralandvpaintegral. Theintegralfunction returnsNaNand issues a warning whilevpaintegralreturns 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

For more information, seevpaintegral.

See Also

||

External Websites