Simpsons 3/8 Rule C Program

By | June 26, 2017

Let’s understand the Simpson’s 3/8th rule method in numerical analysis and implement Simpsons 3/8 Rule in C programming language. You will get an overall idea about Simpson’s rule here.

What is Simpsons 3/8 Rule?

The Simpson’s 3/8th rule was developed by a mathematician named Thomas Simpson. Integration is the process of measuring the area under a function plotted on a graph.

The Simpson’s 3/8th rule is used in complex numerical integrations. This integration method uses parabolas to approximate each part of the curve. It is basically used to measure an area in a curve.

The Simpson’s 3/8th method is used for uniformly sampled function integration purpose. The Simpson’s 3/8th integration method is primarily used for numerical approximation of definite integrals.

Simpson’s Rule Formula

Simpson's 3/8 Rule Formula with Example, Output

Algorithm For Simpson’s 3/8 Rule

Step 1. Input a, b and the number of intervals.
Step 2. Compute the value of h using this formula: h = (b - a) / n
Step 3. Calculate the value of sum using this formula: sum = f(a) + f(b)
Step 4. If n is an odd number, then
              sum = sum + 2 * y(a + i * h)
        Else
              sum = sum + 3 * y(a + i * h)
Step 5. Calculate the Simpson's integration value using this formula: sum* 3 * h / 8

Note: This C Program for Simpson’s 3/8 rule is compiled with GNU GCC compiler on CodeLite IDE. However, it is compatible with all other operating systems.

Method 1: C Program For Simpson’s 3/8th Rule using Function

#include<stdio.h>

float function(float temp)
{
      return 1 / (1 + temp * temp);
}

int main()
{
      float lower_bound, upper_bound, h, sum = 0, value;
      int count, interval;
      printf("Enter Lower Boundary Value:\t");
      scanf("%f", &lower_bound);
      printf("Enter Upper Boundary Value:\t");
      scanf("%f", &upper_bound);
      printf("\nEnter Interval Limit:\t");
      scanf("%d", &interval);
      h = (upper_bound - lower_bound) / interval;
      sum = function(lower_bound) + function(upper_bound);
      for(count = 1; count < interval; count++)
      {
            if(count % 3 == 0)
            {
                  sum = sum + 2 * function(lower_bound + count * h);
            }
            else
            {
                  sum = sum + 3 * function(lower_bound + count * h);
            }
      }
      value = (3 * h / 8) * sum;
      printf("\nValue of Simpson's 3/8 Rule Integration:\t%f\n", value);
      return 0;
}

Output

Simpsons 3/8 Rule in C Programming

Note: This C Program for Simpsons Integration 3/8 rule is compiled with GNU GCC compiler on Linux Ubuntu operating system. However, it is compatible with all other operating systems.

 

Method 2: C Program For Simpsons 3/8 Rule without using Function

#include 
#include 

int main() 
{ 
      float x[10], y[10], sum = 0, h; 
      int count, limit, m, n; 
      printf("\nEnter Limit:\t"); 
      scanf("%d", &limit); 
      for(count = 0; count < limit; count++) 
      { 
            printf("\nEnter Value For X[%d]:\t", count); 
            scanf("%f", &x[count]); 
            printf("\nEnter Value For F{x[%d]}:\t", count); 
            scanf("%f", &y[count]); 
      } 
      h = x[1] - x[0]; 
      limit = limit - 1; 
      sum = sum + y[0]; 
      for(count = 1; count < limit; count++) 
      { 
            if(m == 0 || n == 0) 
            { 
                  sum = sum + 3 * y[count]; 
                  if(m == 1) 
                  { 
                        n = 1; 
                  } 
                  m = 1; 
            } 
            else 
            { 
                  sum = sum + 2 * y[count]; 
                  m = 0; 
                  n = 0; 
            } 
      } 
      sum = sum + y[count]; 
      sum = sum * (3 * h / 8); 
      printf("\n\nSimple 3/8 Rule Integral Value:\t%f\n", sum); 
      return 0;
}

Output

Simpson's 3/8 Rule in C Programming using Function

If you have any doubts about the implementation of Simpson’s 3/8th method in C programming, let us know about it in the comment section. Find more about it on Wikipedia.

NUMERICAL METHODS C PROGRAMS
Newton-Raphson Method C Program
Weddle’s Rule Algorithm C Program
Euler’s Method C Program
Secant Method C Program
Bisection Method C Program
Gauss Seidel Method C Program
Picard’s Method C Program
Regula Falsi Method C Program
Bisection Method Algorithm and Flowchart
Simpson’s 1/3rd Rule C Program
Trapezoidal Rule C Program

Let's Discuss