回答編集履歴
4
あ
test
CHANGED
@@ -142,7 +142,7 @@
|
|
142
142
|
|
143
143
|
for (i = 1; i <= K_MAX; i++) {
|
144
144
|
|
145
|
-
if (df(x1) < EPS)
|
145
|
+
if (fabs(df(x1)) < EPS)
|
146
146
|
|
147
147
|
return false; // ニュートン法が失敗する場合
|
148
148
|
|
3
a
test
CHANGED
@@ -142,7 +142,7 @@
|
|
142
142
|
|
143
143
|
for (i = 1; i <= K_MAX; i++) {
|
144
144
|
|
145
|
-
if (df(x1)
|
145
|
+
if (df(x1) < EPS)
|
146
146
|
|
147
147
|
return false; // ニュートン法が失敗する場合
|
148
148
|
|
2
あ
test
CHANGED
@@ -52,7 +52,7 @@
|
|
52
52
|
|
53
53
|
double df(double x);
|
54
54
|
|
55
|
-
|
55
|
+
bool newton(double init_x, double *x, int *num_iters);
|
56
56
|
|
57
57
|
|
58
58
|
|
@@ -74,9 +74,13 @@
|
|
74
74
|
|
75
75
|
|
76
76
|
|
77
|
-
newton(init_x, &x, &num_iters)
|
77
|
+
if (newton(init_x, &x, &num_iters))
|
78
|
-
|
78
|
+
|
79
|
-
printf("number of iteration: %d, x=%.15f\n", num_iters, x);
|
79
|
+
printf("number of iteration: %d, x=%.15f\n", num_iters, x);
|
80
|
+
|
81
|
+
else
|
82
|
+
|
83
|
+
printf("newton method failed.");
|
80
84
|
|
81
85
|
}
|
82
86
|
|
@@ -126,7 +130,7 @@
|
|
126
130
|
|
127
131
|
*/
|
128
132
|
|
129
|
-
|
133
|
+
bool newton(double init_x, double *x, int *num_iters)
|
130
134
|
|
131
135
|
{
|
132
136
|
|
@@ -138,15 +142,29 @@
|
|
138
142
|
|
139
143
|
for (i = 1; i <= K_MAX; i++) {
|
140
144
|
|
145
|
+
if (df(x1) == 0)
|
146
|
+
|
147
|
+
return false; // ニュートン法が失敗する場合
|
148
|
+
|
149
|
+
|
150
|
+
|
141
|
-
x2 = x1 - f(x1) /
|
151
|
+
x2 = x1 - f(x1) / df(x1);
|
142
152
|
|
143
153
|
printf("%d: x=%.15f\n", i, x2);
|
144
154
|
|
145
155
|
|
146
156
|
|
147
|
-
if (fabs(x2 - x1) < EPS)
|
157
|
+
if (fabs(x2 - x1) < EPS) {
|
148
|
-
|
158
|
+
|
149
|
-
|
159
|
+
*x = x2;
|
160
|
+
|
161
|
+
*num_iters = i;
|
162
|
+
|
163
|
+
return true;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
150
168
|
|
151
169
|
x1 = x2;
|
152
170
|
|
@@ -154,15 +172,9 @@
|
|
154
172
|
|
155
173
|
|
156
174
|
|
157
|
-
//
|
175
|
+
return false; // 収束しなかった場合
|
158
|
-
|
159
|
-
|
176
|
+
|
160
|
-
|
161
|
-
*num_iters = i;
|
162
|
-
|
163
|
-
}
|
177
|
+
}
|
164
|
-
|
165
|
-
|
166
178
|
|
167
179
|
```
|
168
180
|
|
1
a
test
CHANGED
@@ -138,7 +138,7 @@
|
|
138
138
|
|
139
139
|
for (i = 1; i <= K_MAX; i++) {
|
140
140
|
|
141
|
-
x2 = x1 - f(x1) / df(x1);
|
141
|
+
x2 = x1 - f(x1) / (df(x1) + EPS);
|
142
142
|
|
143
143
|
printf("%d: x=%.15f\n", i, x2);
|
144
144
|
|