Thread: The trapezoidal rule question

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    The trapezoidal rule question

    Hi,
    I can't figure out where is the problem.Please help me to correct my code
    Code:
    #include <stdio.h>
    #include <math.h>
    void trap(double a,double b, int n, double *areag, double *areah);
    double g(double x);
    double h(double x);   
    int main(void)
    {
    	double  areag = 0.0, areah = 0.0;
    	double a1 = 0, b1 = 10;
    	int n;
    	for(n=2;n<=128;n*=2){
    		trap(a1, b1, n, &areag, &areah);
    		printf("%f %f\n", areag, areah);
    	}
    	return(0);
    }
    
    double g(double x){
    	return(pow(x,2)*sin(x));
    }
    double h(double x){
    	return(sqrt(4-pow(x,2)));
    }
    void trap(double a,double b, int n, double *areag, double *areah){
    	int i, l;
    	*areag = (b-a)/2*n * ( g(a) + g(b));
    	for(i = 1; i<=n-1;i++)
    		*areag += 2*g(i);
    	*areah = (b-a)/2*n * ( h(a) + h(b));	
    	for(l=1;l<=n-1;l++)
    		*areah += 2*h(i);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How about you tell us what it's doing wrong and what you expect it to do for you...
    Really... the more information you give us the better the help you will get.

    You need to read this --> http://www.cplusplus.com/forum/articles/1295/

    A couple of quick observations...
    g and h are really lousy names for functions... choose something that describes the function's behaviour, not then next letter in the alphabet.
    You don't actually need prototypes for your g and h functions at the top of the program since they are both "seen before called".
    Last edited by CommonTater; 04-23-2011 at 12:19 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Thanks for your advices.If you build and run the code, you will see it doesn't work properly.This question from a book(Problem solving and program design in C).It gives 2 equation and wants find approximating area under a curve.And adds call traps with values for n of 2, 4,8, 16, 32, 64, 128.
    Output of my code is negative and -nan.
    Equations are:
    g(x) = x^2sinx (a = 0, b = 3.14159)
    h(x) = sqrt(4-pow(x.2)) ( a =-2, b=2);

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    For future references, rarely someone will have the time to build the code and debug it for you. To obtain better help, you should post up the errors and output of the program.

    Code:
    for(l=1;l<=n-1;l++)
    		*areah += 2*h(i);
    why is it h(i) when it's 'l' in the for loop?

    the trouble is g(x) = sqrt(4-pow(x,2))
    Code:
    for(n=2;n<=128;n*=2){
    		trap(a1, b1, n, &areag, &areah);
    ///  as n increase, pow(n,2) is getting large
    // inside trap. 
    for(i = 1; i<=n-1;i++)
    		*areag += 2*g(i);
    when n is large enough 4-pow(x,2) is a negative number.
    Last edited by nimitzhunter; 04-23-2011 at 01:12 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    I see.I will do what you said.
    Solved the problem;
    The problem was in trap function :
    Code:
    void trap(double a,double b, int n, double *areag, double *areah){
            int i, l;
    	*areag = (b-a)/2*n * ( g(a) + g(b));
    	for(i = 1; i<=n-1;i++)
    		*areag += 2*g( (double)i / n);
    	*areah = (b-a)/2*n * ( h(a) + h(b));	
    	for(l=1;l<=n-1;l++)
    		*areah += 2*h( (double) l / n);
    }

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    OK you still have several errors there. Here are my suggestions and what I would do:

    1; Don't pass the areas just call the function and return the area.

    2; evaluate the stepping size first then pass it to evaluation function because it's easier if you want to do simples 1/3, 3/8 or bools rule if you decided to do them.

    3; turn (b-a)/2*n * ( g(a) + g(b)) to stepping size * (evaluate left interval + evaluateright interval) / 2.0L to get the first and last pieces evaluated.

    4; oh yeah I suggest you use long doubles because it gives a greater precision.

    5; before the for loop get the first stepping size for your g() or h().evaluation function. [left interval + stepping size]

    6; in the for loop you need to pass the next stepping into g() or h() evaluation function not left and right interval and add the return area to the total sum, and then increase the stepping size.

    7; return the sum.
    Last edited by tripleA; 04-23-2011 at 01:40 PM.

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    you misunderstood the interior points of traperzoid rule.

    The for loop should be:
    Code:
    for( something with i)
        *areag += (b-a)/n*g( a+ i*(b-a)/n)
    or define the step-size like tripleA suggested
    Code:
    double h = (b-a)/n;
    for( something in here)
        *area += h*g(a+i*h);
    Last edited by nimitzhunter; 04-23-2011 at 02:17 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    Quote Originally Posted by nimitzhunter View Post
    you misunderstood the interior points of traperzoid rule.

    The for loop should be:
    Code:
    for( something with i)
        *areag += (b-a)/n*g( a+ i*(b-a)/n)
    or define the step-size like tripleA suggested
    Code:
    double h = (b-a)/n;
    for( something in here)
        *area += h*g(a+i*h);
    To keep it efficient you don't want to do all those multiplication over and over again in the for loop. The h multiplication in my suggestion can be taken out and do it after the loop.

    And my previous point #2 is wrong, the multiplication of the stepping size should be taken out from the equation and applies to to the total area before returning it.

  9. #9
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Yeah, i'ts more efficient that way, but he already calculated the end point. I just want to give him something that work without altering his program( that is up to him.)
    "All that we see or seem
    Is but a dream within a dream." - Poe

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Thanks for your advices.And I guess proper code is
    Code:
    #include <stdio.h>
    #include <math.h>
    void trap(double a,double b, int n, double *areag, double *areah);
    double g(double x);
    double h(double x);   
    int main(void)
    {
    	double  areag = 0.0, areah = 0.0;
    	double a1 = 0, b1 = 3;
    	int n;
    	for(n=2;n<128;n*=2){
    		trap(a1, b1, n, &areag, &areah);
    		printf("%f %f\n", areag, areah);
    	}
    	return(0);
    }
    
    double g(double x){
    	return(pow(x,2)*sin(x));
    }
    double h(double x){
    	return(sqrt(fabs(4-pow(x,2))));
    }
    void trap(double a,double b, int n, double *areag, double *areah){
    	int i, l;
    	*areag = ((b-a)/2)*n * ( g(a) + g(b));
    	for(i = 1; i<=n-1;i++)
    		*areag += (b-a)/n*g( a+ i*(b-a)/n);
    	*areah = (b-a)/2*n * ( h(a) + h(b));	
    	for(l=1;l<=n-1;l++)
    		*areah += (b-a)/n*h(( a+ l*(b-a)/n));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simpson's rule and Trapezoidal rule from fixed array
    By timwonderer in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 03:14 PM
  2. composite trapezoidal rule
    By Sam Robinson in forum C Programming
    Replies: 0
    Last Post: 05-19-2003, 10:01 AM
  3. Arrays and the trapezoidal rule
    By ChemistryStar4 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 09:16 PM
  4. YES!!! I rule!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 10-30-2001, 09:23 AM