📜  使用Weedle规则查找整数

📅  最后修改于: 2021-05-06 23:14:56             🧑  作者: Mango

给定函数f(x)和两个整数ab 。任务是使用Weedle规则找到从下限( a )到上限( b )的给定函数的整数。
给定的函数是:
 f(x) = \frac{1}{(1+x)^2}

例子:

方法:
集成任何函数f(x)使用Weedle公式的公式为:

步骤如下:

  1. 从上面的公式中找到h的值,即h =  \frac{(b-a)}{6}
  2. 从中找到价值 x_1  x_7 并从中计算值 f(x_1)  f(x_7)
  3. 将上述值替换为Weedle公式即可得出积分值。

下面是上述方法的实现:

C++
// C++ program to Implement Weedle's Rule
#include 
using namespace std;
  
// A sample function f(x) = 1/(1+x^2)
float y(float x)
{
    float num = 1;
    float denom = 1.0 + x * x;
  
    return num / denom;
}
  
// Function to find the integral value
// of f(x) with step size h, with
// intial lower limit and upper limit
// a and b
float WeedleRule(float a, float b)
{
    // Find step size h
    double h = (b - a) / 6;
  
    // To store the final sum
    float sum = 0;
  
    // Find sum using Weedle's Formula
    sum = sum + (((3 * h) / 10) * (y(a)
                                + y(a + 2 * h)
                                + 5 * y(a + h)
                                + 6 * y(a + 3 * h)
                                + y(a + 4 * h)
                                + 5 * y(a + 5 * h)
                                + y(a + 6 * h)));
  
    // Return the final sum
    return sum;
}
  
// Driver Code
int main()
{
    // lower limit and upper limit
    float a = 0, b = 6;
  
    // Function Call
    cout<< "f(x) = "<< fixed <<  WeedleRule(a, b);
    return 0;
}
// This code is contributed by shivanisinghss2110


C
// C program to Implement Weedle's Rule
#include 
#include 
  
// A sample function f(x) =  1/(1+x^2)
float y(float x)
{
    float num = 1;
    float denom = 1.0 + x * x;
  
    return num / denom;
}
  
// Function to find the integral value
// of f(x) with step size h, with
// intial lower limit and upper limit
// a and b
float WeedleRule(float a, float b)
{
    // Find step size h
    double h = (b - a) / 6;
  
    // To store the final sum
    float sum = 0;
  
    // Find sum using Weedle's Formula
    sum = sum + (((3 * h) / 10) * (y(a)
                                   + y(a + 2 * h)
                                   + 5 * y(a + h)
                                   + 6 * y(a + 3 * h)
                                   + y(a + 4 * h)
                                   + 5 * y(a + 5 * h)
                                   + y(a + 6 * h)));
  
    // Return the final sum
    return sum;
}
  
// Driver Code
int main()
{
    // lower limit and upper limit
    float a = 0, b = 6;
  
    // Function Call
    printf("f(x) = %f", WeedleRule(a, b));
    return 0;
}


Java
// Java program to Implement Weedle's Rule
import java.util.*;
class GFG{
  
    // A sample function f(x) = 1/(1+x^2)
    static float y(float x)
    {
        float num = 1;
        float denom = (float)1.0 + x * x;
      
        return num / denom;
    }
      
    // Function to find the integral value
    // of f(x) with step size h, with
    // intial lower limit and upper limit
    // a and b
    static float WeedleRule(float a, float b)
    {
        // Find step size h
        float h = (b - a) / 6;
      
        // To store the final sum
        float sum = 0;
      
        // Find sum using Weedle's Formula
        sum = sum + (((3 * h) / 10) * (y(a)
                                    + y(a + 2 * h)
                                    + 5 * y(a + h)
                                    + 6 * y(a + 3 * h)
                                    + y(a + 4 * h)
                                    + 5 * y(a + 5 * h)
                                    + y(a + 6 * h)));
      
        // Return the final sum
        return sum;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        // lower limit and upper limit
        float a = 0, b = 6;
          
        // Function Call
        float num=WeedleRule(a, b);
        System.out.format("f(x) = "+"%.6f", num);
          
    }
}
  
// This code is contributed by AbhiThakur


Python3
# Python3 program to Implement Weedle's Rule
  
# A sample function f(x) = 1/(1+x^2)
def y(x):
    num = 1;
    denom = float(1.0 + x * x);
  
    return num / denom;
  
# Function to find the integral value
# of f(x) with step size h, with
# intial lower limit and upper limit
# a and b
def WeedleRule(a, b):
      
    # Find step size h
    h = (b - a) / 6;
      
    # To store the final sum
    sum = 0;
      
    # Find sum using Weedle's Formula
    sum = sum + (((3 * h) / 10) * (y(a)
            + y(a + 2 * h)
            + 5 * y(a + h)
            + 6 * y(a + 3 * h)
            + y(a + 4 * h)
            + 5 * y(a + 5 * h)
            + y(a + 6 * h)));
  
    # Return the final sum
    return sum;
  
# Driver Code
if __name__ == '__main__':
      
    # lower limit and upper limit
    a = 0;
    b = 6;
  
    # Function Call
    num = WeedleRule(a, b);
    print("f(x) = {0:.6f}".format(num));
  
# This code is contributed by sapnasingh4991


C#
// C# program to Implement Weedle's Rule
using System;
class GFG{
  
    // A sample function f(x) = 1/(1+x^2)
    static float y(float x)
    {
        float num = 1;
        float denom = (float)1.0 + x * x;
      
        return num / denom;
    }
      
    // Function to find the integral value
    // of f(x) with step size h, with
    // intial lower limit and upper limit
    // a and b
    static float WeedleRule(float a, float b)
    {
        // Find step size h
        float h = (b - a) / 6;
      
        // To store the final sum
        float sum = 0;
      
        // Find sum using Weedle's Formula
        sum = sum + (((3 * h) / 10) * (y(a)
                                    + y(a + 2 * h)
                                    + 5 * y(a + h)
                                    + 6 * y(a + 3 * h)
                                    + y(a + 4 * h)
                                    + 5 * y(a + 5 * h)
                                    + y(a + 6 * h)));
      
        // Return the final sum
        return sum;
    }
      
    // Driver Code
    public static void Main()
    {
        // lower limit and upper limit
        float a = 0, b = 6;
          
        // Function Call
        float num=WeedleRule(a, b);
        Console.Write("f(x) = "+num);
          
    }
}
  
// This code is contributed by AbhiThakur


输出:
f(x) = 1.373448