Numerical Integration (Simpsons 1/3 Rule) Implementation in C Programming

Introduction

Numerical integration is a fundamental technique used to approximate the definite integral of a function when an exact solution is not feasible or available. Simpson's 1/3 rule is a numerical integration method that provides a reasonably accurate estimation by dividing the interval into smaller segments and using quadratic approximations. In this article, we will explore the implementation of Simpson's 1/3 rule in the C programming language.
Understanding Simpson's 1/3 Rule.

Simpson's 1/3 rule is based on approximating the curve of a function using quadratic polynomials. The integral of a function f(x) over an interval [a, b] is approximated by dividing the interval into an even number of sub-intervals and applying Simpson's 1/3 formula:

∫(a to b) f(x) dx ≈ h/3 * [f(x0) + 4 * f(x1) + 2 * f(x2) + ... + 4 * f(xN-1) + f(xN)] alert-info

Where:
h = (b - a) / N is the width of each sub-interval.
N is the number of sub-intervals, which should be even.
The equally spaced points within the interval are x0, x1, x2, ..., xN.

We must follow these steps to implement Simpson's 1/3 rule in C programming.

  • Define the function f(x) for which you want to calculate the integral.
  • Determine the interval [a, b] and the number of sub-intervals (N).
  • Calculate the width of each sub-interval (h).
  • Initialize variables to store the summation of function values.
  • Use a loop to calculate the summation of function values based on Simpson's 1/3 formula.
  • Finally, calculate the approximation of the integral using the formula.

Implementation of Simpson's 1/3 rule in C programming

The code for Simpson's 1/3 rule implementation in C is given below:

#include<stdio.h>
#include<conio.h>
float f(float x)
{
   return(1/(1+x));
}
void main()
{
   int i,n;
   float x0,xn,h,y[20],so,se,ans,x[20];
   printf("\n Enter values of x0,xn,h: ");
   scanf("%f%f%f",&x0,&xn,&h);
   n=(xn-x0)/h;
   if(n%2==1)
   {
       n=n+1;
   }
   h=(xn-x0)/n;
   printf("\n Refined value of n and h are:%d %f\n",n,h);
   printf("\n Y values: \n");
   for(i=0; i<=n; i++)
   {
       x[i]=x0+i*h;
       y[i]=f(x[i]);
       printf("\n %f\n",y[i]);
   }
   so=0;
   se=0;
   for(i=1; i<n; i++)
   {
       if(i%2==1)
       {
           so=so+y[i];
       }
       else
       {
           se=se+y[i];
       }
   }
   ans=h/3*(y[0]+y[n]+4*so+2*se);
   printf("\n Final integration is %f",ans);
   getch();
}code-box

Input & Output

Numerical Integration (Simpsons 1/3 Rule) implementation in C programming
Simpsons 1/3 Rule implementation in C programming

Applications of Simpsons 1/3 Rule

Numerical Integration: Simpson's 1/3 rule primarily approximates definite integrals. It provides a more accurate estimate than straightforward methods like the trapezoidal rule. By dividing the interval into smaller sub-intervals and using quadratic approximations, Simpson's 1/3 rule improves the accuracy of the numerical integration.

Physics and Engineering: Simpson's 1/3 rule is widely applied in physics and engineering to solve problems involving area calculations, such as finding the centroid of a complex shape or calculating the moment of inertia. It enables engineers and physicists to approximate the required integrals and obtain helpful information for analysis and design.

Numerical Analysis: Simpson's 1/3 rule is crucial in numerical analysis algorithms. It is often utilized as a building block in more advanced numerical integration techniques, such as Simpson's 3/8 rule and adaptive quadrature methods. These advanced methods aim to achieve higher accuracy by dynamically adjusting the sub-intervals based on the behavior of the integrand.

Scientific Simulations: In computational simulations and modeling, Simpson's 1/3 rule can be employed to approximate integrals arising from differential equations. It is commonly used in numerical methods like finite element analysis, finite difference methods, and numerical solutions of differential equations. These simulations are vital in various fields, including physics, engineering, economics, and environmental science.

Signal Processing: Simpson's 1/3 rule is also applied in signal processing for signal reconstruction or interpolation tasks. It helps to estimate the values between discrete samples by fitting a quadratic curve over a specific interval.

Probability and Statistics: Simpson's 1/3 rule is occasionally utilized in probability and statistics for calculating probabilities and moments of continuous random variables. Approximating the integrals involved in probability density and cumulative distribution functions facilitates analyzing random processes and statistical data.

Conclusion

Simpson's 1/3 rule provides an effective method for approximating definite integrals numerically. This method offers reasonable accuracy by dividing the interval into sub-intervals and using quadratic approximations. In this article, we presented an implementation of Simpson's 1/3 rule in the C programming language, allowing you to calculate the approximation of the integral for a given function and interval. You can further enhance this implementation by adding.

0/Post a Comment/Comments