📜  实施辛普森3/8规则的程序

📅  最后修改于: 2021-04-26 05:14:12             🧑  作者: Mango

编写程序以实现Simpson的3/8规则。
辛普森一家的3/8规则是由托马斯·辛普森(Thomas Simpson)开发的。此方法用于执行数值积分。此方法通常用于定积分的数值逼近。在此,抛物线用于近似曲线的每个部分。
辛普森(Simpson)的3/8公式:
\int_{a}^{b} f(x) dx  = \frac{3h}{8}  [Tex]([/ Tex] F(a)+ 3F (  [Tex] \ frac {2a + b} {3})[/ Tex] + 3F (  [Tex] \ frac {a + 2b} {3})[/ Tex] + F(b) )
这里,
h是h =(b-a)/ n给出的间隔大小
n是间隔数或间隔限制
例子 :

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
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


Javascript


输出 :

0.687927