Case Study: Fig. 9.18 Area Under A Curve
Case Study: Fig. 9.18 Area Under A Curve
One of the applications of computers in numerical analysis is computing the area under a curve.
One simple method of calculating the area under a curve is to divide the area into a number of
trapezoids of same width and summing up the area of individual trapezoids. The area of a
trapezoid is given by
Where h1 and h2 are the heights of two sides and b is the width as shown in Fib 9.18.
curve
f(x) b
h1 h2
A x B
The program in Fig 9.20 calculates the area for a curve of the function
f(x) = x2 + 1
Input
Output
Algorithm
1. Input the lower and upper limits and the number of trapezoids.
2. Calculate the width of trapezoids.
3. Initialize the total area.
4. Calculate the area of trapezoid and add to the total area.
5. Repeat step-4 until all the trapezoids are completed.
6. Print total area.
main
input find_area
function_x trap_area
The evaluation of f(x) has been done using a separate function so that it can be easily modified to
allow other functions to be evaluated.
The output for two runs shows that better accuracy is achieved with larger number of trapezoids.
The actual area for the limits 0 and 3 is 12 units (by analytical method)
AREA UNDER A CURVE
Program
#include <stdio.h>
float start_point, /* GLOBAL VARIABLES */
end_point,
total_area;
int numtraps;
main( )
{
void input(void);
float find_area(float a,float b,int n); /* prototype */
return(x*x + 1);
}
Output