IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Finding area of regions: Trapizoid and Simpson method

3 posters

Go down

C Finding area of regions: Trapizoid and Simpson method

Post by bit0223-sujon Sat May 07, 2011 8:14 pm

Hey guys, I am giving the C code of Trapizoid rule and Simpson's 1/3 rule. By these two methods we can get the result of a definite integration, or, in other words, we can say- we can find the area of a region very easily. I hope this will be helpful to everyone.
Code:
#include<stdio.h>
#include<stdlib.h>
#define f(x) (x)*(x)
double trapizoid(double,double,double);
double simpsons(double,double,double);


int main(void){
    double a,b,n,m,I,T,h,x,diff;
    int i;
    double sum1,sum2=0,sum3=0;

    printf("Enter a,b & m::");
    scanf("%lf%lf%lf",&a,&b,&m);
    sum1=f(a)+f(b);
    n=2*m;
    I=simpsons(a,b,n);
    T=trapizoid(a,b,n);
    diff=fabs(T-I);

    printf("%Trapizoid=%lf\nSimpsons=%lf\n\ndifference=%18.10e",T,I,diff);



return 0;
}
double simpsons(double a, double b, double n){
    int i;
    double x,h,sum1,sum2=0,sum3=0;
    h=(b-a)/n;
    sum1=f(a)+f(b);
printf("in simps h=%lf\n",h);

for(i=1;i<=n/2-1;i++){
        x=a+2*i*h;
        sum2+=f(x);
    }

    for(i=1;i<=n/2;i++)
        {
            x=a+(2*i-1)*h;
            sum3+=f(x);


        }
return (h/3.0)*(sum1+2.0*sum2+4.0*sum3);
}

double trapizoid(double a,double b,double n){
int i;
double x,h,sum=0;
h=(b-a)/n;
printf("in trapizoid h=%lf\n",h);
    for(i=1;i<=n-1;i++){
        x=a+i*h;
        sum+=f(x);
    }
    sum=(h/2.0)*(f(a)+f(b)+2*sum);
return sum;
}

bit0223-sujon
Global Moderator-RC

Course(s) :
  • BIT

Posts : 98
Points : 218

Back to top Go down

C Re: Finding area of regions: Trapizoid and Simpson method

Post by BIT0220-Iftekhar Sat May 07, 2011 8:19 pm

Nice work,keep sharing your resources and knowledges here in fourm rep++
BIT0220-Iftekhar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

http://iftekharunplugged.blogspot.com

Back to top Go down

C Re: Finding area of regions: Trapizoid and Simpson method

Post by BIT0122-Amit Sun May 08, 2011 1:13 am

Good work kiddo Very Happy

You can also check this link out Very Happy

This was what we studied.

[You must be registered and logged in to see this link.]
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

C Re: Finding area of regions: Trapizoid and Simpson method

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum