📜  使用Boole规则查找给定函数的积分的程序

📅  最后修改于: 2021-04-22 01:38:10             🧑  作者: Mango

给定一个函数f(x) ,在点列表

x_i

等距

h=x_{i+1} - x_i

这样

f_1 = f(x_1)  [Tex] f_2 = f(x_2)[/ Tex]

  • …..等等

上限和下限a,b对应于需要找到积分的位置,任务是找到给定方程f(x)的积分值。
例子:

方法:在本文中,讨论了布尔规则,以计算给定函数f(x)的近似积分值。
布尔定律是一种找到积分近似值的数值积分技术。它是由一位自学成才的数学家,哲学家和逻辑学家乔治·布尔(George Boole)命名的。布尔技术的思想是在某些等距值(给定图像中的h)处使用’f k ‘的值来近似积分。下图显示了如何考虑各种f k

布尔定律的积分值由以下公式给出:

  • 在上面的公式中,当积分为6阶时出现的误差项。误差项为

- \frac{8}{945}h^{7}f_{6} [Tex] f_1,f_2,f_3,f_4,f_5,[/ Tex]

  • 是f(x)的值在它们各自的x间隔。
  • 因此,可以按照以下步骤计算间隔(a,b)中某些函数f(x)的积分:
    1. n = 6的值,即间隔被划分成的部分的数量。
    2. 计算宽度,h =(b – a)/ 4。
    3. 将x 1到x 5的值计算为

x_1 = a, x_2 = x_1 + h, x_3 = x_1 + 2h, ...x_5=x_1 +4h

  • 考虑y = f(x)。现在找到值

y(y_1, y_2, .. y_5)

  • 对于相应的

x(x_1, x_2, x_3... x_5)

  • 价值观。
  • 将所有值替换为Boole规则以计算积分值。

下面是上述方法的实现:

C++
// C++ program to implement Boole's Rule
// on the given function
#include 
using namespace std;
 
// In order to represent the implementation,
// a function f(x) = 1/(1 + x) is considered
// in this program
 
// Function to return the value of f(x)
// for the given value of x
float y(float x)
{
    return (1 / (1 + x));
}
 
// Function to computes the integrand of y
// at the given intervals of x with
// step size h and the initial limit a
// and final limit b
float BooleRule(float a, float b)
{
 
    // Number of intervals
    int n = 4;
    int h;
 
    // Computing the step size
    h = ((b - a) / n);
    float sum = 0;
 
    // Substituing a = 0, b = 4 and h = 1
    float bl = ((7 * y(a) +
                32 * y(a + h) +
                12 * y(a + 2 * h) +
                32 * y(a + 3 * h) +
                 7 * y(a + 4 * h)) *
                 2 * h / 45);
 
    sum = sum + bl;
    return sum;
}
 
// Driver code
int main()
{
    float lowlimit = 0;
    float upplimit = 4;
     
    cout << fixed << setprecision(4) <<
        "f(x) = " << BooleRule(0, 4);
         
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// C program to implement Boole's Rule
// on the given function
 
#include 
#include 
 
// In order to represent the implementation,
// a function f(x) = 1/(1 + x) is considered
// in this program
 
// Function to return the value of f(x)
// for the given value of x
float y(float x)
{
    return (1 / (1 + x));
}
 
// Function to computes the integrand of y
// at the given intervals of x with
// step size h and the initial limit a
// and final limit b
float BooleRule(float a, float b)
{
    // Number of intervals
 
    int n = 4;
    int h;
 
    // Computing the step size
    h = ((b - a) / n);
    float sum = 0;
 
    // Substituing a = 0, b = 4 and h = 1
    float bl = (7 * y(a) + 32 * y(a + h)
                + 12 * y(a + 2 * h)
                + 32 * y(a + 3 * h)
                + 7 * y(a + 4 * h))
               * 2 * h / 45;
 
    sum = sum + bl;
    return sum;
}
 
// Driver code
int main()
{
    float lowlimit = 0;
    float upplimit = 4;
    printf("f(x) = %.4f",
           BooleRule(0, 4));
    return 0;
}


Java
// Java program to implement Boole's Rule
// on the given function
class GFG{
     
// In order to represent the implementation,
// a function f(x) = 1/(1 + x) is considered
// in this program
  
// Function to return the value of f(x)
// for the given value of x
static float y(float x)
{
    return (1 / (1 + x));
}
  
// Function to computes the integrand of y
// at the given intervals of x with
// step size h and the initial limit a
// and final limit b
static float BooleRule(float a, float b)
{
    // Number of intervals
  
    int n = 4;
    int h;
  
    // Computing the step size
    h = (int) ((b - a) / n);
    float sum = 0;
  
    // Substituing a = 0, b = 4 and h = 1
    float bl = (7 * y(a) + 32 * y(a + h)
                + 12 * y(a + 2 * h)
                + 32 * y(a + 3 * h)
                + 7 * y(a + 4 * h))
               * 2 * h / 45;
  
    sum = sum + bl;
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    System.out.printf(("f(x) = %.4f"),
           BooleRule(0, 4));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement Boole's Rule
# on the given function
 
# In order to represent the implementation,
# a function f(x) = 1/(1 + x) is considered
# in this program
 
# Function to return the value of f(x)
# for the given value of x
def y(x):
    return (1 / (1 + x))
 
# Function to computes the integrand of y
# at the given intervals of x with
# step size h and the initial limit a
# and final limit b
def BooleRule(a, b):
     
    # Number of intervals
    n = 4
 
    # Computing the step size
    h = ((b - a) / n)
    sum = 0
 
    # Substituing a = 0, b = 4 and h = 1
    bl = (7 * y(a) + 32 * y(a + h) + 12 *
        y(a + 2 * h)+32 * y(a + 3 * h)+7 *
        y(a + 4 * h))* 2 * h / 45
 
    sum = sum + bl
    return sum
 
# Driver code
if __name__ == '__main__':
    lowlimit = 0
    upplimit = 4
    print("f(x) =",round(BooleRule(0, 4),4))
 
# This code is contributed by Surendra_Gangwar


C#
// C# program to implement Boole's
// Rule on the given function
using System;
class GFG{
 
// In order to represent the
// implementation, a function
// f(x) = 1/(1 + x) is considered
// in this program
 
// Function to return the value of
// f(x) for the given value of x
static float y(float x)
{
  return (1 / (1 + x));
}
 
// Function to computes the integrand
// of y at the given intervals of x
// with step size h and the initial
// limit a and final limit b
static float BooleRule(float a,
                       float b)
{
  // Number of intervals
  int n = 4;
  int h;
 
  // Computing the step size
  h = (int)((b - a) / n);
  float sum = 0;
 
  // Substituing a = 0, b = 4
  // and h = 1
  float bl = (7 * y(a) + 32 *
              y(a + h) + 12 *
              y(a + 2 * h) +
              32 * y(a + 3 *
              h) + 7 * y(a +
              4 * h)) * 2 * 
              h / 45;
 
  sum = sum + bl;
  return sum;
}
 
// Driver code
public static void Main(string[] args)
{
  Console.Write(("f(x) = " +
                  System.Math.Round(
                  BooleRule(0, 4), 4)));
}
}
 
// This code is contributed by Chitranayal


输出:
f(x) = 1.6178