Thread: Trapezoidal Rule for Integration

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    17

    Trapezoidal Rule for Integration

    Hi there, here is my code so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    float f(float x)
    {
     return(exp(-x*x));
    }
    
    
    float g(float x)
    {
     return(exp(-x));
    }
    
    
    float trapeze(float a, float b, float x, float h, float sum, int N, int i)
    {
      a = 0.0;
      b = 1.0;
    
    
      h = (b - a)/N;
      sum = ((0.5 * h) * (f(a) + f(b)));
      for (i = 1; i < N; i++)   {
        sum = sum + h * f(a + (i * h));
         }
      return (sum);
    }
    
    
    
    
    int main (void)
    {
      float c, d, e, trap;
      int n;
    
    
      printf("Enter the number of intervals: ");
      scanf("%d", &n);
      printf("integral of exp(-x*x): %f\n", trap(c(e)));
      printf("integral of exp(-x): %f\n", trap(d(e)));
    
    
      return(0);
    }

    Ok so, I know there's probably a lot wrong, so I need help as it goes. What I'm supposed to do is find the numerical integration of the functions above using my other function, trapeze. I don't know how to use functions as parameters though.

    Also, I'm probably forgetting, but why is my main function not recognizing my trapeze function? Variable names?

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    So I changed the c, d, e and all that business - now I'm getting that there's too few arguments to function 'trapeze'

    Why are there too few arguments?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    printf("integral of exp(-x*x): %f\n", trap(c(e)));
    printf("integral of exp(-x): %f\n", trap(d(e)));

    This makes no sense at all.
    trap, c and d are float variables, NOT FUNCTIONS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 10-12-2011, 03:39 PM
  2. The trapezoidal rule question
    By paranoidgnu in forum C Programming
    Replies: 9
    Last Post: 04-24-2011, 09:00 AM
  3. 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
  4. composite trapezoidal rule
    By Sam Robinson in forum C Programming
    Replies: 0
    Last Post: 05-19-2003, 10:01 AM
  5. Arrays and the trapezoidal rule
    By ChemistryStar4 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 09:16 PM