ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

C++で非線形方程式を解く (ベイリー法)

非線形方程式の解法(ベイリー法)を利用して2の平方根を求める

関数 f(x)テイラー展開すると,
f:id:fornext1119:20140601103603p:plain

と, 近似できる.
この式を 左辺 = 0, x = x_{n+1} として変形すると,
f:id:fornext1119:20140601115218p:plain

この式の右辺の x_{n+1}ニュートン法で使った
f:id:fornext1119:20140601115611p:plain

で置き換えると
f:id:fornext1119:20140601120743p:plain

この式を漸化式として用いる.

#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
参考文献