Using Maple to Compute Definite and Indefinite Integrals

Wednesday November 18, 1998

Our current topic in class is the integral and the area under a curve. Using the formulas for summing up the first n integers, the first n squares, the first n cubes, etc. we saw that we can compute the area bounded by any polynomial curve. For example, in class we used inscribed and circumscribed rectangles to compute the area bounded by y = 2x - x^2 on the interval [0,1]. Later we will learn that areas can be computed using antiderivatives. To find the area under y = 2x - x^2, we find an antiderivative for this function --- x^2 - x^3/3 in this case --- and then we evaluate the antiderivative at the endpoints:
Area under y = 2x -x^2 on    =  Antiderivative of 2x - x^2 evaluated at the
       [0,1]                    right endpoint (x = 1) - Antiderivative evaluated
                                at the left endpoint (x=0)

                             = (1^2 - 1^3/3) - (0^2 - 0^3/3) = 1 - 1/3 = 2/3.
The exercises below show how to compute area using Maple.

Exercises and Examples

Find the area under the curve y = 1/x^2 for x from 1 to 3 using inscribed and circumscribed polygons by working through the Maple commands given below:

  1. First graph the function and estimate the area of this region (by eye).
         [> f := x -> 1/x^2;           # define f(x)
         [> plot( f(x), x = 1..3, scaling = constrained);

    We can use Maple to draw some of these polygonal approximations to the area under the curve:

         [> with(student);                      # Load the student package
         [> leftbox(f(x), x = 1..3, 4);         # Use left endpoints and
                                                  4 subintervals
         [> rightbox(f(x), x = 1..3, 6);        # Use right endpoints and
                                                  6 subintervals
    Notice that in this example, leftbox gave us the circumscribed rectangles and rightbox gave us the inscribed ones. (Will this always be true?) Maple will also compute these sums for us:
         [> leftsum(f(x), x = 1..3, 4); value("); evalf(");
         [> rightsum(f(x), x = 1..3, 6); value("); evalf(");
  2. Find an upper and lower bound for the area of this region using inscribed and circumscribed rectangles with n = 10. Repeat using 100 subrectangles. Repeat using 1000 rectangles.
    If you have loaded the student package, you just need commands like:
         [> evalf(leftsum(f(x), x = 1..3, 10));
  3. Maple can even compute the limit of the sum of the inscribed rectangles. Try this:
         [> leftsum( f(x), x = 1..3, n );
         [> limit( " , n = infinity );
  4. Maple can also compute definite integrals using the fundamental theorem of calculus.
         [> Int( 1/x^2, x = 1..3 ) =  int( f(x), x = 1..3 );
    When we use Int with a capital I Maple simply displays the integral. When we type int, Maple actually evaluates the integral if it can.
  5. As in the examples above, we compute the area under the curve y = (sin(x))^2 for x from 0 to Pi.

    First define our new function and graph it on the interval from x = 0 to x = Pi:

              [> f := x -> (sin(x))^2;
              [> plot( f(x), x = 0..Pi, scaling = constrained);
    Estimate the area by eye. (Note that we use the option scaling = constrained to ensure that Maple uses the same scale on the horizontal and vertical axes.)
    Use leftbox and rightbox to look at the approximating rectangles.
              [> leftbox(f(x), x = 0..Pi, 10); 
              [> rightbox(f(x), x = 0..Pi, 10); 

    Since this function changes from increasing to decreasing at x = Pi/2, we need to combine leftsum and rightsum to compute the inscribed rectangles. For n = 10 and inscribed rectangles we need
              [> evalf( leftsum(f(x), x = 0..Pi/2, 5) ) +
                    evalf( rightsum(f(x), x = Pi/2..Pi, 5);

    Compute the limit as n goes to infinity of the inscribed rectangles.
               [> limit( leftsum( f(x), x = 0..Pi/2, n), n = infinity )
                  + limit( rightsum( f(x), x = Pi/2..Pi, n), n = infinity );

    Compare to the result of
              [> Int(sin(x)*sin(x), x = 0..Pi) = int( f(x), x = 0..Pi );
  6. Modify the commands of the previous exercise to compute the area using circumscribed rectangles.
  7. Using the examples above as a guide, compute the area under the curve y = for sin(x)*exp(x) for x from 0 to Pi.

    Define our new function and graph it:

              [> f := x -> sin(x)*exp(x);
              [> plot(f(x), x = 0..Pi);

    This time use middlebox and middlesum to estimate the area:
              [> middlebox( f(x), x = 0..Pi, 10 );
              [> middlesum( f(x), x = 0..Pi, 10 ); evalf(");
    Estimate the area using middlesum with n = 10, n = 100, and n = 1000.
    Next, compute the limit as n goes to infinity:
              [> limit( middlesum(f(x), x = 0..Pi, n), n = infinity);

    Finally, compute the area using
               [> Int( sin(x)*exp(x), x = 0..Pi ) = int( f(x), x = 0..Pi );
  8. Let f(x) = sin(x)*exp(x) and consider the area under this curve for x from 0 to Pi as in the previous problem.
    Use leftbox and rightbox to look at approximating rectangles.
              [> leftbox( f(x), x = 0..Pi, 10 );
              [> rightbox( f(x), x = 0..Pi, 10);
    Notice that the rectangles change from inscribed to circumscribed (or vice versa) at the peak.
    Replot the graph of f and click on the peak to estimate its coordinates. Define a variable xmax that corresponds to the x-value where the peak occurs.
              [> plot( f(x), x = 0..Pi );
              [> xmax := ---Fill in correct value from your plot---;     

    Now you can draw inscribed rectangles using
              [> with(plots):
              [> p1 := leftbox( f(x), x = 0..xmax, 10 ):  #Use colon, not semicolon
              [> p2 := rightbox( f(x), x = xmax..Pi, 4):  #Use colon.
              [> display({p1, p2});                       #Use semicolon.

    Use leftsum and rightsum to compute the area using inscribed rectangles and at least 100 subrectangles.
    Estimate the area using (100) circumscribed rectangles.