fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double newton_method_quartic(double x0, int max_iter, double tol) {
  5. double x = x0;
  6. for (int i = 0; i < max_iter; i++) {
  7. double fx = x * x * x * x - 7;
  8. double dfx = 4 * x * x * x;
  9. double x_next = x - fx / dfx;
  10. if (fabs(x_next - x) < tol) {
  11. return x_next;
  12. }
  13. x = x_next;
  14. }
  15. return x;
  16. }
  17.  
  18. int main() {
  19. double initial_guess = 2.0; // 初期値
  20. int max_iterations = 100; // 最大反復回数
  21. double tolerance = 1e-7; // 許容誤差
  22.  
  23. double root = newton_method_quartic(initial_guess, max_iterations, tolerance);
  24. printf("∜7 ≈ %.10f\n", root);
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
∜7 ≈ 1.6265765617