I am trying to create a c program to integrate sin(x)/sqrt(x) between 0 and Infinity. I am using the trapezium rule by cutting off the end points as the function tends to infinity.
However the total returned is too high and I am not sure why. Here's the code:
#include<math.h>
#include<stdio.h>
double func(double u)
{ double a;
a = ((sin(u))/(sqrt(u)));
return a;}
void main()
{
int i, N;
double sum, u, a, b, h, Fa, Fb, F;
printf("Enter value of N\n");
scanf("%d" ,&N);
a=0.01;
b=1000;
h=(b-a)/(N-1);
sum=0;
F=func(a);
u=a;
for(i=0; i<N; i++)
{
sum=sum+F;
u=u+h;
F=fabs(func(u));
}
Fa=func(a);
Fb=func(b);
sum=sum-(0.5*Fa)-(0.5*Fb);
sum=sum*h;
printf("I: %lf\n", sum);
}
Any thoughts?
working example: http://ideone.com/Xibrov
just remove the fabs
in the line F=fabs(func(u));
.
and you should use int main(void)
and return 0;
at the end instead of void main()
.