Numerical Methods: Integration of given function using Simpson’s 1/3 rule in C

Source Code:

//integration of given function using Simpson's 3/8 rule
#include<stdio.h>
float y(float x){
    return 1/(1+x*x); //function of which integration is to be calculated
}
int main(){
    float x0,xn,h,s;
    int i,n,j,flag;
    printf("Enter x0, xn, no. of subintervals: ");
    scanf("%f%f%d",&x0,&xn,&n);
    h = (xn-x0)/n;
    s = y(x0)+y(xn);
    for(i = 1; i<=n-1;i++){
        for(j=1;j<=n-1;j++){
            if(i==3*j){
                flag = 1;
                break;
            }
            else
                flag = 0;
        }
        if(flag==0)
            s += 3*y(x0+i*h);
        else
            s += 2*y(x0+i*h);
    }
    printf("Value of integral is %6.4f\n",(3*h/8)*s);
    return 0;
}
SHARE Numerical Methods: Integration of given function using Simpson’s 1/3 rule in C

You may also like...

5 Responses

  1. Unknown says:

    this program is not correct — for a correct solution use this link — http://www.codewithc.com/c-program-for-simpson-1-3-rule/

  2. Anonymous says:

    this is correct program

  3. Anonymous says:

    Instead of return 1/1+(x*x),it should be written as return f because every function is different for different problems

  4. Anonymous says:

    Can you please tell me why we have to use i=3 in the for loop in initilization part with increment i=i+2.

  5. Primo Raj says:

    How to plot corresponding area of the integral in Simpson 1/3 rule with c program?

Leave a Reply

Your email address will not be published.

Share