# Thread: area estimation of graph

1. ## area estimation of graph

hihi,everyone.
I use the following program to estimate the area of
y=sqr(cos(x)p2+1)

sqr= square root,
p2 = power 2,

from x=0 to x=2.
I use a rectangle to enclose the graph with height 1.5 and width 2.0.
y<= 2 power 0.5 = 1.414 as cos(x)<=1

so i use this estimation formula:
red_area=rect*(red/total)

if y<1.414, then the pts lie in the graph.
if y>1.414, then the pts lie outside the graph.
for x between 0 and 2.0 with increment 0.00001.

and my program is the following.

# include <stdio.h>
# include <math.h>

int main(void)
{
float y,rect;
float x=0;
int total,red=0;
y=pow(pow(cos(x),2)+1, 0.5);
rect=1.5*2.0;
for (x=0; x<2; x=x+0.00001)
{
total++;
if (y<1.414)
{
red++;
}
}
printf("red area = %f \n", rect*red/total);
return 0;
}

2. estimating the area of graph by many small rectangles will be a better way.

3. You can use the following code to do a numerical estimate of the definate integral (which is really what you want):

Code:
```double y(double x);
double integrate(double lowerBound, double upperBound, int numRects);

double y(double x){
return sqrt(pow(cos(x),2) + 1.0);
}

double integrate(double lowerBound, double upperBound, int numRects){
//computes area by left hand rectangle approximation.
double rectWidth;
double area;
int i;
area = 0;
rectWidth = (upperBound - lowerBound)/(double)numrects;
for (i = 0; i < numRects; i++){
area += y(lowerBound + rectWidth * (double) i) *rectWidth;
}
return area;
}```
As numrects increases, this will better and better approximate the integral. In fact, for any integrable funtion, by definition this converges to the integral as numRects goes to infinity.

4. ## thanks

Ok,it's fine.

thank you very much.^^