Simpson's 1/3rd Method solved in C language.

SIMPSON'S (1/3) RULE TO EVALUATE THE INTEGRATION OF FUNCTION.

Here in this blog we will see how to code the problem of integration in C Language. 

Approach:-

First of all let's us explore the method of Simpson's rule. We evaluate the result of integration on solving the equation of parabolas/quadratic polynomial passing through the points (x0,y0) and (x1,y1) & (x2,y2) as so on............

Popularity:-
                       
It is popular because it integrates exactly cubic function or equivalently, gives the area under a segment of cubic curve.Unlike Trapezoidal rule integrates exactly a liner function only.


Formula:-

a[f(x)dx]b=h/3[y0+yn+4*(y1+y3+y5+.......+yn-1)+2*(y2+y4+y6+............+yn-2)]

where a=lower limit of integration
              b=upper limit of integration
              h=height of interval

              h=(b-a)/n;
              where n is no. of sub interval of the function.


C program to evaluate the result of the integration of given function f(x).

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
void main()
{
         float x[100],fx[100],l,u,h,res=0;
         int n,i;
         clrscr();
         
         printf("\n Enter the lower limit of function: ");
         scanf("%f",&l);
         printf("\n Enter the upper limit of function: ");
         scanf("%f",&u);
         printf("\n Enter the nodal value or sub-interval of function: ");
         scanf("%d",&n);
         
         h=(u-l)/n;
         printf("Step-Size H= %.2f",h);
         for(i=0;i<=n;i++)
         {
                       x[i]=l+(i*h);
                       fx[i]=f(x[i]);
         }
         printf("\n X: ");
         for(i=0;i<=n;i++)
                    {
                             printf("\n %.2f\t",x[i]);
                    }
        printf("\n F(X:) ");
        for(i=0;i<=n;i++)
                    {
                              printf("\n %.2f\t",fx[i]);
                    }

        for(i=0;i<=n;i++)
                  {
                              if(i==0||i==n) 
                                   {
                                                res=res+fx[i];
                                   }
                             else if(i%2!=0)
                                   {
                                                res=res+(4*fx[i]);
                                   }
                             else
                                               res=res+(2*fx[i]);
                  }
        res=res*(h/3);
        printf("\n THE REQUIRED VALUE OF INTEGRATION IS : %.2f",res);
        getch();
}
float f(float x)
{
             return 2+sin(2*pow(x,0.5));          //you can change the function                                                                                                   //according to your need
}

:)   :)  :3

The above code is tested under CODE BLOCK IDE with all possible test cases.
Hope you like this blog. Don't hesitate to shower your love. Subscribe and follow the blog for more solution of the coding problems.
Thank You!!!!!!!!


Comments

Popular posts from this blog

Chatting Application(Chit-Chat Messenger)