Skip to content

Instantly share code, notes, and snippets.

@pureexe
Last active December 26, 2017 04:11
Show Gist options
  • Select an option

  • Save pureexe/8a1cee4f1ba9fdb12bf7ae390dc60916 to your computer and use it in GitHub Desktop.

Select an option

Save pureexe/8a1cee4f1ba9fdb12bf7ae390dc60916 to your computer and use it in GitHub Desktop.
#include<stdio.h>
double func(double x){
return x*x;
}
double trapzoid(double (*f)(double),double a,double h,double b){
int i,n;
double sum = 0;
n = (b-a)/h;
sum += f(a)/2;
for(i = 1;i < n;i++){
sum += f(a+i*h);
}
sum += f(b) / 2;
return h*sum;
}
int main(){
double result = trapzoid(func,0.5,0.01,1);
double exact = (1.0/3) - (0.125/3);
printf("Integrate x^2 from 0.5 to 1\nTrapzoid = %.6f\nExact = %.6f",result,exact);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment