C code using Simpson’s 3/8 rule or Simpson’s three by eight rule

Problem: Here we have to find integration for the (1/1+x*x)dx with lower limit =0 to upper limit = 6

Algorithm:

Step 1: input a,b,number of interval n

Step 2:h=(b-a)/n

Step 3:sum=f(a)+f(b)

Step 4:If n is odd

Then , sum=sum+2*y(a+i*h)

Step 5: else, When   n I s even
Then, Sum = sum+3*y(a+i*h)

Step 6:Display output=sum *3* h/8

 

Code:

#include<stdio.h>
float y(float x){
    return 1/(1+x*x); //function of which integration is to be calculated
}
int main(){
    float a,b,h,sum;
    int i,n,j;
    sum=0;
    printf("Enter a=x0(lower limit), b=xn(upper limit), number of subintervals: ");
    scanf("%f%f%d",&a,&b,&n);
    h = (b-a)/n;
    sum = y(a)+y(b);
    for(i=1;i<n;i++)
    {
        if(i%3==0){
            sum=sum+2*y(a+i*h);
        }
        else{
            sum=sum+3*y(a+i*h);
        }
        }
    printf("Value of integral is %f\n", (3*h/8)*sum);
    }

Output:

2014-11-27 02_44_27-

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.