fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double gauss(double (*f)(), double x1, double x2){
  5. double xs[] = {-0.906179845938664, -0.538469310105683, .0, 0.538469310105683, 0.906179845938664};
  6. double ws[] = {0.236926885056189, 0.478628670499366, 0.568888888888889, 0.478628670499366, 0.236926885056189};
  7. int n = sizeof(xs)/sizeof(xs[0]);
  8. double res = 0;
  9. int i;
  10. for(i=0; i<n; ++i){
  11. res += ws[i] * (*f)((x2+x1)/2. + (x2-x1)/2.*xs[i]);
  12. }
  13. res *= (x2-x1)/2.;
  14. return res;
  15. }
  16.  
  17. double f(double x){
  18. return 1/(1+x*x);
  19. }
  20. int main(void){
  21. double x1 = 0.33, x2 = x1+1;
  22. printf("gauss: %.10f\n", gauss(f, x1, x2));
  23. printf("artan: %.10f\n", atan(x2) - atan(x1));
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
gauss: 0.6073457775
artan: 0.6073457351