Hot Posts

3/recent/ticker-posts

Weddle Rule and it's C++ Code


Wedge's Rule (also known as Weddle's Rule) is a numerical integration method used to approximate the definite integral of a function over a given interval. This method extends Simpson's 3/8 rule by combining Simpson's rule with the trapezoidal rule to achieve higher accuracy.


Basic Principle:


Wedge's Rule combines the Simpson's 3/8 rule with additional corrections using the trapezoidal rule. It subdivides the interval into equally spaced segments, applying Simpson's 3/8 rule whenever possible. However, if the number of segments isn't a multiple of 6, it uses the trapezoidal rule for the remaining segments.


Steps in Wedge's Rule:


1. Divide Interval into Segments:

   - Divide the interval ([a, b]) into (n) equally spaced segments.

   - Ensure (n) is a multiple of 6 to apply Wedge's Rule.


2. Calculate Approximation:

   - Apply Simpson's 3/8 rule for groups of six segments.

   - For any remaining segments, apply the trapezoidal rule.

   - Sum up the areas obtained from Simpson's 3/8 rule and the trapezoidal rule to obtain the approximate integral value.


Advantages and Limitations:


- Advantages:

  - Higher accuracy: Offers higher accuracy than Simpson's 3/8 rule for integrals over a broader range of functions.

  - Flexibility: Can handle integrals with a range of segments, not just multiples of 6.


- Limitations:

  - Requirement of specific segment counts: Wedge's Rule requires the segment count to be a multiple of 6, limiting its application for cases with a different number of segments.


C++ Code for Wedge's Rule:


```cpp

#include <iostream>

#include <cmath>

using namespace std;


Function to integrate (Example function: x^4)

double function(double x) {

    return pow(x, 4);

}

Wedge's rule for numerical integration

double wedgesRule(double a, double b, int n) {

    double h = (b - a) / n;

    double sum = function(a) + function(b);


    for (int i = 1; i < n; ++i) {

        double x = a + i * h;

        if (i % 6 == 0)

            sum += 2 * function(x);

        else if (i % 3 == 0)

            sum += 3 * function(x);

        else

            sum += 3 * function(x);

    }


    return (3 * h / 10) * sum;

}


int main() {

    double a = 0.0; // Lower limit of integration

    double b = 2.0; // Upper limit of integration

    int n = 12; // Number of segments (should be a multiple of 6)


    double integral = wedgesRule(a, b, n);


    cout << "Approximate integral using Wedge's Rule: " << integral << endl;


    return 0;

}


Explanation:


- `function()` Function:

  - Represents the function to be integrated. Modify this function to integrate a different function.


- `wedgesRule()` Function:

  - Implements Wedge's Rule for numerical integration.

  - Divides the interval ([a, b]) into (n) segments and calculates the approximate integral using Wedge's Rule logic.


- `main()` Function:

  - Sets the interval ([a, b]) and the number of segments (n) for the integration.

  - Calls the `wedgesRule()` function to approximate the integral and displays the result.


 Example:


The provided code demonstrates Wedge's Rule to approximate the integral of the function (f(x) = x^4) from 0 to 2 using 12 segments. It calculates and displays the approximate integral value obtained using Wedge's Rule. Adjust the function and interval as needed for different integrals.


Wedge's Rule offers increased accuracy over Simpson's 3/8 rule for a broader range of functions, especially when the number of segments isn't a multiple of 6. Adjusting the number of segments can improve accuracy for more complex functions.


Post a Comment

0 Comments