Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,2 +1,3 @@ | |||
all: | all: | ||
gcc -g -O0 -Wall -Wextra -Wunused -pedantic -std=c99 -lm -o tp3-1 tp3-1.c | gcc -g -O2 -Wall -Wextra -Wunused -pedantic -std=c99 -lm -o tp3-1 tp3-1.c | ||
gcc -g -O2 -Wall -Wextra -Wunused -pedantic -std=c99 -lm -o tp3-2 tp3-2.c |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <math.h> | |||
|
|||
|
|||
double IntegrateTrapezoid(double (*f)(double), double start, double end, size_t num_intervals) { | |||
double sum = 0.0; | |||
double x0 = start; | |||
double h = (end - start) / num_intervals; | |||
|
|||
for (size_t i = 0; i < num_intervals; ++i) { | |||
double y1 = (*f)(x0); | |||
double y2 = (*f)(x0 + h); | |||
|
|||
sum += (y1 + y2) / 2 * h; | |||
x0 += h; | |||
} | |||
|
|||
return sum; | |||
} | |||
|
|||
|
|||
double IntegrateSimpson1_3(double (*f)(double), double start, double end) { | |||
double h = (end - start) / 2; | |||
|
|||
return h/3 * ((*f)(start) + 4*(*f)(start + h) + (*f)(end)); | |||
} | |||
|
|||
|
|||
double IntegrateSimpson3_8(double (*f)(double), double start, double end) { | |||
double h = (end - start) / 3; | |||
|
|||
return 3.0/8.0 * h * ((*f)(start) + 3*(*f)(start + h) + | |||
3*(*f)(start + 2*h) + (*f)(end)); | |||
} | |||
|
|||
|
|||
double f(double x) { | |||
return 1.0 / (x*x + x + 1); | |||
} | |||
|
|||
int main(void) { | |||
printf("%g\n", IntegrateTrapezoid(&f, 0, 6, 100)); | |||
printf("%g\n", IntegrateSimpson1_3(&f, 0, 6)); | |||
printf("%g\n", IntegrateSimpson3_8(&f, 0, 6)); | |||
return EXIT_SUCCESS; | |||
} |