Page 81 - A Guide to MATLAB for Beginners and Experienced Users
P. 81
62 Chapter 4: Beyond the Basics
Integration
MATLAB can compute definite and indefinite integrals. Here is an indefinite
integral:
>> int (’xˆ2’, ’x’)
ans =
1/3*x^3
As with diff, you can declare x to be symbolic and dispense with the char-
acter string quotes. Note that MATLAB does not include a constant of inte-
gration; the output is a single antiderivative of the integrand. Now here is a
definite integral:
>> syms x; int(asin(x), 0, 1)
ans =
1/2*pi-1
You are undoubtedly aware that not every function that appears in calcu-
lus can be symbolically integrated, and so numerical integration is sometimes
necessary. MATLAB has three commands for numerical integration of a func-
tion f (x): quad, quad8, and quadl (the latter is new in MATLAB 6). We
recommend quadl, with quad8 as a second choice. Here’s an example:
>> syms x; int(exp(-xˆ4), 0, 1)
Warning: Explicit integral could not be found.
> In /data/matlabr12/toolbox/symbolic/@sym/int.m at line 58
ans =
int(exp(-x^4),x=0..1)
>> quadl(vectorize(exp(-xˆ4)), 0, 1)
ans =
0.8448
➱ The commands quad, quad8, and quadl will not accept Inf or -Inf as
a limit of integration (though int will). The best way to handle a
numerical improper integral over an infinite interval is to evaluate
it over a very large interval.