Integration Previous Contents Next

10   Integration

10.1   Riemann Sum

MATLAB has a command rsums that allows you to graphically explore Riemann sums. Here is an example.


Example: Find ò01 e-3x dx.
>> rsums exp(-3*x)
MATLAB has given you a window (you may have to click on windows and then on figure no. 1) which shows the Riemann sums for exp(-3*x) on the interval [0,1] when n=10. To increase n, click on the right arrow at the bottom. The value of the Riemann sum is shown above the graph. You can watch this value change as you increase n.

Compare this answer to
>> numeric(int('exp(-3*x)',0,1))
Notice: With the command rsums you can only use the interval [0,1] and you can only use the maximum n allowed by this graph.

10.2   Simpson's Rule and other numerical methods

10.3   Symbolic integration

MATLAB has a built in ability to compute symbolic integrals. You will need to be familiar with the symbolic math operations to fully appreciate this.

Note: MATLAB has improved and different symbolic math capabilities in version 5.0 and greater. These notes refer to earlier versions.

  • Finding Antiderivatives
    Example: Use MATLAB to compute ò e-3xdx. Answer: Of course you know how to find this easy antiderivative by hand. But we'll use it to illustrate how to find integrals using MATLAB.
    >>int('exp(-3*x)')
    ans=
       -1/3 * exp (-3*x)
    
    We know the answer should be -(1/3)e-3x +C, with a ``constant of integration" C. MATLAB omits this constant, but that doesn't mean you should!

  • finding a second derivative
    Example: consider
    >>int
    ans=
       1/9*exp (-3*x)
    
    MATLAB has found the antiderivative of the last answer namely the antiderivative of -1/3 * exp (-3*x).

    one can use the diff command to recover the integrand:
    >> diff
    
  • Finding definite integrals

    MATLAB can compute definite integrals as well.
    Example:
    >>int ('exp (-3*x)', 0, 1)
    ans = -1/3 * exp (-3)+1/3
    
    This computed the following ò01 e-3x dx.

  • numeric answers What if you wanted a number? You can use the numeric command:
    >> numeric                      % for the last integral done above
    ans = .3167
    
  • Simplifying messy integration Often the answer MATLAB gives is quite hard to read. It has a built in simplify command to help in these cases:
    >> int('sin(x)^7 * cos(x)^5')
    ans = TOO UGLY TO PRINT
    
    To see how MATLAB simplifies this answer, use the simplify command and then the pretty command:
    >> simplify                     % simplifies the last integral
    >> pretty                       % makes it _pretty_
    
    Do this example with MATLAB. Is it ``prettier''?

  • When there is no antiderivative Sometimes there is just no antiderivative for a given function. So the Fundamental Theorem of Calculus will offer no help in finding an answer to a definite integral. You need to approximate the answer if you want to move on. Again we use the MATLAB command numeric . As well, you could approximate the integral with a Riemann sum , or use Simpson's Method .
    >> int('\cos(x^3)', 0,1)        % if MATLAB chokes it prints out its input
    ans = int('\cos(x^3)', 0,1)
    >> numeric                      % find numeric answer to last query
    ans = 0.9317
    

10.4   Multiple integration


Previous Contents Next