C code using Simpson’s 1/3 rule or Simpson’s one third 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)+4*f(a+h)

Step 4:sum=sum+4*f(a+i*h)+2*f(a+(i-1)*h)

Step 5:Display output=sum * h/3

Code:

#include<stdio.h>
float y(float x){
    return 1/(1+x*x);
}
int main(){
    float a,b,h,sum;
    int i,n;
    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)+4*y(a+h);
    for(i = 3; i<=n-1; i=i+2){
        sum=sum+4*y(a+i*h) + 2*y(a+(i-1)*h);
    }
    printf("\n Value of integral is %f\n",(h/3)*sum);
    return 0;
}

 Output:

2014-11-26 17_14_31-

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

Leave a Reply

Your email address will not be published.