Thread: perform numeric integration

  1. #1
    Registered User
    Join Date
    Oct 2005

    perform numeric integration

    How would I perform numeric integration?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    How indeed.

    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    Read this (the whole thing, you need it):
    If you understand what you're doing, you're not learning anything.

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

    One simple way is:
    #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


    We have to use the trapezoidal rule with Richarson's expolation

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >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-14-2008, 12:58 AM
  2. Replies: 3
    Last Post: 02-26-2008, 02: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, 02:53 PM