Exemplo n.º 1
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests DIF*;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 June 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int MAXTAB = 10;

        double[] diftab  = new double[MAXTAB];
        double[] diftab2 = new double[MAXTAB];
        double[] diftab3 = new double[MAXTAB];
        int      i;
        int      ntab3 = 0;

        double[] xtab  = new double[MAXTAB];
        double[] xtab2 = new double[MAXTAB];
        double[] xtab3 = new double[MAXTAB];
        double[] ytab  = new double[MAXTAB];

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  DATA_TO_DIF_DISPLAY sets up a difference table");
        Console.WriteLine("  and displays intermediate calculations;");
        Console.WriteLine("  DIF_APPEND appends a new data point;");
        Console.WriteLine("  DIF_ANTIDERIV computes the antiderivative;");
        Console.WriteLine("  DIF_DERIV_TABLE computes the derivative;");
        Console.WriteLine("  DIF_SHIFT_ZERO shifts all the abscissas to 0;");
        Console.WriteLine("  DIF_VAL evaluates at a point;");
        Console.WriteLine("");
        //
        //  Set XTAB, YTAB to X, X^2.
        //
        int ntab = 4;

        for (i = 0; i < ntab; i++)
        {
            xtab[i] = i + 1;
            ytab[i] = xtab[i] * xtab[i];
        }

        Data.data_to_dif_display(ntab, xtab, ytab, ref diftab);

        Dif.dif_print(ntab, xtab, diftab, "  The divided difference polynomial:");
        //
        //  Add (5,25) to the table.
        //
        Console.WriteLine("");
        Console.WriteLine("  DIF_APPEND can add the data (5,25) to the table.");
        Console.WriteLine("");

        double xval = 5.0;
        double yval = 25.0;

        Dif.dif_append(ntab, xtab, diftab, xval, yval, ref ntab, ref xtab, ref diftab);

        Dif.dif_print(ntab, xtab, diftab,
                      "  The updated divided difference polynomial:");
        //
        //  Evaluate the polynomial at 2.5.
        //
        Console.WriteLine("");
        Console.WriteLine("  DIF_VAL can evaluate the table at a point.");
        Console.WriteLine("");

        xval = 2.5;

        yval = Dif.dif_val(ntab, xtab, diftab, xval);

        Console.WriteLine("");
        Console.WriteLine("  DIF_VAL reports P(" + xval + ") = " + yval + "");
        //
        //  Shift the base to zero.
        //
        Dif.dif_shift_zero(ntab, ref xtab, ref diftab);

        Dif.dif_print(ntab, xtab, diftab,
                      "  The divided difference table after DIF_SHIFT_ZERO:");
        //
        //  Compute a table for the derivative.
        //
        int ntab2 = ntab - 1;

        Dif.dif_deriv_table(ntab, xtab, diftab, ref xtab2, diftab2);

        Dif.dif_print(ntab2, xtab2, diftab2,
                      "  The divided difference table for the derivative:");

        yval = Dif.dif_val(ntab2, xtab2, diftab2, xval);

        Console.WriteLine("");
        Console.WriteLine("  DIF_VAL reports P'(" + xval + ") = " + yval + "");
        //
        //  Compute the antiderivative.
        //
        Dif.dif_antideriv(ntab, xtab, diftab, ref ntab3, xtab3, ref diftab3);

        Dif.dif_print(ntab3, xtab3, diftab3,
                      "  The divided difference table for the antiderivative:");

        yval = Dif.dif_val(ntab3, xtab3, diftab3, xval);

        Console.WriteLine("");
        Console.WriteLine("  DIF_VAL reports (Anti)P(" + xval + ") = " + yval + "");
    }
Exemplo n.º 2
0
    private static void dif_derivk_table_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    Dif.dif_derivk_table_test tests Dif.dif_derivk_table;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 June 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int i;
        int j;

        Console.WriteLine("");
        Console.WriteLine("Dif.dif_derivk_table_test");
        Console.WriteLine("  Dif.dif_derivk_table() computes the K-th derivative");
        Console.WriteLine("  for a divided difference table.");
        //
        //  Set the 0 data points.
        //
        int n0 = 5;

        double[] x0 = new double[n0];
        double[] f0 = new double[n0];
        for (i = 0; i < 5; i++)
        {
            x0[i] = i - 2;
        }

        //
        //  Set data for x^4/24+x^3/3+x^2/2+x+1
        //
        for (j = 0; j < n0; j++)
        {
            f0[j] = 1.0;
            for (i = 4; 1 <= i; i--)
            {
                f0[j] = f0[j] * x0[j] / i + 1.0;
            }
        }

        //
        //  Compute the difference table.
        //
        double[] d0 = new double[n0];
        Data.data_to_dif(n0, x0, f0, ref d0);
        Dif.dif_print(n0, x0, d0, "  The divided difference polynomial P0:");

        double[] c0 = new double[n0];
        Dif.dif_to_r8poly(n0, x0, d0, ref c0);

        typeMethods.r8poly_print(n0, c0, "  Using DIF_TO_R8POLY");
        //
        //  Compute the difference table for the K=1 derivative.
        //
        int k  = 1;
        int n1 = n0 - k;

        double[] x1 = new double[n1];
        double[] d1 = new double[n1];
        Dif.dif_derivk_table(n0, x0, d0, k, x1, ref d1);
        //
        //  Compute the difference table for the K=2 derivative.
        //
        k = 2;
        int n2 = n0 - k;

        double[] x2 = new double[n2];
        double[] d2 = new double[n2];
        Dif.dif_derivk_table(n0, x0, d0, k, x2, ref d2);
        //
        //  Compute the difference table for the K=3 derivative.
        //
        k = 3;
        int n3 = n0 - k;

        double[] x3 = new double[n3];
        double[] d3 = new double[n3];
        Dif.dif_derivk_table(n0, x0, d0, k, x3, ref d3);
        //
        //  Compute the difference table for the K=4 derivative.
        //
        k = 4;
        int n4 = n0 - k;

        double[] x4 = new double[n4];
        double[] d4 = new double[n4];
        Dif.dif_derivk_table(n0, x0, d0, k, x4, ref d4);
        //
        //  Evaluate all 5 polynomials.
        //
        Console.WriteLine("");
        Console.WriteLine("  Evaluate difference tables for the function P0");
        Console.WriteLine("  and its first four derivatives, P1...P4.");
        Console.WriteLine("");
        Console.WriteLine("      X         P0        P1        P2        P3        P4");
        Console.WriteLine("");

        for (i = 0; i <= 10; i++)
        {
            double x  = i / 5.0;
            double y0 = Dif.dif_val(n0, x0, d0, x);
            double y1 = Dif.dif_val(n1, x1, d1, x);
            double y2 = Dif.dif_val(n2, x2, d2, x);
            double y3 = Dif.dif_val(n3, x3, d3, x);
            double y4 = Dif.dif_val(n4, x4, d4, x);
            Console.WriteLine("  " + x.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + y0.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + y1.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + y2.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + y3.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + y4.ToString(CultureInfo.InvariantCulture).PadLeft(8) + "");
        }
    }
Exemplo n.º 3
0
    private static void test05()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 tests DATA_TO_DIF_DISPLAY, DIF_PRINT, DIF_SHIFT_ZERO, DIF_TO_R8POLY;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    18 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int MAXTAB = 10;

        double[] c       = new double[MAXTAB];
        double[] diftab1 = new double[MAXTAB];
        double[] diftab2 = new double[MAXTAB];
        int      i;

        double[] xtab1 = new double[MAXTAB];
        double[] xtab2 = new double[MAXTAB];
        double[] ytab1 = new double[MAXTAB];
        double[] ytab2 = new double[MAXTAB];

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  DIF_TO_R8POLY converts a difference table to a polynomial;");
        Console.WriteLine("  DIF_SHIFT_ZERO shifts a divided difference table to 0;");
        Console.WriteLine("");
        Console.WriteLine("  These are equivalent operations");
        Console.WriteLine("");
        //
        //  Set XTAB, YTAB to X, F(X)
        //
        const int ntab = 4;

        for (i = 0; i < ntab; i++)
        {
            double x = i + 1;
            xtab1[i] = x;
            xtab2[i] = x;
            ytab1[i] = -4.0 + x * (3.0 + x * (-2.0 + x));
            ytab2[i] = ytab1[i];
        }

        //
        //  Compute and display the finite difference table.
        //
        Data.data_to_dif_display(ntab, xtab1, ytab1, ref diftab1);

        Data.data_to_dif_display(ntab, xtab2, ytab2, ref diftab2);
        //
        //  Examine the corresponding polynomial.
        //
        Dif.dif_print(ntab, xtab1, diftab1, "  The divided difference table:");
        //
        //  Shift to zero using DIF_SHIFT_ZERO.
        //
        Dif.dif_shift_zero(ntab, ref xtab1, ref diftab1);

        typeMethods.r8poly_print(ntab, diftab1, "  The polynomial using DIF_SHIFT_ZERO:");
        //
        //  Shift to zero using DIF_TO_R8POLY.
        //
        Dif.dif_to_r8poly(ntab, xtab2, diftab2, ref c);

        typeMethods.r8poly_print(ntab, c, "  The polynomial using DIF_TO_R8POLY:");
    }