Hot Posts

3/recent/ticker-posts

Simpson 1/3rd Rule and it's C++ Code


The Simpson's 1/3 rule is a numerical integration method used to approximate the definite integral of a function. It's based on approximating the curve with quadratic approximations, resulting in higher accuracy compared to simpler methods like the trapezoidal rule.


Basic Principle:


Given a function ( f(x) ) and an interval ([a, b]), the Simpson's 1/3 rule divides the interval into equally spaced segments and fits a quadratic polynomial through every two consecutive segments. It then approximates the integral by summing the areas under these quadratic segments.


Steps in Simpson's 1/3 Rule:


1. Divide Interval into Segments:

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

   - Ensure (n) is even to apply Simpson's 1/3 rule.


2. Calculate Approximation:

   - Approximate the integral by summing the areas under quadratic polynomials formed by every two consecutive segments.

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

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


Advantages and Limitations:


- Advantages:

  - Higher accuracy: Offers higher accuracy compared to simpler methods like the trapezoidal rule.

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


- Limitations:

  - Requirement of an even number of segments: Simpson's 1/3 rule demands an even number of segments for application, limiting its use for cases with an odd number of segments.


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


```cpp

#include <iostream>

#include <cmath>

using namespace std;


Function to integrate (Example function: x^2)

double function(double x) {

    return x * x;

}


Simpson's 1/3 rule for numerical integration

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

    double h = (b - a) / n;

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


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

        double x = a + i * h;

        sum += 4 * function(x);

    }


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

        double x = a + i * h;

        sum += 2 * function(x);

    }


    return (h / 3) * sum;

}


int main() {

    double a = 0.0; // Lower limit of integration

    double b = 2.0; // Upper limit of integration

    int n = 10; // Number of segments (should be even)


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


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


    return 0;

}


Explanation:


- `function()` Function:

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


- `simpsonsRule()` Function:

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

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


- `main()` Function:

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

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


Example:


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


Simpson's 1/3 rule is a powerful numerical method for approximating integrals, especially for smooth functions, offering increased accuracy compared to simpler methods. Adjusting the number of segments can improve accuracy for more complex functions.


Post a Comment

0 Comments