コード例 #1
0
    private static void test04()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST04 tests DIFFER_FORWARD, DIFFER_BACKWARD, DIFFER_CENTRAL.
    //
    //  Discussion:
    //
    //    Evaluate the coefficients for uniformly spaced finite difference
    //    approximations of derivatives.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("TEST04");
        Console.WriteLine("  DIFFER_FORWARD,");
        Console.WriteLine("  DIFFER_BACKWARD, and");
        Console.WriteLine("  DIFFER_CENTRAL produce coefficients for difference");
        Console.WriteLine("  approximations of the O-th derivative,");
        Console.WriteLine("  with error of order H^P, for a uniform spacing of H.");

        double h = 1.0;

        Console.WriteLine("");
        Console.WriteLine("  Use a spacing of H = " + h + " for all examples.");
        //
        //  Forward difference approximation to the third derivative with error of O(h).
        //
        int o = 3;
        int p = 1;
        int n = o + p;

        double[] c = new double[n];
        double[] x = new double[n];
        Differ.differ_forward(h, o, p, ref c, ref x);
        string label = "  Forward difference coefficients, O = " + o.ToString()
                       + ", P = " + p.ToString();

        typeMethods.r8vec2_print(n, x, c, label);
        //
        //  Backward difference approximation to the third derivative with error of O(h).
        //
        o = 3;
        p = 1;
        n = o + p;
        c = new double[n];
        x = new double[n];
        Differ.differ_backward(h, o, p, ref c, ref x);
        label = "  Backward difference coefficients, O = " + o.ToString()
                + ", P = " + p.ToString();
        typeMethods.r8vec2_print(n, x, c, label);
        //
        //  Central difference approximation to the third derivative with error of O(h^2).
        //
        o = 3;
        p = 2;
        n = o + p;
        c = new double[n];
        x = new double[n];
        Differ.differ_central(h, o, p, ref c, ref x);
        label = "  Central difference coefficients, O = " + o.ToString()
                + ", P = " + p.ToString();
        typeMethods.r8vec2_print(n, x, c, label);
        //
        //  Central difference approximation to the third derivative with error of O(h^4).
        //
        o = 3;
        p = 4;
        n = o + p;
        c = new double[n];
        x = new double[n];
        Differ.differ_central(h, o, p, ref c, ref x);
        label = "  Central difference coefficients, O = " + o.ToString()
                + ", P = " + p.ToString();
        typeMethods.r8vec2_print(n, x, c, label);
        //
        //  Forward difference approximation to the fourth derivative with error of O(h).
        //
        o = 4;
        p = 1;
        n = o + p;
        c = new double[n];
        x = new double[n];
        Differ.differ_forward(h, o, p, ref c, ref x);
        label = "  Forward difference coefficients, O = " + o.ToString()
                + ", P = " + p.ToString();
        typeMethods.r8vec2_print(n, x, c, label);
        //
        //  Backward difference approximation to the fourth derivative with error of O(h).
        //
        o = 4;
        p = 1;
        n = o + p;
        c = new double[n];
        x = new double[n];
        Differ.differ_backward(h, o, p, ref c, ref x);
        label = "  Backward difference coefficients, O = " + o.ToString()
                + ", P = " + p.ToString();
        typeMethods.r8vec2_print(n, x, c, label);
        //
        //   Central difference approximation to the fourth derivative with error of O(h^3).
        //
        o = 4;
        p = 3;
        n = o + p;
        c = new double[n];
        x = new double[n];
        Differ.differ_central(h, o, p, ref c, ref x);
        label = "  Central difference coefficients, O = " + o.ToString()
                + ", P = " + p.ToString();
        typeMethods.r8vec2_print(n, x, c, label);
    }