32 lines
831 B
C
32 lines
831 B
C
#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;
|
|
}
|