C++で非線形方程式を解く (ベイリー法)
非線形方程式の解法(ベイリー法)を利用して2の平方根を求める
関数 をテイラー展開すると,
と, 近似できる.
この式を 左辺 , として変形すると,
この式の右辺の をニュートン法で使った
で置き換えると
この式を漸化式として用いる.
#include <stdio.h> #include <math.h> double f(double x) { return x * x - 2; } double g(double x) { return 2 * x; } double h(double x) { return 2; } int main() { double low = 1; double high = 2; double x = (low + high) / 2; while (true) { double dx = x; x = x - (f(x) / (g(x) - (f(x) * h(x) / (2 * g(x) )))); printf("%12.10f %12.10f\n", dx, x); if (fabs(x - dx) < 0.0000000001) break; } printf("%12.10f %12.10f\n", x, sqrt(2)); return 0; }
1.5000000000 1.4142857143 1.4142857143 1.4142135624 1.4142135624 1.4142135624 1.4142135624 1.4142135624
非線形方程式の解法(ベイリー法)を利用して2の立方根を求める
#include <stdio.h> #include <math.h> double f(double x) { return x * x * x - 2; } double g(double x) { return 3 * x * x; } double h(double x) { return 6 * x; } int main() { double low = 1; double high = 2; double x = (low + high) / 2; while (true) { double dx = x; x = x - (f(x) / (g(x) - (f(x) * h(x) / (2 * g(x) )))); printf("%12.10f %12.10f\n", dx, x); if (fabs(x - dx) < 0.0000000001) break; } printf("%12.10f %12.10f\n", x, cbrt(2)); return 0; }
1.5000000000 1.2642857143 1.2642857143 1.2599210846 1.2599210846 1.2599210499 1.2599210499 1.2599210499 1.2599210499 1.2599210499
参考文献