Thread: What's wrong with this numerical integration function?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    What's wrong with this numerical integration function?

    Hi, I'm writing this function to numerically approximate the value of the function f(x) = exp(-x^2) over the interval [0,2] (Simpson's Rule). I start with n = 4, and the tolerance level, defined as the absolute value of the difference between the old value and the new value of the numerical approximation. I give it a pre-defined tolerance level. I've run this program and it got stuck: no output given, anything entered from the keyboard just makes it a new line... Could you please help debug this small program? Thanks a lot.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define TOL 0.00000005
    
    double isimp(int a, int b, int n);
    
    int main(void)
    {
    	int a, b, n;
    	double i_approx = 0.0, i_old, i_new;
    	
    	printf("Enter left-end point a: ");
    	scanf("%d", &a);
    	printf("Enter left-end point b: ");
    	scanf("%d", &b);
    	printf("Enter # of partition intervals: ");
    	scanf("%d", &n);
    	
    	n = 4;
    	i_old = isimp(a, b, n);
    	n = 2*n;
    	i_new = isimp(a, b, n);
    	while (fabs(i_new - i_old) > TOL) {
    		i_old = i_new;
    		n = 2*n;
    		i_new = isimp(a, b, n);
    	}
    	
    	i_approx = i_new;
    	
    	printf("n = %d\ni_approx = %lf\n", n, i_approx);
    	
    	return 0;
    }
    
    double isimp(int a, int b, int n) 
    {
    
    int i;
    double i_simp = 0.0, h, xi;
    
    h = (b - a) / n;
    i_simp = (exp(-pow(a, 2)) + exp(-pow(b, 2))) / 6;
    
    for (i = 1; i <= (n-1); n++) {
    	xi = a + h*(i - 0.5);
    	i_simp = i_simp + (2/3) * exp(-pow(xi, 2));
    }
    
    i_simp = h * i_simp;
    
    return i_simp;
    }
    Last edited by maccat; 10-27-2009 at 07:39 AM.

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    At first glance:

    Code:
    double i_simp = 0.0, h, xi;
    
    h = (b - a) / n;
    a, b, n are integers and h is a double. You need to cast them as doubles in order to do some floating point arithmetic. Also, pow(,) expects floating point variables, not integers.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    i <= (n-1);
    why not just i < n ?

    Code:
    n = 2*n;
    n *= 2;

    etc etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM