Thread: what's wrong with this simple program?

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

    what's wrong with this simple program?

    Hi, I'm starting to learn C by self-study. I can't figure out how this function doesn't work out properly. It doesn't return the correct answer. It calculates the numerical integration of f(x) = 1/(1+x)^2 over the interval [a,b]. Could you pls help?


    Code:
    #include<stdio.h>
     
    double f_int(double a, double b, double n); /*Declarations*/
     
    int main(void)
    {
        double a, b, n;
        printf("This function calculates the integration from a to b of ");
        printf("the function f(x) = 1/(1+x)^2\n");
        printf("Enter left endpoint: ");
        scanf("%lf", &a);
        printf("Enter right endpoint: ");
        scanf("%lf", &b);
        printf("Enter number of partition intervals: ");
        scanf("%lf", &n);
        printf("The numerical result of this integration is %lf.\n", f_int(a, b, n));
     
        return 0;
    }
     
    double f_int(double a, double b, double n) /*Definitions*/
    {
        double h, xi, I_midpoint = 0.0f;
        int i;
     
        h = (b - a) / n;
        for(i = 1; i <= n; i++)
    {
            xi = a + (i - 0.5) * h;
            I_midpoint = I_midpoint + 1.0 / (1.0 + xi) * (1.0 + xi);
        }
     
        I_midpoint = h * I_midpoint;
     
    return I_midpoint;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1.0 / (1.0 + xi) * (1.0 + xi) is identically equal to 1.0.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    17
    Quote Originally Posted by tabstop View Post
    1.0 / (1.0 + xi) * (1.0 + xi) is identically equal to 1.0.
    Thanks a lot. A really stupid mistake indeed. Thanks

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Your order of operations needs some sowrk. add a couple parenthesis or use the pow() function

    Code:
    I_midpoint += pow(1.0 + xi , -2.0);
    Last edited by abachler; 10-08-2009 at 02:04 AM.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    17
    Quote Originally Posted by abachler View Post
    Your order of operations needs some sowrk. add a couple parenthesis or use the pow() function

    Code:
    I_midpoint += pow(1.0 + xi , -2.0);
    So it means you include #include<math.h> to call this function? Thanks a lot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  4. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM