Hot Posts

3/recent/ticker-posts

Simpson 3/8th Rule and it's C++ Code


The Simpson's 3/8 rule is a numerical integration method used to approximate the definite integral of a function. It's an extension of the Simpson's 1/3 rule, allowing for integration over a more extensive range of functions by approximating the curve with cubic approximations.


Basic Principle:


Similar to the Simpson's 1/3 rule, the 3/8 rule divides the interval into equally spaced segments. However, it fits cubic polynomials through every three consecutive segments instead of quadratic polynomials.


Steps in Simpson's 3/8 Rule:


1. Divide Interval into Segments:

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

   - Ensure (n) is a multiple of 3 to apply Simpson's 3/8 rule.


2. Calculate Approximation:

   - Approximate the integral by summing the areas under cubic polynomials formed by every three consecutive segments.

   - Calculate the area under each group of three segments using Simpson's 3/8 rule formula: ( text{Area} = frac{3h}{8} times [f(x_i) + 3f(x_{i+1}) + 3f(x_{i+2}) + f(x_{i+3})] ).

   - Sum up these areas to obtain the approximate integral value.


Advantages and Limitations:


- Advantages:

  - Higher accuracy: Offers higher accuracy for a broader range of functions compared to the Simpson's 1/3 rule.

  - Efficiency: Requires fewer function evaluations than some other numerical integration techniques.


- Limitations:

  - Requirement of a multiple of 3 segments: Simpson's 3/8 rule demands a multiple of 3 segments for application, limiting its use for cases with a different number of segments.


 C++ Code for Simpson's 3/8 Rule:


```cpp

#include <iostream>

#include <cmath>

using namespace std;


Function to integrate (Example function: x^3)

double function(double x) {

    return x * x * x;

}


Simpson's 3/8 rule for numerical integration

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

    double h = (b - a) / n;

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


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

        double x = a + i * h;

        sum += 3 * function(x);

    }


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

        double x = a + i * h;

        sum += 3 * function(x);

    }


    for (int i = 3; i < n - 2; i += 3) {

        double x = a + i * h;

        sum += 2 * function(x);

    }


    return (3 * h / 8) * sum;

}


int main() {

    double a = 0.0; // Lower limit of integration

    double b = 2.0; // Upper limit of integration

    int n = 9; // Number of segments (should be a multiple of 3)


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


    cout << "Approximate integral using Simpson's 3/8 Rule: " << integral << endl;


    return 0;

}


Explanation:


- `function()` Function:

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


- `simpsons3_8Rule()` Function:

  - Implements Simpson's 3/8 rule for numerical integration.

  - Divides the interval ([a, b]) into (n) segments and calculates the approximate integral using the Simpson's 3/8 rule formula.


- `main()` Function:

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

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


Example:


The provided code demonstrates Simpson's 3/8 rule to approximate the integral of the function (f(x) = x^3) from 0 to 2 using 9 segments. It calculates and displays the approximate integral value obtained using Simpson's 3/8 rule. Adjust the function and interval as needed for different integrals.


Simpson's 3/8 rule is a more advanced numerical integration method, offering higher accuracy for a broader range of functions compared to simpler methods. Adjusting the number of segments can improve accuracy for more complex functions.


Post a Comment

0 Comments