Example #1
0
    private static void chkder_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CHKDER_TEST tests CHKDER.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 April 2010
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int ido;

        const int m = 5;
        const int n = 5;

        double[] err   = new double[m];
        double[] fjac  = new double[n * n];
        double[] fvec  = new double[m];
        double[] fvecp = new double[m];
        double[] x     = new double[n];
        double[] xp    = new double[n];

        Console.WriteLine("");
        Console.WriteLine("CHKDER_TEST");
        Console.WriteLine("  CHKDER compares a user supplied jacobian");
        Console.WriteLine("  and a finite difference approximation to it");
        Console.WriteLine("  and judges whether the jacobian is correct.");

        for (ido = 1; ido <= 2; ido++)
        {
            int seed = 123456789;
            switch (ido)
            {
            case 1:
                Console.WriteLine("");
                Console.WriteLine("  On the first test, use a correct jacobian.");
                break;

            case 2:
                Console.WriteLine("");
                Console.WriteLine("  Repeat the test, but use a bad jacobian");
                Console.WriteLine("  and see if the routine notices.");
                break;
            }

            //
            //  Set the point at which the test is to be made:
            //
            int i;
            for (i = 0; i < n; i++)
            {
                x[i] = UniformRNG.r8_uniform_01(ref seed);
            }

            Console.WriteLine("");
            Console.WriteLine("  Evaluation point X:");
            Console.WriteLine("");
            for (i = 0; i < n; i++)
            {
                Console.WriteLine("  " + x[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
            }

            int mode = 1;
            Minpack.chkder(m, n, x, fvec, fjac, n, xp, fvecp, mode, ref err);

            int iflag = 1;
            chkder_f(n, x, fvec, fjac, n, ref iflag);
            chkder_f(n, xp, fvecp, fjac, n, ref iflag);

            Console.WriteLine("");
            Console.WriteLine("  Sampled function values F(X) and F(XP)");
            Console.WriteLine("");
            for (i = 0; i < m; i++)
            {
                Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                                  + "  " + fvec[i].ToString(CultureInfo.InvariantCulture).PadLeft(14)
                                  + "  " + fvecp[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
            }

            iflag = 2;
            chkder_f(n, x, fvec, fjac, n, ref iflag);
            switch (ido)
            {
            //
            //  Here's where we put a mistake into the jacobian, on purpose.
            //
            case 2:
                fjac[0 + 0 * n] = 1.01 * fjac[0 + 0 * n];
                fjac[1 + 2 * n] = -fjac[1 + 2 * n];
                break;
            }

            Console.WriteLine("");
            Console.WriteLine("  Computed jacobian:");
            Console.WriteLine("");
            string cout = "";
            for (i = 0; i < m; i++)
            {
                int j;
                for (j = 0; j < n; j++)
                {
                    cout += "  " + fjac[i + j * n].ToString(CultureInfo.InvariantCulture).PadLeft(12);
                }

                Console.WriteLine(cout);
            }

            mode = 2;
            Minpack.chkder(m, n, x, fvec, fjac, n, xp, fvecp, mode, ref err);

            Console.WriteLine("");
            Console.WriteLine("  CHKDER gradient component error estimates:");
            Console.WriteLine("     > 0.5, the component is probably correct.");
            Console.WriteLine("     < 0.5, the component is probably incorrect.");
            Console.WriteLine("");
            for (i = 0; i < m; i++)
            {
                Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                                  + "  " + err[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
            }
        }
    }
Example #2
0
    private static void hybrd1_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HYBRD1_TEST tests HYBRD1.
    //
    //  Discussion:
    //
    //    This is an example of what your main program would look
    //    like if you wanted to use MINPACK to solve N nonlinear equations
    //    in N unknowns.  In this version, we avoid computing the jacobian
    //    matrix, and request that MINPACK approximate it for us.
    //
    //    The set of nonlinear equations is:
    //
    //      x1 * x1 - 10 * x1 + x2 * x2 + 8 = 0
    //      x1 * x2 * x2 + x1 - 10 * x2 + 8 = 0
    //
    //    with solution x1 = x2 = 1
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    08 April 2010
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int    n   = 2;
        const double tol = 0.00001;

        const int lwa = n * (3 * n + 13) / 2;

        double[] fvec = new double[n];
        double[] wa   = new double[lwa];
        double[] x    = new double[n];

        Console.WriteLine("");
        Console.WriteLine("HYBRD1_TEST");
        Console.WriteLine("  HYBRD1 solves a nonlinear system of equations.");

        x[0] = 3.0;
        x[1] = 0.0;
        typeMethods.r8vec_print(n, x, "  Initial X");
        const int iflag = 1;
        fcnData   res   = hybrd1_f(n, x, fvec, iflag, 0, 0);

        fvec = res.fvec;

        typeMethods.r8vec_print(n, fvec, "  F(X)");

        int info = Minpack.hybrd1(hybrd1_f, n, x, fvec, tol, wa, lwa);

        Console.WriteLine("");
        Console.WriteLine("  Returned value of INFO = " + info + "");
        typeMethods.r8vec_print(n, x, "  X");
        typeMethods.r8vec_print(n, fvec, "  F(X)");
    }