diff --git a/exemples/zero.c b/exemples/zero.c new file mode 100644 index 0000000000000000000000000000000000000000..34df9cdac3b23a78b4236e6b4472397e37df8b41 --- /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