Finding Integreand 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 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.022897

Approach:
The integration of any function f(x) using Weedle’s Formula is given by:



 \int_{a}^{b} f(x) dx =  \frac{3}{10} h (f_1 + 5f_2 + f_3 + 6f_4 +  f_5 + 5f_6 + f_7)
where,
h =  \frac{(b-a)}{6}
 f_i = f(x_i) and i = [0, 6]
 x_1 = a
 x_2 = x_1 + h
 x_3 = x_1 + 2h
 x_4 = x_1 + 3h
 x_5 = x_1 + 4h
 x_6 = x_1 + 5h

Below are the steps:

  1. Find the value of h from the above formula i.e., h =  \frac{(b-a)}{6} .
  2. Find the value from  x_1 to  x_7 and calculate the value from  f(x_1) to  f(x_7)
  3. Substitute the above values in the Weedle’s Formula to find the integral value.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to Implement Weedle's Rule
#include <bits/stdc++.h>
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

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to Implement Weedle's Rule
#include <math.h>
#include <stdio.h>
  
// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

f(x) = 1.373448

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.