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

=== 二分法 ===
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

=== 二分法 ===
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

=== はさみうち法  ===
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

=== はさみうち法  ===
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