Last active
February 17, 2018 16:37
-
-
Save Dimanaux/ffef406f9d7b7ee346b3c0577801adad to your computer and use it in GitHub Desktop.
This file contains 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> | |
#include <math.h> | |
typedef double (*function)(double); | |
double func(double); | |
double integral(function, double, double); | |
const int SECTIONS_QUANTITY = 1e4; | |
// const double EPS = 1e-6; | |
int main(int argc, char const *argv[]) | |
{ | |
printf( | |
"computed integral sinxdx from 0 to 2Pi = %.6f\nand must be 0", | |
integral(&sin, 0.0, 3 * M_PI) | |
); | |
printf( | |
"computed integral polynome dx from 0 to 15 = %.6f\nand must be ...", | |
integral(&polynome, 0.0, 15.0) | |
); | |
return 0; | |
} | |
double integral(function f, double from, double to) | |
{ | |
const double STEP = (double) (to - from) / SECTIONS_QUANTITY; | |
double s = 0.0; | |
for (double x = from; x < to; x += STEP) | |
{ | |
s += f(x) * STEP; | |
} | |
return s; | |
} | |
double polynome(double x) | |
{ | |
return x * x - 13 * x + 36; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment