This example shows how to compute definite integrals using Symbolic Math Toolbox™.

Show that the definite integral for on is 0.

```
syms x
int(sin(x),pi/2,3*pi/2)
```

ans =

To maximize for , first, define the symbolic variables and assume that :

syms a x assume(a >= 0);

Then, define the function to maximize:

F = int(sin(a*x)*sin(x/a),x,-a,a)

F =

Note the special case here for
. To make computations easier, use `assumeAlso`

to ignore this possibility (and later check that
is not the maximum):

assumeAlso(a ~= 1); F = int(sin(a*x)*sin(x/a),x,-a,a)

F =

Create a plot of to check its shape:

fplot(F,[0 10])

Use `diff`

to find the derivative of
with respect to
:

Fa = diff(F,a)

Fa =

The zeros of are the local extrema of :

hold on fplot(Fa,[0 10]) grid on

The maximum is between 1 and 2. Use `vpasolve`

to find an approximation of the zero of
in this interval:

a_max = vpasolve(Fa,a,[1,2])

a_max =

Use `subs`

to get the maximal value of the integral:

F_max = subs(F,a,a_max)

F_max =

The result still contains exact numbers
and
. Use `vpa`

to replace these by numerical approximations:

vpa(F_max)

ans =

Check that the excluded case does not result in a larger value:

vpa(int(sin(x)*sin(x),x,-1,1))

ans =

Numerical integration over higher dimensional areas has special functions:

integral2(@(x,y) x.^2-y.^2,0,1,0,1)

ans = 4.0127e-19

There are no such special functions for higher-dimensional symbolic integration. Use nested one-dimensional integrals instead:

syms x y int(int(x^2-y^2,y,0,1),x,0,1)

ans =

Define a vector field `F`

in 3D space:

syms x y z F(x,y,z) = [x^2*y*z, x*y, 2*y*z];

Next, define a curve:

```
syms t
ux(t) = sin(t);
uy(t) = t^2-t;
uz(t) = t;
```

The line integral of `F`

along the curve `u`

is defined as
, where the
on the right-hand-side denotes a scalar product.

Use this definition to compute the line integral for from

F_int = int(F(ux,uy,uz)*diff([ux;uy;uz],t),t,0,1)

F_int =

Get a numerical approximation of this exact result:

vpa(F_int)

ans =

Was this topic helpful?