Last active
December 26, 2017 04:11
-
-
Save pureexe/8a1cee4f1ba9fdb12bf7ae390dc60916 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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