fork 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 - sin(x);
  8. }
  9.  
  10. // 二分法
  11. double bisection(double xl, double xr) {
  12. double xi;
  13. int i = 0;
  14.  
  15. printf("\n=== Bisection Method ===\n");
  16. printf("i\t xl\t\t xi\t\t xr\t\t f(xl)\t\t f(xi)\t\t f(xr)\n");
  17.  
  18. while (1) {
  19. i++;
  20. xi = (xl + xr) / 2.0;
  21.  
  22. printf("%d\t %.6f %.6f %.6f %.6f %.6f %.6f\n",
  23. i, xl, xi, xr, f(xl), f(xi), f(xr));
  24.  
  25. if (fabs(f(xi)) < EPS) {
  26. printf("→ Root ≈ %.6f\n", xi);
  27. return xi;
  28. }
  29.  
  30. if (f(xl) * f(xi) < 0)
  31. xr = xi;
  32. else
  33. xl = xi;
  34. }
  35. }
  36.  
  37. // はさみうち法
  38. double false_position(double xl, double xr) {
  39. double xi;
  40. int i = 0;
  41.  
  42. printf("\n=== False Position Method ===\n");
  43. printf("i\t xl\t\t xi\t\t xr\t\t f(xl)\t\t f(xi)\t\t f(xr)\n");
  44.  
  45. while (1) {
  46. i++;
  47.  
  48. xi = (xl * f(xr) - xr * f(xl)) / (f(xr) - f(xl));
  49.  
  50. printf("%d\t %.6f %.6f %.6f %.6f %.6f %.6f\n",
  51. i, xl, xi, xr, f(xl), f(xi), f(xr));
  52.  
  53. if (fabs(f(xi)) < EPS) {
  54. printf("→ Root ≈ %.6f\n", xi);
  55. return xi;
  56. }
  57.  
  58. if (f(xl) * f(xi) < 0)
  59. xr = xi;
  60. else
  61. xl = xi;
  62. }
  63. }
  64.  
  65. int main() {
  66.  
  67. printf("Solving f(x) = x^2 - 1 - sin(x) = 0\n");
  68.  
  69. // 二分法
  70. bisection(-1.0, 0.0);
  71. bisection(1.0, 2.0);
  72.  
  73. // はさみうち法
  74. false_position(-1.0, 0.0);
  75. false_position(1.0, 2.0);
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Solving f(x) = x^2 - 1 - sin(x) = 0

=== Bisection Method ===
i	 xl		 xi		 xr		 f(xl)		 f(xi)		 f(xr)
1	 -1.000000 -0.500000 0.000000 0.841471 -0.270574 -1.000000
2	 -1.000000 -0.750000 -0.500000 0.841471 0.244139 -0.270574
3	 -0.750000 -0.625000 -0.500000 0.244139 -0.024278 -0.270574
4	 -0.750000 -0.687500 -0.625000 0.244139 0.107263 -0.024278
5	 -0.687500 -0.656250 -0.625000 0.107263 0.040814 -0.024278
6	 -0.656250 -0.640625 -0.625000 0.040814 0.008097 -0.024278
7	 -0.640625 -0.632812 -0.625000 0.008097 -0.008133 -0.024278
8	 -0.640625 -0.636719 -0.632812 0.008097 -0.000029 -0.008133
9	 -0.640625 -0.638672 -0.636719 0.008097 0.004031 -0.000029
10	 -0.638672 -0.637695 -0.636719 0.004031 0.002001 -0.000029
11	 -0.637695 -0.637207 -0.636719 0.002001 0.000986 -0.000029
12	 -0.637207 -0.636963 -0.636719 0.000986 0.000478 -0.000029
13	 -0.636963 -0.636841 -0.636719 0.000478 0.000225 -0.000029
14	 -0.636841 -0.636780 -0.636719 0.000225 0.000098 -0.000029
15	 -0.636780 -0.636749 -0.636719 0.000098 0.000035 -0.000029
16	 -0.636749 -0.636734 -0.636719 0.000035 0.000003 -0.000029
→ Root ≈ -0.636734

=== Bisection Method ===
i	 xl		 xi		 xr		 f(xl)		 f(xi)		 f(xr)
1	 1.000000 1.500000 2.000000 -0.841471 0.252505 2.090703
2	 1.000000 1.250000 1.500000 -0.841471 -0.386485 0.252505
3	 1.250000 1.375000 1.500000 -0.386485 -0.090268 0.252505
4	 1.375000 1.437500 1.500000 -0.090268 0.075277 0.252505
5	 1.375000 1.406250 1.437500 -0.090268 -0.008954 0.075277
6	 1.406250 1.421875 1.437500 -0.008954 0.032797 0.075277
7	 1.406250 1.414062 1.421875 -0.008954 0.011830 0.032797
8	 1.406250 1.410156 1.414062 -0.008954 0.001416 0.011830
9	 1.406250 1.408203 1.410156 -0.008954 -0.003775 0.001416
10	 1.408203 1.409180 1.410156 -0.003775 -0.001181 0.001416
11	 1.409180 1.409668 1.410156 -0.001181 0.000117 0.001416
12	 1.409180 1.409424 1.409668 -0.001181 -0.000532 0.000117
13	 1.409424 1.409546 1.409668 -0.000532 -0.000208 0.000117
14	 1.409546 1.409607 1.409668 -0.000208 -0.000045 0.000117
15	 1.409607 1.409637 1.409668 -0.000045 0.000036 0.000117
16	 1.409607 1.409622 1.409637 -0.000045 -0.000005 0.000036
→ Root ≈ 1.409622

=== False Position Method ===
i	 xl		 xi		 xr		 f(xl)		 f(xi)		 f(xr)
1	 -1.000000 -0.543044 0.000000 0.841471 -0.188359 -1.000000
2	 -1.000000 -0.626623 -0.543044 0.841471 -0.020932 -0.188359
3	 -1.000000 -0.635685 -0.626623 0.841471 -0.002176 -0.020932
4	 -1.000000 -0.636625 -0.635685 0.841471 -0.000225 -0.002176
5	 -1.000000 -0.636722 -0.636625 0.841471 -0.000023 -0.000225
6	 -1.000000 -0.636732 -0.636722 0.841471 -0.000002 -0.000023
→ Root ≈ -0.636732

=== False Position Method ===
i	 xl		 xi		 xr		 f(xl)		 f(xi)		 f(xr)
1	 1.000000 1.286979 2.000000 -0.841471 -0.303680 2.090703
2	 1.286979 1.377411 2.000000 -0.303680 -0.084098 2.090703
3	 1.377411 1.401486 2.000000 -0.084098 -0.021538 2.090703
4	 1.401486 1.407589 2.000000 -0.021538 -0.005404 2.090703
5	 1.407589 1.409116 2.000000 -0.005404 -0.001349 2.090703
6	 1.409116 1.409497 2.000000 -0.001349 -0.000336 2.090703
7	 1.409497 1.409592 2.000000 -0.000336 -0.000084 2.090703
8	 1.409592 1.409616 2.000000 -0.000084 -0.000021 2.090703
9	 1.409616 1.409622 2.000000 -0.000021 -0.000005 2.090703
→ Root ≈ 1.409622