Thread: Simpsons Rule Problem

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    46

    Simpsons Rule Problem

    Hey guys, I have looked around and am having trouble figuring out why my program is not integrating the function correctly. Here is my program and a sample of the output.

    Code:
    #include <stdio.h>  //for use of the standard i/o functions
    #include <stdlib.h> //for use of the standard library functions
    #include <math.h>   //for use of the math functions
    
    double function(double x) //prototype for passing a number into the function
    {
        return exp(-x*x); //the hardcoded function e to the -x squared
    }
    
    double simpson(double a, double b, int c) //passing variables into the simpson function for evaluating simpsons rule 
    //a is the integrate to, b is the integrate from
    //and c is the number of subdivisions
    {
           double dx = 1;  //intial value of dx is 1 for the first pass of the for loop
           int i = 0;   //this variable is used for a 'for' loop as a counter and index position in the array
           int n = 1;  //this will be used as the multiplier to dx which increments based on the number of subdivisions
           int j = 1;
           int arr_length = ++c;  //this array will hold the number of values calculated for the integration
           double odd_sum = 0;
           double even_sum = 0;
           double total_sum = 0;
           double answer_sum = 0;
           double first_sum = 0;
           double last_sum = 0;
           
           dx = (b - a)/(c);
           
           double arr_nums[arr_length]; //intialization of the array of numbers which will be summated for the integration
           
           for(i = 1; i < --c; i++) //runs a for loop to evaluate the areas of the subdivisions of the function
           {
               arr_nums[i] = function(a + (dx * j));
               j++;
           }
           
           if(n % 2 != 0 & n != c)
           {
               odd_sum = ((arr_nums[n]) * 4) + odd_sum;
               n++;
               n++;
           }
               
           n = 2;
           
           if(n % 2 == 0)
           {
                even_sum = ((arr_nums[n]) * 2) + even_sum;
                n++;
                n++;
           }
               
           first_sum = function(a);
           last_sum = function(b);
               
           for(i = 0; i < c; i++)
           {
                total_sum = arr_nums[i] + total_sum;
           }
           
           total_sum = total_sum + first_sum + last_sum;
               
           answer_sum = (dx / 3) * total_sum;
               
           return answer_sum; 
    }
    
    int main()
    {
        double integrate_from;
        double integrate_to;
        int num_of_subdivisions;
        int temp_holder;
        double answer;
        
        printf("Integrate from: \n");
        scanf("%lf", &integrate_from);
        printf("Integrate to: \n");
        scanf("%lf", &integrate_to);
        printf("Number of subdivisions (must be a multiple of 2): \n");
        scanf("%d", &num_of_subdivisions);
        
        if(num_of_subdivisions % 2 != 0)
        {
             printf("That is not a multiple of 2");          
             exit(1);
        }
            
        else
        {
             // answer = simpson(integrate_from, integrate_to, num_of_subdivisions);
             answer = simpson(integrate_from, integrate_to, num_of_subdivisions);
             printf("%.10f\nThe integration is: ", answer);        
        }
        
    }
    Sample output:
    Code:
    C:\Users\askinne2\Desktop\Fall 2010\CS\Labs>simpson
    Integrate from:
    0
    Integrate to:
    3.14
    Number of subdivisions (must be a multiple of 2):
    6
    0.3386618454

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Warnings generated:
    Code:
    simp.c: In function ‘simpson’:
    simp.c:28: warning: ISO C90 forbids variable-size array ‘arr_nums’
    simp.c:28: warning: ISO C90 forbids mixed declarations and code
    simp.c:36: warning: suggest parentheses around comparison in operand of &
    simp.c: In function ‘main’:
    simp.c:72: warning: unused variable ‘temp_holder’
    simp.c:95: warning: control reaches end of non-void function
    Of particular interest is the one on line 36. What is the correct syntax for boolean and?
    Code:
    if(n % 2 != 0 & n != c)
    What's the expected result, BTW? I left my calculus far behind me.
    Last edited by rags_to_riches; 09-23-2010 at 09:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 10:54 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM