Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Last active February 17, 2018 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimanaux/ffef406f9d7b7ee346b3c0577801adad to your computer and use it in GitHub Desktop.
Save Dimanaux/ffef406f9d7b7ee346b3c0577801adad to your computer and use it in GitHub Desktop.
#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