MATLAB can find both an indefinite integral (i.e., antiderivative)
and a definite integral of a symbolic expression. That assumes an elementary
antiderivative exists. If not, MATLAB can compute a very accurate
numerical approximation to the definite integral.
- First we calculate
indefinite integrals. Before doing anything, we must declare
x to be a symbolic variable. Enter:
syms x
Then enter
int(x*sin(x), x)
Check that the result is an antiderivative of x sin(x) by
entering:
diff(ans, x)
- Introduce another symbolic variable a to investigate how
MATLAB deals with more than one symbolic variable in integration.
Enter:
syms a
Then enter
int(a*sin(x), x)
Now enter
int(a*sin(x), a)
What is the role of the variable following the comma?
- Now try to find an
antiderivative for sin ( x^{5} + x^{3} ).
MATLAB does not know an antiderivative of this
function that can be defined in terms of functions known to MATLAB. On
the other hand, enter:
int(sin(x^2), x)
The Fresnel S function is known to MATLAB, but
probably not to you. However, you can check by differentiation
that the expression is an antiderivative.
- Next we calculate
definite integrals. To integrate x sin(x) over the interval
[0,pi/2], enter:
int(x*sin(x), x, 0, pi/2)
- Now try this
method on the integral of sin ( x^{5} + x^{3} )
over the interval [0,pi/2].
MATLAB still doesn't know an antiderivative for this function.
To obtain a numerical estimate, enter:
double( int(sin(x^5 + x^3), x, 0, pi/2) )
- If you know that
all you want is a numerical estimate, you can use MATLAB's
numerical integrator called quad8. No symbolics are involved.
Enter:
quad8( inline('sin(x^5+x^3)'), 0, pi/2 )
Numerical integration also works with user-defined functions in m-files.
Symbolic integration doesn't. If you still have the m-file called fun.m
that you created in Part 6 of this Tutorial, enter:
quad8('fun', 0, pi/2 )
The significance of using the quad8 function is that
MATLAB does not try to find a symbolic solution
before starting on the numerical estimate.
- Use MATLAB
to find the exact value of each of the following integrals.
(Type inf for the infinity symbol.)
- The integral of
1/(1+x^{2}) from 0 to 1,
- The integral of
1/(1+x^{2}) from 0 to
Infinity,
- The integral of
1/(1+x^{4}) from 0 to
Infinity.