# Thread: Integrating function using trapezoidal rule

1. ## Integrating function using trapezoidal rule

Hi guys, can some one please help me with this program. I need to be able to integrade a function but i seem to be having some trouble getting consistant result using the rule. more specifically im not getting consistant result for the sub intervals i am implementing. Im thinking that my for loop needs to be modified to make this work, but i dont know how... that is.. the intergration of y=x from 0 to 10 is 50, but as you can see from the output i only get that for the first interval mainly 10.

Code:
#include<stdio.h>
#include<math.h>
#define F(X) X
int main(){
int i,k1,k2;
double a,b,h1,h2,sum=0,sum2=0,U1,U2,y;
printf("Enter the lower limit:");
scanf ("%lf",&a);
printf("Enter the upper limit:");
scanf("%lf",&b);

k1=10;
k2=20;
printf("%5s %5s %12s %12s %12s\n","k1","k2","U1","U2","y");
while(k1<=1250 && k2<=2500){
h1=(b-a)/k1;
for (i=1;i<=k1-1;i++){sum=sum+F(a+(i*h1)); }
U1=(h1/2)*(F(a)+2*sum+F(b));

h2=(b-a)/k2;
for (i=1;i<=k2-1;i++){sum2=sum2+F(a+(i*h2)); }
U2=(h2/2)*(F(a)+2*sum2+F(b));

y=( (2*2*U2)/((2*2)-1) )-( U1/((2*2)-1) );

printf("%5d %5d %12.6lf %12.6lf %12.6lf\n",k1,k2,U1,U2,y);

k1+=(5*k1)-k1;
k2+=(5*k2)-k2;

}

return 0;
}