/* --------------------------------------------------------------------- // Figure 5.14: Integration by summing rectangles. // --------------------------------------------------------------------- */ #include "tools.h" #define N 100 /* This is the function that we will integrate. ------------------------ */ double f( double x ){ return x*x + x; } void main( void ) { double a = 0.0; /* Lower limit of integration. */ double b = 1.0; /* Upper limit of integration. */ double h = (b - a) / N; /* Width of one of the N rectangles summed.*/ double x; /* Current function argument; a <= x < b. */ double sum; /* Total area of rectangles. */ int k; /* Loop counter. */ banner(); printf( " Integrate x*x + x from %g to %g.\n", a, b ); sum = 0.0; k = 0; while (k < N) { /* Add up N rectangles. */ x = a + k * h; /* Lower left corner of kth rectangle. */ sum += h * f( x ); /* Area of rectangle at x. */ ++k; } printf( " The area is %g\n", sum ); bye(); }