Currently attempting to do a numerical integration using the Trap rule.

Original integral was

cos(x)/sqrt(x)

between 0 and infinity.

With some subsitution and what not, have got it to

cos(x^2), which obviously is a fresnel integral.

My code currently looks like this, but it will only complete 1 iteration. I cannot work out why it will only complete 1 loop and give a totally wrong answer.

Thanks in advance guys!

Code:

#include <stdio.h>
#include <math.h>
main()
{
int n,i;
double x1,xn,h,new,old,sum,y,x,accuracyreq,accuracy;
printf("Input Accuracy:");
scanf("%lf",&accuracyreq);
printf("\nInput Upper Limit Value (Larger is More Accurate):");
scanf("%lf",&xn);
printf("The following is the answer to the integral cos(x)/sqrt(x): ");
accuracy=0;
new=2;
sum=0;
x=x1;
old=0.5;
for(n=10000000;accuracy>accuracyreq;n++)
{
h=xn-x1/(n-1);
for(i=0;x<xn;i++)
{
y=2*cos(pow(x,2));
if(x==x1||x==xn){
y=y/2;
}
sum=sum+y;
x=x+h;
}
new=sum*h;
accuracy=new/old;
printf("Accuracy is %lf",accuracy);
}
printf("%lf\n",new);
printf("%d\n",i);
printf("%d\n",n);
int end;
scanf("%d",&end);
}