Thread: Simpson's Rule (not composite simpson's rule)

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    13

    Simpson's Rule (not composite simpson's rule)

    im trying to solve an integral for any function according to the Simpson's rule (not composite Simpson's rule):

    have i defined the function properly?
    Code:
    double simpsons ( double a, int n, double b, double h)
    
    {
             int i;
             double ans[10000], x=a, final_ans, ans1, holding=0;
             
             for ( i = 0; i <= n-1; i++ )
         
                  {  
                     ans[i] = exp (2*x);             ///function to be integrated e^2x ans[] is an array of all x values.
                     x = x + h; 
                  }
         
             for ( i = 1; i <= n-2; i+=2 )
                 {  
                    ans1 = 4*ans[i];
                    holding = holding + ans1; 
                 }             
             
             final_ans = (h/3)*(ans[0] + holding + ans[n-1]);
             return final_ans;
    }
    the value returned is incorrect and does not correspond to the value when using Simpson's rule (not composite Simpson's rule). I keep getting a large number.
    the value should be equal to 2.799598

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Perhaps post your main() as well, so we can see what parameters you pass to this function.

    We could also run the same test as you as well.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Quote Originally Posted by Salem View Post
    Perhaps post your main() as well, so we can see what parameters you pass to this function.

    We could also run the same test as you as well.
    Code:
    double simpsons ( double a, int n, double b, double ans);
    double Anal_function (double a, double b);
    int main ()
    {
        double a, b, h, ans, Anal_ans;           ////variables to the function 
        int n;                                     ///variable to the function (n should be an integer)
        
        
        printf ("Please enter Start value (a):");
        scanf ("%lf", &a);
        
        printf ("Please enter End value (b);");
        scanf ("%lf", &b);
        
        printf("Please enter number of segments (n);");
        scanf ("lf", &n);
        
        h= (b-a)/2.0;                          ///defining segment size
        
        ans= simpsons (a, b, h, n);                        //// ans  is equal to the simpsons function, that is defined later)
        Anal_ans= Anal_function (a, b);                    //// here we linked the two variables that are defined differently.
        
        printf ("The value of Integral according to Simpson's Rule= %0.8f\n",ans);
        printf ("The value from the Analytical method = %0.8f\n", Anal_ans);
        
        system ("pause");
        return 0;
        
    }
    
    
    double simpsons ( double a, int n, double b, double h)
    {
             int i;
             double ans[10000], x=a, final_ans, ans1, holding=0;
             
             for ( i = 0; i <= n-1; i++ )
         
                  {  
                     ans[i] = exp (2*x);             ///function to be integrated e^2x ans[] is an array of all x values.
                     x = x + h; 
                  }
         
             for ( i = 1; i <= n-2; i+=2 )
                 {  
                    ans1 = 4*ans[i];
                    holding = holding + ans1; 
                 }             
             
             h= (b+a)/2.0;
             
             final_ans = (h/3)*(ans[0] + holding + ans[n-1]);
             return final_ans;
    }
    
    
    double Anal_function (double a, double b)
    {      
           double Anal_ans;
           Anal_ans = (0.5*(exp(2*b)))-(0.5*(exp(2*a)));
           return Anal_ans;
           }
    what im trying here is to do the simpson's rule with 3 points only, the start value, end value and the midpoint.
    where f(x) dx= ((b-a)/6)* [f(a) + 4f*(a+b/2) + f(b)].
    Last edited by Solas99; 04-01-2013 at 12:08 PM.

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Anal functions.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Quote Originally Posted by Barney McGrew View Post
    Anal functions.
    analytical function!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    $ gcc -W -Wall -Wextra foo.c -lm
    foo.c: In function ‘main’:
    foo.c:20:5: warning: too many arguments for format [-Wformat-extra-args]

    Your scanf for n isn't all that it seems to be.
    For a start, it lacks a %
    Then it's completely wrong for reading an int anyway.

    Additionally, telling us the values you would like to type in will help us check results.
    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.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Quote Originally Posted by Salem View Post
    $ gcc -W -Wall -Wextra foo.c -lm
    foo.c: In function ‘main’:
    foo.c:20:5: warning: too many arguments for format [-Wformat-extra-args]

    Your scanf for n isn't all that it seems to be.
    For a start, it lacks a %
    Then it's completely wrong for reading an int anyway.

    Additionally, telling us the values you would like to type in will help us check results.
    For n, i presume we dont need it as there is only 2 segments, am i right?
    the integral is between 0 and 1.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    does anyone know where the problem is?

  9. #9
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    You should take some time to read about scanf: scanf(3) - Linux manual page

    Particularly, you want to read about the conversion specifiers used in scanf's format string (and the types you need to match them with), as well as scan'f return value which is used to indicate various things. As you start to larger write programs you'll need get used to reading documentation so you can call functions appropriately.

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    thanks for the link, i found it very helpful..my issue is with the simpsons function. i dont think i have the maths right in terms of translating it into code.
    Code:
    double simpsons ( double a, int n, double b, double h)
    {
             int i;
             double ans[10000], x=a, final_ans, ans1, holding=0;
    
             for ( i = 0; i <= n-1; i++ )
    
                  {  
                     ans[i] = exp (2*x);             ///function to be integrated e^2x ans[] is an array of all x values.
                     x = x + h; 
                  }
    
             for ( i = 1; i <= n-2; i+=2 )
                 {  
                    ans1 = 4*ans[i];
                    holding = holding + ans1; 
                 }             
    
             h= (b+a)/2.0;
    
             final_ans = (h/3)*(ans[0] + holding + ans[n-1]);
             return final_ans;
    }
    



  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    > does anyone know where the problem is?
    Well until you've shown us that you've fixed your scanf for n, we all know you're just passing garbage values into your function, so any further discussion is moot.

    Nor have you told us what values you typed in for a, b (and n - perhaps).
    Without this information, all we can do is look at the code and find semantic errors.

    We can't run the code to examine in detail runtime behaviour to see where you're screwing up.
    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. Trouble with Simpson's rule in my program
    By chinesebarbie in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2011, 10:38 PM
  2. Simpson 3/8 rule
    By Matts in forum C Programming
    Replies: 3
    Last Post: 06-16-2011, 08:23 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. simpson's 1/3rd rule
    By rakeshkool27 in forum C Programming
    Replies: 16
    Last Post: 03-22-2010, 07:40 AM
  5. SImpson's Rule
    By Brent in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 09:34 PM