回答編集履歴
4
あ
answer
CHANGED
@@ -70,7 +70,7 @@
|
|
70
70
|
int i;
|
71
71
|
|
72
72
|
for (i = 1; i <= K_MAX; i++) {
|
73
|
-
if (df(x1) < EPS)
|
73
|
+
if (fabs(df(x1)) < EPS)
|
74
74
|
return false; // ニュートン法が失敗する場合
|
75
75
|
|
76
76
|
x2 = x1 - f(x1) / df(x1);
|
3
a
answer
CHANGED
@@ -70,7 +70,7 @@
|
|
70
70
|
int i;
|
71
71
|
|
72
72
|
for (i = 1; i <= K_MAX; i++) {
|
73
|
-
if (df(x1)
|
73
|
+
if (df(x1) < EPS)
|
74
74
|
return false; // ニュートン法が失敗する場合
|
75
75
|
|
76
76
|
x2 = x1 - f(x1) / df(x1);
|
2
あ
answer
CHANGED
@@ -25,7 +25,7 @@
|
|
25
25
|
void input(double *init_x);
|
26
26
|
double f(double x);
|
27
27
|
double df(double x);
|
28
|
-
|
28
|
+
bool newton(double init_x, double *x, int *num_iters);
|
29
29
|
|
30
30
|
int main()
|
31
31
|
{
|
@@ -36,8 +36,10 @@
|
|
36
36
|
printf("input x: ");
|
37
37
|
scanf("%lf", &init_x);
|
38
38
|
|
39
|
-
newton(init_x, &x, &num_iters)
|
39
|
+
if (newton(init_x, &x, &num_iters))
|
40
|
-
|
40
|
+
printf("number of iteration: %d, x=%.15f\n", num_iters, x);
|
41
|
+
else
|
42
|
+
printf("newton method failed.");
|
41
43
|
}
|
42
44
|
|
43
45
|
/**
|
@@ -62,25 +64,29 @@
|
|
62
64
|
@param x 解
|
63
65
|
@param num_iters 反復回数
|
64
66
|
*/
|
65
|
-
|
67
|
+
bool newton(double init_x, double *x, int *num_iters)
|
66
68
|
{
|
67
69
|
double x1 = init_x, x2;
|
68
70
|
int i;
|
69
71
|
|
70
72
|
for (i = 1; i <= K_MAX; i++) {
|
73
|
+
if (df(x1) == 0)
|
74
|
+
return false; // ニュートン法が失敗する場合
|
75
|
+
|
71
|
-
x2 = x1 - f(x1) /
|
76
|
+
x2 = x1 - f(x1) / df(x1);
|
72
77
|
printf("%d: x=%.15f\n", i, x2);
|
73
78
|
|
74
|
-
if (fabs(x2 - x1) < EPS)
|
79
|
+
if (fabs(x2 - x1) < EPS) {
|
75
|
-
|
80
|
+
*x = x2;
|
81
|
+
*num_iters = i;
|
82
|
+
return true;
|
83
|
+
}
|
84
|
+
|
76
85
|
x1 = x2;
|
77
86
|
}
|
78
87
|
|
79
|
-
//
|
88
|
+
return false; // 収束しなかった場合
|
80
|
-
*x = x2;
|
81
|
-
*num_iters = i;
|
82
89
|
}
|
83
|
-
|
84
90
|
```
|
85
91
|
|
86
92
|
```
|
1
a
answer
CHANGED
@@ -68,7 +68,7 @@
|
|
68
68
|
int i;
|
69
69
|
|
70
70
|
for (i = 1; i <= K_MAX; i++) {
|
71
|
-
x2 = x1 - f(x1) / df(x1);
|
71
|
+
x2 = x1 - f(x1) / (df(x1) + EPS);
|
72
72
|
printf("%d: x=%.15f\n", i, x2);
|
73
73
|
|
74
74
|
if (fabs(x2 - x1) < EPS)
|