How to implement Trapezoidal Integration with Infinite Limits in C? -
i trying create c program integrate sin(x)/sqrt(x) between 0 , infinity. using trapezium rule cutting off end points function tends infinity.
however total returned high , not sure why. here's code:
#include<math.h> #include<stdio.h> double func(double u) { double 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 fabs
in line f=fabs(func(u));
.
and should use int main(void)
, return 0;
@ end instead of void main()
.
Comments
Post a Comment