Simpson Method in C
×


Simpson Method in C

33

Simpson's method, also known as Simpson's rule, is a numerical integration technique used to approximate the definite integral of a function over an interval.

 It provides a way to estimate the area under a curve by approximating the curve with simpler geometric shapes, such as parabolas.


Segmentation of Interval:

The interval [a,b] over which the function is to be integrated is divided into n equal segments.

The more segments used, the closer the approximation to the actual integral.


Approximation with Parabolas:

Simpson's rule approximates the function over each segment by a parabola that passes through three points: the segment's endpoints and its midpoint.

Two Main Rules 

1Simpson's 1/3 Rule:

It uses parabolas to approximate the function over the segments.

For each segment, two parabolas are used, and the weights are usually 1,4,2,4,…,1

Formula:


2Simpson's 3/8 Rule:

It extends Simpson's 1/3 rule by using three parabolas for each segment.

The weights for this rule are usually 1,3,3,2,3,…,1.

Formula:

Accuracy:

Simpson's rule provides a more accurate approximation of integrals compared to methods like the trapezoidal rule when the function being integrated is smooth (has continuous derivatives).


Applications:

Simpson's method is widely used in engineering, physics, and other scientific fields where numerical approximations of integrals are required.


Example:

// Program simpsons method in C
#include<stdio.h> 
#include<math.h>

// Define the function to integrate
double func(double x) {
    return sin(x);  // Example function: sin(x)
}

// Simpson's 1/3 rule implementation
double simpson13(double a, double b, int n) {
    double delta_x = (b - a) / n;
    double sum = func(a) + func(b);  // f(a) + f(b)
    
    for (int i = 1; i < n; i++) {
        double x = a + i * delta_x;
        if (i % 2 == 0) {
            sum += 2 * func(x);  // 2 * f(x) for even i
        } else {
            sum += 4 * func(x);  // 4 * f(x) for odd i
        }
    }
    
    return (delta_x / 3) * sum;
}

// Simpson's 3/8 rule implementation
double simpson38(double a, double b, int n) {
    double delta_x = (b - a) / n;
    double sum = func(a) + func(b);  // f(a) + f(b)
    
    for (int i = 1; i < n; i++) {
        double x = a + i * delta_x;
        if (i % 3 == 0) {
            sum += 2 * func(x);  // 2 * f(x) for every third i
        } else {
            sum += 3 * func(x);  // 3 * f(x) otherwise
        }
    }
    
    return (3 * delta_x / 8) * sum;
}

int main() {
    double a = 0.0, b = M_PI;  // Interval [0, pi]
    int n = 100;  // Number of segments
    
    double integral13 = simpson13(a, b, n);
    double integral38 = simpson38(a, b, n);
    
    printf("Approximate integral using Simpson's 1/3 rule: %lf\n", integral13);
    printf("Approximate integral using Simpson's 3/8 rule: %lf\n", integral38);
    
    return 0;
}
	

Output:

Approximate integral using Simpson's 1/3 rule: 2.000000
Approximate integral using Simpson's 3/8 rule: 1.999877

In this program:

We define the function func(x) as sin(x) for demonstration.

The simpson13() function computes the integral using Simpson's 1/3 rule.

The simpson38() function computes the integral using Simpson's 3/8 rule.

In the main() function, we integrate sin(x) over [0,π].

[0,π] with 100 segments and print both approximations.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments