fork(2) download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define EPS 1e-5
  5.  
  6. double f(double x) {
  7. return x * x - 1.0 - sin(x);
  8. }
  9.  
  10. // Logic for Bisection Method
  11. void solve_bisection(double a, double b, char* label) {
  12. double mid;
  13. int iter = 0;
  14. printf("\n--- Bisection Method for %s ---\n", label);
  15. printf("Iter | a b mid f(mid)\n");
  16.  
  17. do {
  18. iter++;
  19. mid = (a + b) / 2.0;
  20. printf("%4d | %10.6f %10.6f %10.6f %10.6f\n", iter, a, b, mid, f(mid));
  21.  
  22. if (f(a) * f(mid) < 0) b = mid;
  23. else a = mid;
  24.  
  25. } while (fabs(f(mid)) >= EPS);
  26.  
  27. printf("Final Result for %s: x = %.6f\n", label, mid);
  28. }
  29.  
  30. // Logic for False Position Method
  31. void solve_false_position(double a, double b, char* label) {
  32. double mid;
  33. int iter = 0;
  34. printf("\n--- False Position Method for %s ---\n", label);
  35. printf("Iter | a b mid f(mid)\n");
  36.  
  37. do {
  38. iter++;
  39. // The specific formula for はさみうち法
  40. mid = (a * f(b) - b * f(a)) / (f(b) - f(a));
  41. printf("%4d | %10.6f %10.6f %10.6f %10.6f\n", iter, a, b, mid, f(mid));
  42.  
  43. if (f(a) * f(mid) < 0) b = mid;
  44. else a = mid;
  45.  
  46. } while (fabs(f(mid)) >= EPS);
  47.  
  48. printf("Final Result for %s: x = %.6f\n", label, mid);
  49. }
  50.  
  51. int main() {
  52. // Solve for both roots using Bisection
  53. solve_bisection(-1.0, 0.0, "Root 1");
  54. solve_bisection(1.0, 2.0, "Root 2");
  55.  
  56. // Solve for both roots using False Position
  57. solve_false_position(-1.0, 0.0, "Root 1");
  58. solve_false_position(1.0, 2.0, "Root 2");
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
--- Bisection Method for Root 1 ---
Iter | a          b          mid        f(mid)
   1 |  -1.000000   0.000000  -0.500000  -0.270574
   2 |  -1.000000  -0.500000  -0.750000   0.244139
   3 |  -0.750000  -0.500000  -0.625000  -0.024278
   4 |  -0.750000  -0.625000  -0.687500   0.107263
   5 |  -0.687500  -0.625000  -0.656250   0.040814
   6 |  -0.656250  -0.625000  -0.640625   0.008097
   7 |  -0.640625  -0.625000  -0.632812  -0.008133
   8 |  -0.640625  -0.632812  -0.636719  -0.000029
   9 |  -0.640625  -0.636719  -0.638672   0.004031
  10 |  -0.638672  -0.636719  -0.637695   0.002001
  11 |  -0.637695  -0.636719  -0.637207   0.000986
  12 |  -0.637207  -0.636719  -0.636963   0.000478
  13 |  -0.636963  -0.636719  -0.636841   0.000225
  14 |  -0.636841  -0.636719  -0.636780   0.000098
  15 |  -0.636780  -0.636719  -0.636749   0.000035
  16 |  -0.636749  -0.636719  -0.636734   0.000003
Final Result for Root 1: x = -0.636734

--- Bisection Method for Root 2 ---
Iter | a          b          mid        f(mid)
   1 |   1.000000   2.000000   1.500000   0.252505
   2 |   1.000000   1.500000   1.250000  -0.386485
   3 |   1.250000   1.500000   1.375000  -0.090268
   4 |   1.375000   1.500000   1.437500   0.075277
   5 |   1.375000   1.437500   1.406250  -0.008954
   6 |   1.406250   1.437500   1.421875   0.032797
   7 |   1.406250   1.421875   1.414062   0.011830
   8 |   1.406250   1.414062   1.410156   0.001416
   9 |   1.406250   1.410156   1.408203  -0.003775
  10 |   1.408203   1.410156   1.409180  -0.001181
  11 |   1.409180   1.410156   1.409668   0.000117
  12 |   1.409180   1.409668   1.409424  -0.000532
  13 |   1.409424   1.409668   1.409546  -0.000208
  14 |   1.409546   1.409668   1.409607  -0.000045
  15 |   1.409607   1.409668   1.409637   0.000036
  16 |   1.409607   1.409637   1.409622  -0.000005
Final Result for Root 2: x = 1.409622

--- False Position Method for Root 1 ---
Iter | a          b          mid        f(mid)
   1 |  -1.000000   0.000000  -0.543044  -0.188359
   2 |  -1.000000  -0.543044  -0.626623  -0.020932
   3 |  -1.000000  -0.626623  -0.635685  -0.002176
   4 |  -1.000000  -0.635685  -0.636625  -0.000225
   5 |  -1.000000  -0.636625  -0.636722  -0.000023
   6 |  -1.000000  -0.636722  -0.636732  -0.000002
Final Result for Root 1: x = -0.636732

--- False Position Method for Root 2 ---
Iter | a          b          mid        f(mid)
   1 |   1.000000   2.000000   1.286979  -0.303680
   2 |   1.286979   2.000000   1.377411  -0.084098
   3 |   1.377411   2.000000   1.401486  -0.021538
   4 |   1.401486   2.000000   1.407589  -0.005404
   5 |   1.407589   2.000000   1.409116  -0.001349
   6 |   1.409116   2.000000   1.409497  -0.000336
   7 |   1.409497   2.000000   1.409592  -0.000084
   8 |   1.409592   2.000000   1.409616  -0.000021
   9 |   1.409616   2.000000   1.409622  -0.000005
Final Result for Root 2: x = 1.409622