Beispiel #1
0
    public static vector newton(Func <vector, vector> f, vector x, double eps = 1e-3, double dx = 1e-7)
    {
        int    n = x.size;
        vector fx = f(x), z, fz;

        while (true)
        {
            matrix J = jacobi(f, fx, x);
            matrix R = new matrix(n, n);
            QR.qr_gs_decomp(J, R);
            matrix B = QR.qr_gs_inverse(J, R);
            //B.print("inverse B");
            vector del_x  = -B * fx;
            double lambda = 1;
            while (true)
            {
                z  = x + lambda * del_x;
                fz = f(z);
                if (fz.norm() < (1 - lambda / 2) * fx.norm())
                {
                    break;
                }
                else if (lambda < 1.0 / 64)
                {
                    break;
                }
                else
                {
                    lambda /= 2;
                }
            }
            x  = z;
            fx = fz;
            if (fx.norm() < eps)
            {
                break;
            }
            else if (x.norm() < dx)
            {
                break;
            }
        }
        return(x);
    }