area estimation of graph

This is a discussion on area estimation of graph within the C Programming forums, part of the General Programming Boards category; hihi,everyone. As I'm just a beginner of C program, please help ^^. I use the following program to estimate the ...

  1. #1
    hei
    hei is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    Smile area estimation of graph

    hihi,everyone.
    As I'm just a beginner of C program, please help ^^.
    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;
    }


    please help me, thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    estimating the area of graph by many small rectangles will be a better way.
    Attached Images Attached Images  

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    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. #4
    hei
    hei is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    thanks

    Ok,it's fine.

    thank you very much.^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  2. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  3. determining a path through the graph
    By Mist in forum C Programming
    Replies: 2
    Last Post: 02-27-2005, 11:21 AM
  4. Need help with switch
    By 3kgt in forum C Programming
    Replies: 2
    Last Post: 02-26-2003, 11:43 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21