analistica/ex-5/manual.c

32 lines
831 B
C
Raw Normal View History

2020-03-06 02:24:32 +01:00
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
/* This program evaluates the integral of exp(x) in [0;1] through the
* trapezoidal rule.
* The integration step is given in input as d in step = 1e-d.
*
*/
int main(int argc, char** argv){
double left = 0; // left limit
double point = left; // points at the current position
double right = 1; // right limit
double step = pow(10, -atoi(argv[1])); // step of integration
double sum = 0;
// Integral evaluation: stops when point goes over the right edge of the
// range.
do {
sum += step * 0.5 * (exp(point) + exp(point + step));
point += step;
} while (point < right);
// Print out the result
printf("∫exp(x)dx in [0,1]:\t%.7f\n", sum);
return EXIT_SUCCESS;
}