Approximations of Definite Integrals by xeg10270


									                   Approximations of Definite Integrals
                              D. P. Morstad, University of North Dakota

                                          Objectives of Assignment

        1.      To demonstrate how to write and use function type subroutines in
        2.      To develop subroutines which use Simpson’s Rule and the
                Trapezoidal Rule to approximate definite integrals.
        3.      To write subroutines which find the minimum value of n for a
        given error size, f(x), and interval.

I.      Subroutines and X(PLORE)

         One of the most valuable features of X(PLORE) is that it allows you to write and use
subroutines. The tedium of performing mindless repetitive tasks can be avoided by making
X(PLORE) do them for you with subroutines. A subroutine is just a list of computer commands
that are given a name, and then are used over and over again.

        For example, in order to approximate a definite integral using Simpson’s Rule, you must
find ∆x, multiply some values by 4, multiply others by 2, add–up a bunch of numbers, and then
multiply the whole thing by ∆x . X(PLORE) allows you to write down the instructions for this
just once, and then any time you type in a special code word, X(PLORE) will automatically
perform this list of instructions.

     Area ≈   ∆x
               3   [ f ( x0 ) + 4 f ( x1 ) + 2 f ( x 2 ) + 4 f ( x 3 ) + L + 2 f ( x n −2 ) + 4 f ( x n −1 ) + f ( x n )]

                          Figure 1. Simpson’s Rule can be tedious if n ≥ 4.

       The list of instructions you want X(PLORE) to remember is called a subroutine. The
code word you use to make X(PLORE) execute that list of instructions is just the name of the

subroutine. Pressing ) will move you back and forth between the subroutine screen and the
input screen.

         X(PLORE) recognizes two different types of subroutines. A procedure subroutine runs
through a list of instructions and does whatever they demand (like graphing several functions all
at one time). A function subroutine runs through a bunch of instructions and then puts an
answer on the screen.

II. An X(PLORE) Subroutine for Simpson’s Rule

        It is impossible to write a subroutine unless you know exactly what you want the
subroutine to do, and you know in what order you want it to do it. You can’t just type in,
“Figure out the area using Simpson’s Rule.”

      Examining the formula for Simpson’s Rule reveals several tasks which need to be
accomplished: ∆x must be found, a sum must be multiplied by ∆x , f ( x0 ) and f ( x n ) are
added to the sum, the f ( x odd ) ’s must each be multiplied by 4 and added to the sum, and the
rest of the f ( xeven )’s must be multiplied by 2 and added to the sum.

        It appears that there are only five main tasks to be performed. Now let’s put them in
the order you would probably use if you were going to evaluate Simpson’s Rule by hand:

⇒ Step 1. Find the value of ∆x.
⇒ Step 2. Add f ( x0 ) and f ( x n ) to the sum.
⇒ Step 3. Multiply each f ( x odd ) by 4 and add it to the sum.
⇒ Step 4. Multiply each of the remaining f ( xeven )’s by 2 and add them to the sum.
⇒ Step 5. Multiply the entire sum by      3

         Of course, you will also have to tell the computer that the sum should be zero before
anything is added to it (this is called initializing a variable), and you will have to tell the
computer to inform you of what the final answer is. Computers will only do what you tell them
to, and it has to be in their language.

         The only tricky part of this subroutine will be figuring out a mathematical way of making
X(PLORE) realize what should be multiplied by 2 and what should be multiplied by 4. Starting
with x 1 and then adding 2∆x over and over again will generate all the terms which must be
multiplied by 4 – notice that there will be n of them (don’t let the x 0 term confuse you on how
many odd terms there are).

        Similarly, if you start with x 2, then adding 2∆x over and over again will generate all the
terms that need to be multiplied by 2. There is one fewer of these terms than there were of the
others, so there are n –1 of them.

          The complete subroutine is given below with explanations. The large curly brackets and
the italicized text are only there for explanation. They should NOT be typed into the computer.
Press ) to switch to X(PLORE)’s subroutine screen. Press ) again when you want to leave
the subroutine screen.

         The name of the function subroutine is simprule. The “%f” tells X(PLORE) that the
function which is the integrand is f(x). The “n” is how we will tell it the number of subintervals
we want (remember n must be even for Simpson’s Rule). Instead of using “a” and “b” for the
left and right endpoints of the interval of integration, I will use “xl” for the x–left endpoint and
“xr” for the x–right endpoint.

       function simprule(%f, n, xl, xr)  Naming the procedure, the function,
                                         and the variables.
       dx = (xr - xl) / n                    
       area = 0                               STEP 1 and STEP 2: finding ∆x,
                                              initializing area,
                                              adding f(x ) and f(x ) to area.
       area = area + f(xl) + f(xr)                         0      n

       x = xl + dx                           STEP 3: setting x=x 1 , then
       for I = 1 to n / 2                   
                                             repeatedly adding 4f(x) to area for
          area = area + 4 * f(x)            
          x = x + 2 * dx                     each x=x odd . This is done n/2 times.
       end                                  
       x = xl + 2dx                         
       for I = 1 to (n / 2 − 1)                  STEP 4: setting x=x 2 , then
          area = area + 2 * f(x)                repeatedly adding 2f(x) to area for
          x = x + 2 * dx                        each x=x even except x 0 and x n . This
       end                                  
                                                is done (n/2)-1 times.
       area = (dx / 3) * area               
                                             STEP 5: multiplying by ∆x/3 and
       return(area)                         
                                              then reporting the final result.
       end                                   }
                                                 Ending the subroutine.

                Figure 2. An X(PLORE) subroutine for Simpson’s Rule.

        After you have figured out what each line of the subroutine does and typed it in on the
subroutine screen, go back to the input screen to see if it works. On the input screen declare a
function (this means type in and enter the function) like f(x) = x 2. Then evaluate its integral from
x = 0 to x = 9 using simprule. Let n be any positive even number you want. You know the

answer should be 243. Type in and enter “simprule(f(x), [put any even number here], 0, 9)”.
X(PLORE) should say “Answer =243”. If not, go back to the subroutine area and recheck
your typing.

III. A Subroutine to Help do the REAL Dirty Work

         Using Simpson’s Rule by hand can be very tedious, but trying to find the minimum value
of n to guarantee the error is small is an even bigger pain. So, let’s use X(PLORE) to help
lessen the pain.

         Remember that for Simpson’s Rule,

                                               M ( b − a )5
                                          ε≤                  ,
                                                180 ⋅ n 4

where ε is the error, n is the number of subintervals, b and a are xr and xl respectively, 180 is
two times ninety, and M is the maximum of the absolute value of the fourth derivative of f(x) on
[xl, xr].

       You usually know how small the error ε must be, and your job is to determine how
many subintervals will be necessary. That means you need to solve for n and make sure that

                                M (b − a ) 5  4
                             n≥               , and n must be even.
                                180 ⋅ ε 
                                             

Notice the tricky stuff that is going on with the inequalities in these two expressions. The biggest
pain here, as you may well know, is finding M. Finding the fifth power and the fourth root are
minor pains.

        X(PLORE) can easily perform the computations, but it can also help you find M. The
command “dif(f(x), x, 4)” tells X(PLORE) to find the fourth derivative of f(x), and then we can
make X(PLORE) graph the fourth derivative on the interval [a, b]. You can just look at the
graph and eyeball a pretty good guess for M. (Be careful to never underestimate M, always
overestimate it a little bit if you can’t be exact.)

         Might as well write a subroutine to do all this to help find n. First, list the necessary

⇒ Step 1. Find the fourth derivative of f(x).

⇒ Step 2. Graph f ( 4) ( x ) .

⇒ Step 3. Ask for the maximum of the absolute value of the graph.

⇒ Step 4. Compute n.
⇒ Step 5.     Report the value of n.

       Since X(PLORE) will be graphing while running the subroutine, you have to tell it what
window to use right in the subroutine command. That is, use something like
“simperr(%f, error, xl, xr, yb, yt)” where yb denotes y–bottom of the window and yt denotes
y–top. Below is a complete subroutine which helps you find M for Simpson’s Rule.

      function simperr(%f, error, xl,xr, yb, yt)            Naming the subroutine and
      graphics                                              everything it will need.
                                                            STEP 1: finding the fourth
      g(x) = dif(f(x), x, 4)                    
                                                             derivative and calling it g(x).
      window(xl, xr, yb, yt)                   
      graph(g(x), x)                                        STEP 2: graphing g(x).

      Input('What is the maximum of the              STEP 3: asking for what you
             absolute value of the graphed           found by visually inspecting the
             function?' , m)                        
      n = (m *(xr - xl)^5 / (180 * error))^ (1 / 4) 
      return(n)                                     
                                                      STEP 4 and STEP 5: computing
      text                                           and reporting the result.
      end                                           
                                                             Ending the subroutine.

           Figure 3. An X(PLORE) subroutine to find M and n for Simpson’s Rule.

       Try this subroutine to find M and n for f(x) = sin(x) on [0, π] with a maximum error of
0.000 000 1. Since X(PLORE) often only uses six significant digits, an error of less than
0.0000001 will usually be as accurate as it can get. Next use your value of n and simprule to

approximate sin( x) dx . Remember that n must be even, so round up to an even number. A

boring example, but you know the answer should be 2. Your ‘eyeballed’ value for M should
have been 1 and n should have been 66 (not 64).

        Next try
                             sin x 2 dx . You might have to be patient with simperr, but it shouldn’t

take more than four minutes. The window is critical here. Obviously, xl and xr should be 0
and π respectively, but yb and yt may take several tries before you can ‘eyeball’ M. The
smallest value M can be is 170. This gives n = 114 and a value of 0.894831 for the integral.
Notice that even if you found a larger M and n, the value of the integral was the same. It just
took X(PLORE) a few milliseconds longer to evaluate simprule.


                     ∫ (1 − x ) dx . Again, this might be slow, and the necessary
                      0 .75        3
       Finally try

y-coordinates for the window will surprise you. The smallest value M can be is 120. This gives
n = 36 and a value of 0.64968 for the integral.


To top