Program to implement Simpson's 3/8 rule Last Updated : 21 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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 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 limitExamples : 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++ // 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; } Java // 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. Python3 # 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. C# // 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. PHP <?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. ?> JavaScript <script> // javascript 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 ) { let value; let interval_size = (upper_limit - lower_limit) / interval_limit; let sum = func(lower_limit) + func(upper_limit); // Calculates value till integral limit for (let 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 Function let interval_limit = 10; let lower_limit = 1; let upper_limit = 10; let integral_res = calculate(lower_limit, upper_limit, interval_limit); document.write(integral_res); // This code is contributed by susmitakundugoaldanga. </script> Output : 0.687927 Time Complexity: O(interval_limit)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to implement Simpson's 3/8 rule A Arnav Kr Improve Article Tags : Misc Mathematical DSA Algebra Practice Tags : MathematicalMisc Similar Reads Program for Simpson's 1/3 Rule 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 appr 7 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 Round to next smaller multiple of 8 Given an unsigned integer x. Round it down to the next smaller multiple of 8 using bitwise operations only.Examples: Input : 35 Output : 32 Input : 40 Output : 40 As 40 is already a multiple of 8. So, no modification is done. Solution 1: A naive approach to solve this problem using arithmetic operat 4 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 C++ program to divide a number by 3 without using *, / , +, -, % operators For a given positive number we need to divide a number by 3 without using any of these *, /, +, â % operatorsExamples: Input : 48 Output : 16 Input : 16 Output : 5 Algorithm Take a number num, sum = 0while(num>3), shift the number left by 2 bits and sum = add(num >> 2, sum). Create a functi 2 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 determine the octant of the axial plane Given 3 coordinates x, y and z, the task is to determine the octant of the axial plane.Examples: Input: 2, 3, 4 Output: Point lies in 1st octant Input: -4, 2, -8 Output: Point lies in 6th octant Input: -6, -2, 8 Output: Point lies in 3rd octant Approach: Given below are the conditions which need to 10 min read Program to convert float decimal to Octal number Write a program that converts a floating-point decimal number into its octal format. The program should take a number and the precision for the octal fractional part as inputs from the user. It should calculate and provide its correct octal representation as output. Examples: Input: Number = 123.45, 8 min read Program to implement Inverse Interpolation using Lagrange Formula Given task is to find the value of x for a given y of an unknown function y = f(x) where values of some points (x, y) pairs are given.Let, y = f(x) be an unknown function where x in an independent variable. For different values of x, say x_0, x_1, x_2, ..., x_m ( i.e x_k, k=0, 1, 2, 3...m) values of 8 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 Like