fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. float f(float x) {
  5. return 3*x - 14 + exp(x) - exp(-x);
  6. }
  7.  
  8. float secant_method(float (*f)(float), float a, float b, float tol, int max_iter) {
  9. float x0 = a, x1 = b, x_new;
  10. for (int i = 0; i < max_iter; i++) {
  11. float f_x0 = f(x0);
  12. float f_x1 = f(x1);
  13. if (fabs(f_x1) < tol) return x1;
  14. x_new = x1 - f_x1 * (x1 - x0) / (f_x1 - f_x0);
  15. if (fabs(x_new - x1) < tol) return x_new;
  16. x0 = x1;
  17. x1 = x_new;
  18. }
  19. return x1;
  20. }
  21.  
  22. int main() {
  23. float a = 1.0f, b = 3.0f, tol = 1e-6f;
  24. int max_iter = 100;
  25. float root = secant_method(f, a, b, tol, max_iter);
  26. printf("\nответ: %6.6f\n", root);
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
ответ: 2.069218