C Program to implement Trapezoidal Rule



Sumit Kar, Numerical Methods, C, Timus Rak



/*
Trapezoidal Rule
Equation: x/(1+x) dx
lower limit=0 upper limit=1 number of interval=6
*/
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,h,x,y,s=0;
float x0,y0,xn,yn,r;
int i,n;
float f(float);
clrscr();
printf("\nEnter lower limit value:: ");
scanf("%f",&a);
printf("\nEnter upper limit value:: ");
scanf("%f",&b);
printf("\nEnter the number of intervals:: ");
scanf("%d",&n);
h=(b-a)/n;
x0=a;
y0=f(a);
yn=f(b);
x=x0+h;
for(i=1;i<=n-1;i++)
{
y=f(x);
s=s+y;
x=x+h;
}
r=(h/2)*(y0+yn+2*s);
printf("\nResult is:: %f",r);
getch();
}
float f(float x)
{
return x/(1+x);
}


0 Comments