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) + ""); } }
private static void dif_basis_test() //****************************************************************************80 // // Purpose: // // Dif.dif_basis_test tests Dif.dif_basis(). // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 18 January 2007 // // Author: // // John Burkardt // { const int NTAB = 5; int i; int j; const int nstep = 9; int pointerIndex = 0; const int set = 3; double[] xtab = new double[NTAB]; Console.WriteLine(""); Console.WriteLine("Dif.dif_basis_test"); Console.WriteLine(" Dif.dif_basis() computes Lagrange basis polynomials"); Console.WriteLine(" in difference form."); Console.WriteLine(""); // // Set the base points. // typeMethods.r8vec_indicator(NTAB, ref xtab); typeMethods.r8vec_print(NTAB, xtab, " The base points:"); // // Get the difference tables for the basis polynomials and print them. // double[] diftab = new double[NTAB * NTAB]; Dif.dif_basis(NTAB, xtab, ref diftab); Console.WriteLine(""); Console.WriteLine(" The table of difference vectors defining the basis polynomials."); Console.WriteLine(" Each ROW represents a polynomial."); Console.WriteLine(""); double[] pointer = diftab; for (i = 0; i < NTAB; i++) { string cout = " "; for (j = 0; j < NTAB; j++) { cout += pointerIndex.ToString(CultureInfo.InvariantCulture).PadLeft(10) + " "; pointerIndex++; } Console.WriteLine(cout); } // // Evaluate basis polynomial 3 at a set of points. // Console.WriteLine(""); Console.WriteLine(" Evaluate basis polynomial #" + set + " at a set of points."); Console.WriteLine(""); Console.WriteLine(" X Y"); Console.WriteLine(""); const double xhi = NTAB; const double xlo = 1.0; // // Advance pointer to beginning of data for basis polynomial SET. // pointer = diftab; pointerIndex = 0; for (i = 1; i < set; i++) { for (j = 1; j <= NTAB; j++) { pointerIndex++; } } for (i = 1; i <= nstep; i++) { double xval = ((nstep - i) * xlo + (i - 1) * xhi) / (nstep - 1); double yval = Dif.dif_val(NTAB, xtab, pointer, xval, diftabIndex: pointerIndex); Console.WriteLine(" " + xval.ToString(CultureInfo.InvariantCulture).PadLeft(10) + " " + yval.ToString(CultureInfo.InvariantCulture).PadLeft(10) + ""); } }
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 + ""); }
private static void test02() //****************************************************************************80 // // Purpose: // // TEST02 tests DATA_TO_DIF and DIF_VAL. // // Discussion: // // This routine demonstrates how divided difference approximation // improves with N. // // Evaluate these polynomials at 2.5. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 18 January 2007 // // Author: // // John Burkardt // { const int MAXTAB = 8; double[] diftab = new double[MAXTAB]; int j; int ntab; double[] xtab = new double[MAXTAB]; double[] ytab = new double[MAXTAB]; Console.WriteLine(""); Console.WriteLine("TEST02"); Console.WriteLine(" Approximate Y = EXP(X) using orders 1 to " + MAXTAB + "."); Console.WriteLine(""); Console.WriteLine(" Original data:"); Console.WriteLine(""); Console.WriteLine(" X Y"); Console.WriteLine(""); for (j = 0; j < MAXTAB; j++) { xtab[j] = j; ytab[j] = Math.Exp(xtab[j]); Console.WriteLine(" " + xtab[j].ToString(CultureInfo.InvariantCulture).PadLeft(10) + " " + ytab[j].ToString(CultureInfo.InvariantCulture).PadLeft(10) + ""); } const double xval = 2.5; double true_value = Math.Exp(xval); Console.WriteLine(""); Console.WriteLine(" Evaluate at X = " + xval + " where EXP(X) = " + true_value + ""); Console.WriteLine(""); Console.WriteLine(" Order Approximate Y Error"); Console.WriteLine(""); for (ntab = 1; ntab <= MAXTAB; ntab++) { for (j = 0; j < ntab; j++) { xtab[j] = j; ytab[j] = Math.Exp(xtab[j]); } Data.data_to_dif(ntab, xtab, ytab, ref diftab); double yval = Dif.dif_val(ntab, xtab, diftab, xval); double error = yval - true_value; Console.WriteLine(" " + ntab.ToString(CultureInfo.InvariantCulture).PadLeft(6) + " " + yval.ToString(CultureInfo.InvariantCulture).PadLeft(10) + " " + error.ToString(CultureInfo.InvariantCulture).PadLeft(10) + ""); } }