예제 #1
0
    public static void data_to_r8poly(int ntab, double[] xtab, double[] ytab, ref double[] c)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    DATA_TO_R8POLY computes the coefficients of a polynomial interpolating data.
    //
    //  Discussion:
    //
    //    Space can be saved by using a single array for both the C and
    //    YTAB parameters.  In that case, the coefficients will
    //    overwrite the Y data without interfering with the computation.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 September 2004
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Carl deBoor,
    //    A Practical Guide to Splines,
    //    Springer, 2001,
    //    ISBN: 0387953663,
    //    LC: QA1.A647.v27.
    //
    //  Parameters:
    //
    //    Input, int NTAB, the number of data points.
    //
    //    Input, double XTAB[NTAB], YTAB[NTAB], the data values.
    //
    //    Output, double C[NTAB], the coefficients of the polynomial that passes
    //    through the data (XTAB,YTAB).  C(0) is the constant term.
    //
    {
        if (!typeMethods.r8vec_distinct(ntab, xtab))
        {
            Console.WriteLine("");
            Console.WriteLine("DATA_TO_R8POLY - Fatal error!");
            Console.WriteLine("  Two entries of XTAB are equal.");
            return;
        }

        data_to_dif(ntab, xtab, ytab, ref c);

        Dif.dif_to_r8poly(ntab, xtab, c, ref c);
    }
예제 #2
0
    private static void dif_basis_deriv_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    Dif.dif_basis_deriv_test tests Dif.dif_basis_deriv;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 June 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int nd = 3;

        Console.WriteLine("");
        Console.WriteLine("Dif.dif_basis_deriv_test");
        Console.WriteLine("  Dif.dif_basis_deriv() computes difference tables for");
        Console.WriteLine("  the first derivative of each Lagrange basis.");

        double[] xd  = new double[nd];
        double[] xdp = new double[nd - 1];
        double[] ddp = new double[(nd - 1) * nd];

        xd[0] = -2.0;
        xd[1] = 1.0;
        xd[2] = 5.0;

        Dif.dif_basis_deriv(nd, xd, ref xdp, ref ddp);
        //
        //  Because the difference tables were shifted to all 0 abscissas,
        //  they contain the polynomial coefficients.
        //
        typeMethods.r8mat_transpose_print(nd - 1, nd, ddp,
                                          "  Lagrange basis derivative polynomial coefficients:");

        double[] c = new double[nd - 1];
        Dif.dif_to_r8poly(nd - 1, xdp, ddp, ref c, diftabIndex: +0 * (nd - 1));
        typeMethods.r8poly_print(nd - 1, c, "  P1'=-(2x-6)/21");

        Dif.dif_to_r8poly(nd - 1, xdp, ddp, ref c, diftabIndex:  +1 * (nd - 1));
        typeMethods.r8poly_print(nd - 1, c, "  P2'=-(2x-3)/12");

        Dif.dif_to_r8poly(nd - 1, xdp, ddp, ref c, diftabIndex:  +2 * (nd - 1));
        typeMethods.r8poly_print(nd - 1, c, "  P3'=(2x+1)/28");
    }
예제 #3
0
    public static double[] hermite_interpolant_rule(int n, double a, double b, double[] x)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HERMITE_INTERPOLANT_RULE: quadrature rule for a Hermite interpolant.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    31 October 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the number of abscissas.
    //
    //    Input, double A, B, the integration limits.
    //
    //    Input, double X[N], the abscissas.
    //
    //    Output, double HERMITE_INTERPOLANT_RULE[2*N], the quadrature
    //    coefficients, given as pairs for function and derivative values
    //    at each abscissa.
    //
    {
        int i;

        double[] y  = new double[n];
        double[] yp = new double[n];

        int nd = 2 * n;

        double[] c  = new double[nd];
        double[] w  = new double[nd];
        double[] xd = new double[nd];
        double[] yd = new double[nd];

        int ndp = 2 * n - 1;

        double[] xdp = new double[ndp];
        double[] ydp = new double[ndp];

        for (i = 0; i < n; i++)
        {
            y[i]  = 0.0;
            yp[i] = 0.0;
        }

        int k = 0;

        for (i = 0; i < n; i++)
        {
            y[i] = 1.0;
            hermite_interpolant(n, x, y, yp, ref xd, ref yd, ref xdp, ref ydp);
            Dif.dif_to_r8poly(nd, xd, yd, ref c);
            double a_value = typeMethods.r8poly_ant_val(n, c, a);
            double b_value = typeMethods.r8poly_ant_val(n, c, b);
            w[k] = b_value - a_value;
            y[i] = 0.0;
            k   += 1;

            yp[i] = 1.0;
            hermite_interpolant(n, x, y, yp, ref xd, ref yd, ref xdp, ref ydp);
            Dif.dif_to_r8poly(nd, xd, yd, ref c);
            a_value = typeMethods.r8poly_ant_val(n, c, a);
            b_value = typeMethods.r8poly_ant_val(n, c, b);
            w[k]    = b_value - a_value;
            yp[i]   = 0.0;
            k      += 1;
        }

        return(w);
    }
예제 #4
0
    public static void hermite_demo(int n, double[] x, double[] y, double[] yp)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HERMITE_DEMO computes and prints Hermite interpolant information for data.
    //
    //  Discussion:
    //
    //    Given a set of Hermite data, this routine calls HERMITE_INTERPOLANT to
    //    determine and print the divided difference table, and then DIF_TO_R8POLY to
    //    determine and print the coefficients of the polynomial in standard form.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    31 October 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the number of data points.
    //
    //    Input, double X[N], the abscissas.
    //
    //    Input, double Y[N], YP[N], the function and derivative
    //    values at the abscissas.
    //
    {
        int i;

        Console.WriteLine("");
        Console.WriteLine("HERMITE_DEMO");
        Console.WriteLine("  Compute coefficients CD of the Hermite polynomial");
        Console.WriteLine("  interpolant to given data (x,y,yp).");

        Console.WriteLine("");
        Console.WriteLine("  Data:");
        Console.WriteLine("              X           Y           Y'");
        Console.WriteLine("");
        for (i = 0; i < n; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + x[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + y[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + yp[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        int nd = 2 * n;

        double[] xd = new double[nd];
        double[] yd = new double[nd];

        int ndp = 2 * n - 1;

        double[] xdp = new double[ndp];
        double[] ydp = new double[ndp];

        hermite_interpolant(n, x, y, yp, ref xd, ref yd, ref xdp, ref ydp);

        Console.WriteLine("");
        Console.WriteLine("  Difference table:");
        Console.WriteLine("              XD          YD");
        Console.WriteLine("");

        for (i = 0; i < nd; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + xd[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + yd[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        Console.WriteLine("");
        Console.WriteLine("  Difference table:");
        Console.WriteLine("              XDP          YDP");
        Console.WriteLine("");
        for (i = 0; i < nd - 1; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + xdp[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + ydp[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        double[] cd = new double[2 * n];

        Dif.dif_to_r8poly(nd, xd, yd, ref cd);

        typeMethods.r8poly_print(nd - 1, cd, "  Hermite interpolating polynomial:");
        //
        //  Verify interpolation claim!
        //
        double[] xv  = new double[n];
        double[] yv  = new double[n];
        double[] yvp = new double[n];

        for (i = 0; i < n; i++)
        {
            xv[i] = x[i];
        }

        hermite_interpolant_value(nd, xd, yd, xdp, ydp, n, xv, ref yv, ref yvp);

        Console.WriteLine("");
        Console.WriteLine("  Data Versus Interpolant:");
        Console.WriteLine("              X           Y           H           YP          HP");
        Console.WriteLine("");
        for (i = 0; i < n; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + xv[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + y[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + yv[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + yp[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + yvp[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }
    }
예제 #5
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) + "");
        }
    }
예제 #6
0
    private static void test18()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST18 tests ROOTS_TO_DIF and DIF_TO_R8POLY.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    18 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int MAXROOTS = 4;

        double[] c      = new double[MAXROOTS + 1];
        double[] diftab = new double[MAXROOTS + 1];
        int      ntab   = 0;

        double[] roots = new double[MAXROOTS];
        double[] xtab  = new double[MAXROOTS + 1];

        Console.WriteLine("");
        Console.WriteLine("TEST18");
        Console.WriteLine("  ROOTS_TO_DIF computes a divided difference");
        Console.WriteLine("  polynomial with the given roots;");
        Console.WriteLine("  DIF_TO_R8POLY converts it to a standard form polynomial.");
        Console.WriteLine("");

        int nroots = 1;

        roots[0] = 3.0;
        typeMethods.r8vec_print(nroots, roots, "  The roots:");

        Roots.roots_to_dif(nroots, roots, ref ntab, ref xtab, ref diftab);
        Dif.dif_to_r8poly(ntab, xtab, diftab, ref c);
        typeMethods.r8poly_print(ntab, c, "  The polynomial:");

        nroots   = 2;
        roots[0] = 3.0;
        roots[1] = 1.0;
        typeMethods.r8vec_print(nroots, roots, "  The roots:");

        Roots.roots_to_dif(nroots, roots, ref ntab, ref xtab, ref diftab);
        Dif.dif_to_r8poly(ntab, xtab, diftab, ref c);
        typeMethods.r8poly_print(ntab, c, "  The polynomial:");

        nroots   = 3;
        roots[0] = 3.0;
        roots[1] = 1.0;
        roots[2] = 2.0;
        typeMethods.r8vec_print(nroots, roots, "  The roots:");

        Roots.roots_to_dif(nroots, roots, ref ntab, ref xtab, ref diftab);
        Dif.dif_to_r8poly(ntab, xtab, diftab, ref c);
        typeMethods.r8poly_print(ntab, c, "  The polynomial:");

        nroots   = 4;
        roots[0] = 3.0;
        roots[1] = 1.0;
        roots[2] = 2.0;
        roots[3] = 4.0;
        typeMethods.r8vec_print(nroots, roots, "  The roots:");

        Roots.roots_to_dif(nroots, roots, ref ntab, ref xtab, ref diftab);
        Dif.dif_to_r8poly(ntab, xtab, diftab, ref c);
        typeMethods.r8poly_print(ntab, c, "  The polynomial:");
    }
예제 #7
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:");
    }
예제 #8
0
    private static void dif_basis_derivk_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    Dif.dif_basis_derivk_test tests DIF_BASIS_DERIVK;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    04 June 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        // ReSharper disable once JoinDeclarationAndInitializer
        double[]  ddp;
        const int k  = 2;
        const int nd = 5;

        Console.WriteLine("");
        Console.WriteLine("Dif.dif_basis_derivk_test");
        Console.WriteLine("  DIF_BASIS_DERIVK computes difference tables for");
        Console.WriteLine("  the K-th derivative of each Lagrange basis.");

        double[] xd  = new double[nd];
        double[] xdp = new double[nd - k];
        ddp = new double[(nd - k) * nd];

        xd[0] = 1.0;
        xd[1] = 2.0;
        xd[2] = 3.0;
        xd[3] = 4.0;
        xd[4] = 5.0;

        Dif.dif_basis_derivk(nd, xd, k, ref xdp, ref ddp);
        //
        //  Because the difference tables were shifted to all 0 abscissas,
        //  they contain the polynomial coefficients.
        //
        typeMethods.r8mat_transpose_print(nd - k, nd, ddp,
                                          "  Lagrange basis K-th derivative polynomial coefficients:");

        double[] c = new double[nd - k];

        Dif.dif_to_r8poly(nd - k, xdp, ddp, ref c, diftabIndex: +0 * (nd - k));
        typeMethods.r8poly_print(nd - k, c, "  P1''=(12x^2-84x+142)/24");

        Dif.dif_to_r8poly(nd - k, xdp, ddp, ref c, diftabIndex:  +1 * (nd - k));
        typeMethods.r8poly_print(nd - k, c, "  P2''=-2x^2+13x-59/3");

        Dif.dif_to_r8poly(nd - k, xdp, ddp, ref c, diftabIndex:  +2 * (nd - k));
        typeMethods.r8poly_print(nd - k, c, "  P3''=3x^2-18x+49/2");

        Dif.dif_to_r8poly(nd - k, xdp, ddp, ref c, diftabIndex:  +3 * (nd - k));
        typeMethods.r8poly_print(nd - k, c, "  P4''=-2x^2+11x-41/3");

        Dif.dif_to_r8poly(nd - k, xdp, ddp, ref c, diftabIndex:  +4 * (nd - k));
        typeMethods.r8poly_print(nd - k, c, "  P5''=(6x^2-30x+35)/12");
    }