Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added integration number

  • Loading branch information...
commit 1228c7f72b61ded5b3178ed818765aad903c7606 1 parent 5141c7b
@gnuvince authored
Showing with 49 additions and 1 deletion.
  1. +2 −1  Makefile
  2. +47 −0 tp3-2.c
View
3  Makefile
@@ -1,2 +1,3 @@
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
View
47 tp3-2.c
@@ -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;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.