C Course Homework


Homework 4 - Integration


Things You Will Need to Know/Learn:


Program Description

For this homework assignment, you will implement two different numerical integration techniques (Simpson and Monte Carlo) and compare them. For each technique you will need to write at least one function.

Simpson Rule

The first technique will use the Simpson Rule to evaluate an integral. The Simpson rule uses rectangles to approximate the area under a curve. Use the following prototype for this function.


     double integrate_rect(double a, double b, double F(double), int n);
      /* The interval (a,b) is partitioned into n parts.  The function    */
      /*  F(x) is computed on the midpoint of each partition to calculate */
      /*  the area of each rectangle.  The areas of all the rectangles    */
      /*  are then added to approximate the entire area under the curve.  */

Monte Carlo Method

The second technique will use a random number generator to do a Monte Carlo estimation of the area under a curve. The theory of this method will be explained in class. Use the following prototype for this function.


     double integrate_mc(double a, double b, double max, double F(double), int n);
      /* The interval of integration is (a,b) and max is a float larger   */
      /*  than any function value in this interval.  The dimensions       */
      /*  (b - a) * max define a rectangle within which F(x) is drawn.    */
      /*  n points are sampled at random using a random number generator  */
      /*  to produce (x,y).  If y <= F(x), then we count this as a     */
      /*  "yes", but if y > F(x) then we count this as a "no".   	  */
      /* The number of "yesses"/n * (b - a) * max is the area estimate.   */

Testing Both Methods

In addition to implementing the two routines above, you will have to write the function F(x) to test both methods. This function will be passed as an argument to the two routines you write to implement each method. An easy function to use for initial testing is F(x) = x in the interval (0,1). Try not to exceed n = 1000, as this will take a long time to run. Here's the function definition for F(x) = x:


     double F(double x)
     {
        return x;
     }
You should also test your implementations with other functions you know how to integrate--feel free to use functions from "math.h" to implement F(x). We will will test your implementations of the two integration techniques with several different functions.

Main

Your main routine should set the interval you wish to check and the maximum value which is larger than F(x) in the interval (for the Monte Carlo method). You can hard-code (set) the interval and the maximum to values--you don't need to do any input from the user. You should then run your functions for five different values of n (4, 16, 64, 256, 1024). A for loop works nicely here. You should print the results from each iteration as well as a difference between the two methods.

Take a look at the sample output .

Grading

Style -- 2 points total

Correctness -- 6 points total

Deductions

Deduct 3 points if the program does not compile.

(use "gcc <filename.c>" or "gcc <filename.c> -lm")


Hints In hw3, we scaled the value returned by rand() to an integer in the range 0-25 using the '%' operator. In this homework, you will have to scale the value returned by rand() to a double within your interval (for the x-coordinate) or between 0 and max (for the y-coordinate).
Here's a hint on scaling the random number returned by rand(). First, you need to know that (for "gcc") rand() returns an integer in the range 0 to LONG_MAX where LONG_MAX is defined in "limits.h". (You will have to #include <limits.h> to use LONG_MAX.) The first step is to scale the random number to a floating point number in the range 0.0 to 1.0. This can be achieved using:

              rand()/((double)(LONG_MAX))
         

Before dividing rand() by LONG_MAX we have to cast LONG_MAX to a double so that the division is a floating point division and not an integer division. (Recall that an integer division will truncate any fractional part, so in this case if we did not do the cast we would get either 0 or 1, but nothing in between. Casting is mentioned on p. 240 of the book.)
Now that you have a value between 0.0 and 1.0, you can manipulate this value with a bit of math to pick a point in the desired interval.


FAQ

   
Star Solutions for Integration