Program for Simpson's 1/3 Rule
Last Updated :
31 Jul, 2024
In numerical analysis, Simpson's 1/3 rule is a method for numerical approximation of definite integrals. Specifically, it is the following approximation:
$$\int_{a}^{b} f(x) dx \approx \frac{(b-a)}{6} \bigg(f(a) + 4f \frac{(a+b)}{2} + f(b)\bigg)$$
In Simpson's 1/3 Rule, we use parabolas to approximate each part of the curve.We divide
the area into n equal segments of width Δx.
Simpson's rule can be derived by approximating the integrand f (x) (in blue)
by the quadratic interpolant P(x) (in red).

In order to integrate any function f(x) in the interval (a, b), follow the steps given below:
1.Select a value for n, which is the number of parts the interval is divided into.
2.Calculate the width, h = (b-a)/n
3.Calculate the values of x0 to xn as x0 = a, x1 = x0 + h, .....xn-1 = xn-2 + h, xn = b.
Consider y = f(x). Now find the values of y(y0 to yn) for the corresponding x(x0 to xn) values.
4.Substitute all the above found values in the Simpson's Rule Formula to calculate the integral value.
Approximate value of the integral can be given by Simpson's Rule:
$$\int_{a}^{b} f(x) dx \approx \frac{h}{3} \bigg(f_0 + f_n + 4 * \sum_{i=1,3,5}^{n-1}f_i + 2* \sum_{i=2,4,6}^{n-2}f_i \bigg)$$
Note : In this rule, n must be EVEN.
Application :
It is used when it is very difficult to solve the given integral mathematically.
This rule gives approximation easily without actually knowing the integration rules.
Example :
Evaluate logx dx within limit 4 to 5.2.
First we will divide interval into six equal
parts as number of interval should be even.
x : 4 4.2 4.4 4.6 4.8 5.0 5.2
logx : 1.38 1.43 1.48 1.52 1.56 1.60 1.64
Now we can calculate approximate value of integral
using above formula:
= h/3[( 1.38 + 1.64) + 4 * (1.43 + 1.52 +
1.60 ) +2 *(1.48 + 1.56)]
= 1.84
Hence the approximation of above integral is
1.827 using Simpson's 1/3 rule.
C++
// CPP program for simpson's 1/3 rule
#include <iostream>
#include <math.h>
using namespace std;
// Function to calculate f(x)
float func(float x)
{
return log(x);
}
// Function for approximate integral
float simpsons_(float ll, float ul, int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x and f(x)
float x[10], fx[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver program
int main()
{
float lower_limit = 4; // Lower limit
float upper_limit = 5.2; // Upper limit
int n = 6; // Number of interval
cout << simpsons_(lower_limit, upper_limit, n);
return 0;
}
Java
// Java program for simpson's 1/3 rule
public class GfG{
// Function to calculate f(x)
static float func(float x)
{
return (float)Math.log(x);
}
// Function for approximate integral
static float simpsons_(float ll, float ul,
int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x
// and f(x)
float[] x = new float[10];
float[] fx= new float[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver Code
public static void main(String s[])
{
// Lower limit
float lower_limit = 4;
// Upper limit
float upper_limit = (float)5.2;
// Number of interval
int n = 6;
System.out.println(simpsons_(lower_limit,
upper_limit, n));
}
}
// This code is contributed by Gitanjali
Python3
# Python code for simpson's 1 / 3 rule
import math
# Function to calculate f(x)
def func( x ):
return math.log(x)
# Function for approximate integral
def simpsons_( ll, ul, n ):
# Calculating the value of h
h = ( ul - ll )/n
# List for storing value of x and f(x)
x = list()
fx = list()
# Calculating values of x and f(x)
i = 0
while i<= n:
x.append(ll + i * h)
fx.append(func(x[i]))
i += 1
# Calculating result
res = 0
i = 0
while i<= n:
if i == 0 or i == n:
res+= fx[i]
elif i % 2 != 0:
res+= 4 * fx[i]
else:
res+= 2 * fx[i]
i+= 1
res = res * (h / 3)
return res
# Driver code
lower_limit = 4 # Lower limit
upper_limit = 5.2 # Upper limit
n = 6 # Number of interval
print("%.6f"% simpsons_(lower_limit, upper_limit, n))
C#
// C# program for simpson's 1/3 rule
using System;
public class GfG
{
// Function to calculate f(x)
static float func(float x)
{
return (float)Math.Log(x);
}
// Function for approximate integral
static float simpsons_(float ll, float ul,
int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x
// and f(x)
float[] x = new float[10];
float[] fx= new float[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver Code
public static void Main()
{
// Lower limit
float lower_limit = 4;
// Upper limit
float upper_limit = (float)5.2;
// Number of interval
int n = 6;
Console.WriteLine(simpsons_(lower_limit,
upper_limit, n));
}
}
// This code is contributed by vt_m
JavaScript
<script>
// JavaScriptprogram for simpson's 1/3 rule
// Function to calculate f(x)
function func(x)
{
return Math.log(x);
}
// Function for approximate integral
function simpsons_(ll, ul, n)
{
// Calculating the value of h
let h = (ul - ll) / n;
// Array for storing value of x
// and f(x)
let x = [];
let fx= [];
// Calculating values of x and f(x)
for (let i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
let res = 0;
for (let i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver code
// Lower limit
let lower_limit = 4;
// Upper limit
let upper_limit = 5.2;
// Number of interval
let n = 6;
document.write(simpsons_(lower_limit,
upper_limit, n));
// This code is contributed by code_hunt.
</script>
PHP
<?php
// PhP program for simpson's 1/3 rule
// Function to calculate f(x)
function func($x)
{
return log($x);
}
// Function for approximate integral
function simpsons_($ll, $ul, $n)
{
// Calculating the value of h
$h = ($ul - $ll) / $n;
// Calculating values of x and f(x)
for ($i = 0; $i <= $n; $i++)
{
$x[$i] = $ll + $i * $h;
$fx[$i] = func($x[$i]);
}
// Calculating result
$res = 0;
for ($i = 0; $i <= $n; $i++)
{
if ($i == 0 || $i == $n)
$res += $fx[$i];
else if ($i % 2 != 0)
$res += 4 * $fx[$i];
else
$res += 2 * $fx[$i];
}
$res = $res * ($h / 3);
return $res;
}
// Driver program
$lower_limit = 4; // Lower limit
$upper_limit = 5.2; // Upper limit
$n = 6; // Number of interval
echo simpsons_($lower_limit, $upper_limit, $n);
// This code is contributed by ajit.
?>
Output:
1.827847
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Program to implement Simpson's 3/8 rule Write a program to implement Simpson's 3/8 rule.The Simpson'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 cur
6 min read
Program to find simple interest Given Principal p, Rate r and Time t, the task is to calculate Simple Interest.Examples :Input: p = 10000, r = 5, t = 5 Output:2500 Explanation: We need to find simple interest on Rs. 10,000 at the rate of 5% for 5 units of time. Input: p = 3000, r = 7, t = 1 Output:210The basic idea is to calculate
2 min read
Program for Volume and Surface Area of Cube Cube is a 3-dimensional box-like figure represented in the 3-dimensional plane. Cube has 6 squared-shape equal faces. Each face meet another face at 90 degree each. Three sides of cube meet at same vertex. Examples: Input : Side of a cube = 2 Output : Area = 8 Total surface area = 24 Input : Side of
3 min read
Program for finding the Integral of a given function using Boole's Rule Given a function, f(x), tabulated at points x_i equally spaced by h=x_{i+1} - x_i such that f_1 = f(x_1) f_2=f(x_2) .....and so on The upper and lower limits a, b correspond to which the integral needs to be found, the task is to find the integral value of the given equation f(x).Examples: Input: a
8 min read
Finding Integrand using Weedle's Rule Given a function f(x) and two integers a and b. The task is to find the integrand of the given function from lower limit(a) to the upper limit(b) using Weedle's Rule. The given function is: f(x) = \frac{1}{(1+x)^2} Examples: Input: a = 0, b = 6 Output: 1.373448 Input: a = 10, b = 13 Output: f(x) = 0
7 min read
Program for Stirling Interpolation Formula Given n number of floating values x, and their corresponding functional values f(x), estimate the value of the mathematical function for any intermediate value of the independent variable x, i.e., at x = a. Examples: Input : n = 5 x_1 = 0, x_2 = 0.5, x_3 = 1.0, x_4 = 1.5, x_5 = 2.0 f(x_1) = 0, f(x_2
13 min read
Program to print binomial expansion series Given three integers, A, X and n, the task is to print terms of below binomial expression series. (A+X)n = nC0AnX0 + nC1An-1X1 + nC2An-2X2 +....+ nCnA0Xn Examples: Input : A = 1, X = 1, n = 5 Output : 1 5 10 10 5 1 Input : A = 1, B = 2, n = 6 Output : 1 12 60 160 240 192 64 Simple Solution : We know
12 min read
Program to solve the Alligation Problem Write a program to find the ratio in which a shopkeeper will mix two types of rice worth Rs. X kg and Rs. Y kg, so that the average cost of the mixture is Rs. Z kg. Examples: Input: X = 50, Y = 70, Z = 65Output: Ratio = 1:3 Input: X = 1000, Y = 2000, Z = 1400Output: Ratio = 3:2 According to Alligati
6 min read
Program for EMI Calculator Provided with amount of money i.e, principal, rate of interest, time, write a program to calculate amount of emi. EMI stand for Equated Monthly Installment. This calculator is used to calculate per month EMI of loan amount if loan amount that is principal, rate of interest and time in years is given
4 min read
Probability of rain on N+1th day Given an array of 1 and 0's, where Ai = 1 denotes that ith day was a rainy day and Ai = 0 denotes it was not a rainy day. The task is to find the probability that the N+1th was a rainy day. Examples: Input: a[] = {0, 0, 1, 0} Output: .25 Since one day was rainy out of 4 days, hence the probability o
4 min read