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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

ios - Possible to get UIButton sizeThatFits to work? -