perform numeric integration

This is a discussion on perform numeric integration within the C Programming forums, part of the General Programming Boards category; How would I perform numeric integration?...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    perform numeric integration

    How would I perform numeric integration?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How indeed.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Read this (the whole thing, you need it): http://www.catb.org/~esr/faqs/smart-questions.html
    If you understand what you're doing, you're not learning anything.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Do you want to know in general, or do you have a particular algorithm in mind?

    One simple way is:
    Code:
    #include <stdio.h>
    #include <math.h>
                                                                                    
    double f(double x) {
      return 1.0 + exp(x);
    }
    
    /* Integrates f from a to b, using n intervals. */
    double integrate(double (*f)(double), double a, double b, int n) {
    
      double interval_size = (b - a) / n;
      double x = a + interval_size * 0.5;
      double ret = 0.0;
                                                                                    
      while (n--) {
        ret += (*f)(x);
                                                                                    
        x += interval_size;
      }
                                                                                    
      ret *= interval_size;
                                                                                    
      return ret;
    }
                                                                                    
    int main(void) {
                                                                                    
      int i;
                                                                                    
      puts("Approximations of the area underneath 1+e^x from 0 to 1:");
                                                                                    
      for (i = 1; i <= 20; ++i) {
        printf("%d intervals: %f\n", i, integrate(&f, 0.0, 1.0, i));
      }
                                                                                    
      return 0;
    }
    This is known as the 'midpoint method,' and is relatively well-known. There are many more sophisticated general numeric integration algorithms; this is about the simplest you'll find. Don't forget about floating point error.

    For some popular functions, there exist specialized formulas that approximate the integral extremely well. For example, the normal curve has a whole slew of specialized formulas that are much more efficient than naive integration. So you might want to look around for those if you have a peculiar function in mind.
    Last edited by Rashakil Fol; 10-17-2005 at 05:44 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    Thank-You!!!

    Thank-You!!
    We have to use the trapezoidal rule with Richarson's expolation

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >We have to use the trapezoidal rule with Richarson's expolation
    That's nice. And what was your attempt at solving the problem?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working with numeric data in files
    By bored_guy in forum C Programming
    Replies: 1
    Last Post: 11-13-2008, 11:58 PM
  2. Replies: 3
    Last Post: 02-26-2008, 01:12 PM
  3. Simple vector class problem, please help
    By fidodidohk in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2007, 09:13 AM
  4. only accept numeric data
    By willc0de4food in forum Windows Programming
    Replies: 6
    Last Post: 08-19-2005, 03:38 AM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 01:53 PM

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