Skip to content
Snippets Groups Projects
Unverified Commit 3006e6cc authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added zero

parent 4c4cb7a7
No related branches found
No related tags found
No related merge requests found
Pipeline #12406 passed
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment