Exemple #1
0
    private static void test05()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 tests DIFFER_STENCIL.
    //
    //  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
    //
    {
        int i;

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  DIFFER_STENCIL produces coefficients for difference");
        Console.WriteLine("  approximations of the O-th derivative,");
        Console.WriteLine("  using arbitrarily spaced data, with maximum spacing H");
        Console.WriteLine("  with error of order H^P.");
        //
        //  Let X0 = 1.0.
        //
        double x0 = 0.0;
        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];
        for (i = 0; i < n; i++)
        {
            x[i] = i * h;
        }

        Differ.differ_stencil(x0, o, p, x, ref c);
        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];
        for (i = 0; i < n; i++)
        {
            x[i] = (i + 1 - n) * h;
        }

        Differ.differ_stencil(x0, o, p, x, ref c);
        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];
        for (i = 0; i < n; i++)
        {
            x[i] = (-n + 1 + 2 * i) * h / 2.0;
        }

        Differ.differ_stencil(x0, o, p, x, ref c);
        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];
        for (i = 0; i < n; i++)
        {
            x[i] = (-n + 1 + 2 * i) * h / 2.0;
        }

        Differ.differ_stencil(x0, o, p, x, ref c);
        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];
        for (i = 0; i < n; i++)
        {
            x[i] = i * h;
        }

        Differ.differ_stencil(x0, o, p, x, ref c);
        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];
        for (i = 0; i < n; i++)
        {
            x[i] = (i + 1 - n) * h;
        }

        Differ.differ_stencil(x0, o, p, x, ref c);
        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];
        for (i = 0; i < n; i++)
        {
            x[i] = (-n + 1 + 2 * i) * h / 2.0;
        }

        Differ.differ_stencil(x0, o, p, x, ref c);
        label = "  Central difference coefficients, O = " + o.ToString()
                + ", P = " + p.ToString();
        typeMethods.r8vec2_print(n, x, c, label);
    }