From 3006e6cca44b06779068099eead7cac16e80ca71 Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@hesge.ch> Date: Fri, 2 Oct 2020 11:50:20 +0200 Subject: [PATCH] added zero --- exemples/zero.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 exemples/zero.c diff --git a/exemples/zero.c b/exemples/zero.c new file mode 100644 index 0000000..34df9cd --- /dev/null +++ b/exemples/zero.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <math.h> +#include <stdlib.h> + +double f(double x) { + return x * x - 25.0; +} + +double next_x(double x1, double x2, double (*foo)(double)) { + return x1 - (x2-x1)/(foo(x2)-foo(x1))*foo(x1); +} + +int main() { + const int max_iter = 1000; + double x1 = rand(); + double x2 = rand(); + printf("x1 = %f, x2 = %f\n", x1, x2); + for (int i = 0; i < max_iter; ++i) { + double x3 = next_x(x1, x2, f); + x1 = x2; + x2 = x3; + printf("x3 = %f, f(%f) = %f, at iter = %d\n", x3, x3, f(x3), i); + if (fabs(f(x3)) < 0.00001 ) { + return EXIT_SUCCESS; + } + } + return EXIT_FAILURE; +} \ No newline at end of file -- GitLab