Program to implement Simpson’s 3/8 rule

Write a program to implement Simpson’s 3/8 rule.

The Simpsons’s 3/8 rule was developed by Thomas Simpson. This method is used for performing numerical integrations. This method is generally used for numerical approximation of definite integrals. Here, parabolas are used to approximate each part of curve.

Simpson’s 3/8 formula :



 \int_{a}^{b} f(x) dx =  \frac{3h}{8} ( F(a) + 3F (  \frac{2a + b}{3} ) + 3F(  \frac{a + 2b}{3} ) + F(b) )

Here,
h is the interval size given by h = ( b – a ) / n
n is number of intervals or interval limit

Examples :

Input : lower_limit = 1, upper_limit = 10, 
        interval_limit = 10
Output : integration_result = 0.687927


Input : lower_limit = 1, upper_limit = 5, 
        interval_limit = 3
Output : integration_result = 0.605835

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to implement Simpson's rule
#include<iostream>
using namespace std;
  
// Given function to be integrated
float func( float x)
{
    return (1 / ( 1 + x * x ));
}
  
// Function to perform calculations
float calculate(float lower_limit, float upper_limit,
                int interval_limit )
{
    float value;
    float interval_size = (upper_limit - lower_limit)
                          / interval_limit;
    float sum = func(lower_limit) + func(upper_limit);
  
    // Calculates value till integral limit
    for (int i = 1 ; i < interval_limit ; i++)
    {
        if (i % 3 == 0)
            sum = sum + 2 * func(lower_limit + i * interval_size);
        else
            sum = sum + 3 * func(lower_limit + i * interval_size);
    }
    return ( 3 * interval_size / 8 ) * sum ;
}
  
// Driver Code
int main()
{
    int interval_limit = 10;
    float lower_limit = 1;
    float upper_limit = 10;
    float integral_res = calculate(lower_limit, upper_limit,
                                   interval_limit);
  
    cout << integral_res;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to implement Simpson's rule
import java.util.*;
  
class GFG {
      
    // Given function to be integrated
    static float func( float x)
    {
        return (1 / ( 1 + x * x ));
    }
       
    // Function to perform calculations
    static float calculate(float lower_limit, 
                           float upper_limit, int interval_limit )
    {
        float value;
        float interval_size = (upper_limit - lower_limit)
                               / interval_limit;
  
        float sum = func(lower_limit) + func(upper_limit);
       
        // Calculates value till integral limit
        for (int i = 1 ; i < interval_limit ; i++)
        {
            if (i % 3 == 0)
                sum = sum + 2 * func(lower_limit 
                                     + i * interval_size);
            else
                sum = sum + 3 * func(lower_limit + i 
                                     * interval_size);
        }
        return ( 3 * interval_size / 8 ) * sum ;
    }
      
    // Driver program to test above function
    public static void main(String[] args)
    {
        int interval_limit = 10;
        float lower_limit = 1;
        float upper_limit = 10;
        float integral_res = calculate(lower_limit, upper_limit,
                                       interval_limit);
       
        System.out.println(integral_res);
        }
    }
  
// This article is contributed by Arnav Kr. Mandal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to implement
# Simpson's rule
  
# Given function to be
# integrated
def func(x):
      
    return (float(1) / ( 1 + x * x ))
  
   
# Function to perform calculations
def calculate(lower_limit, upper_limit, interval_limit ):
      
    interval_size = (float(upper_limit - lower_limit) / interval_limit)
    sum = func(lower_limit) + func(upper_limit);
   
    # Calculates value till integral limit
    for i in range(1, interval_limit ):
        if (i % 3 == 0):
            sum = sum + 2 * func(lower_limit + i * interval_size)
        else:
            sum = sum + 3 * func(lower_limit + i * interval_size)
      
    return ((float( 3 * interval_size) / 8 ) * sum )
  
# driver function
interval_limit = 10
lower_limit = 1
upper_limit = 10
  
integral_res = calculate(lower_limit, upper_limit, interval_limit)
  
# rounding the final answer to 6 decimal places 
print (round(integral_res, 6))
  
# This code is contributed by Saloni.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code to implement Simpson's rule
using System;
  
class GFG {
      
    // Given function to be integrated
    static float func( float x)
    {
        return (1 / ( 1 + x * x ));
    }
      
    // Function to perform calculations
    static float calculate(float lower_limit, 
                        float upper_limit, int interval_limit )
    {
        //float value;
        float interval_size = (upper_limit - lower_limit)
                            / interval_limit;
  
        float sum = func(lower_limit) + func(upper_limit);
      
        // Calculates value till integral limit
        for (int i = 1 ; i < interval_limit ; i++)
        {
            if (i % 3 == 0)
                sum = sum + 2 * func(lower_limit 
                                    + i * interval_size);
            else
                sum = sum + 3 * func(lower_limit + i 
                                    * interval_size);
        }
        return ( 3 * interval_size / 8 ) * sum ;
    }
      
    // Driver program to test above function
    public static void Main()
    {
        int interval_limit = 10;
        float lower_limit = 1;
        float upper_limit = 10;
        float integral_res = calculate(lower_limit, upper_limit,
                                    interval_limit);
      
        Console.WriteLine(integral_res);
        }
    }
  
// This code is contributed by Vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to implement 
// Simpson's rule
  
// Given function to be integrated
function func( $x)
{
    return (1 / ( 1 + $x * $x ));
}
  
// Function to perform calculations
function calculate($lower_limit, $upper_limit,
                   $interval_limit)
{
    $interval_size = ($upper_limit
                      $lower_limit) / 
                      $interval_limit;
    $sum = func($lower_limit) + 
           func($upper_limit);
  
    // Calculates value till
    // integral limit
    for ($i = 1 ; $i < $interval_limit ; $i++)
    {
        if ($i % 3 == 0)
            $sum = $sum + 2 * func($lower_limit
                                   $i * $interval_size);
        else
            $sum = $sum + 3 * func($lower_limit
                                   $i * $interval_size);
    }
    return ( 3 * $interval_size / 8 ) * $sum ;
}
  
// Driver Code
$interval_limit = 10;
$lower_limit = 1;
$upper_limit = 10;
$integral_res = calculate($lower_limit, $upper_limit,
                          $interval_limit);
  
echo $integral_res;
      
// This code is contributed by mits.
?>

chevron_right



Output :

0.687927


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.