private static void lagrange_value_test() //****************************************************************************80 // // Purpose: // // LAGRANGE_VALUE_TEST tests LAGRANGE_VALUE. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 19 November 2014 // // Author: // // John Burkardt // { int i; Console.WriteLine(""); Console.WriteLine("LAGRANGE_VALUE_TEST"); Console.WriteLine(" LAGRANGE_VALUE evaluates the Lagrange basis polynomials."); const int nd = 5; const double xlo = 0.0; const double xhi = nd - 1; double[] xd = typeMethods.r8vec_linspace_new(nd, xlo, xhi); typeMethods.r8vec_print(nd, xd, " Lagrange basis points:"); // // Evaluate the polynomials. // Console.WriteLine(""); Console.WriteLine(" I X L1(X) L2(X) L3(X)" + " L4(X) L5(X)"); Console.WriteLine(""); int ni = 2 * nd - 1; double[] xi = typeMethods.r8vec_linspace_new(ni, xlo, xhi); double[] li = FEM_1D_Lagrange.lagrange_value(nd, xd, ni, xi); for (i = 0; i < ni; i++) { string cout = " " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2) + " " + xi[i].ToString(CultureInfo.InvariantCulture).PadLeft(10); int j; for (j = 0; j < nd; j++) { cout += " " + li[i + j * ni].ToString(CultureInfo.InvariantCulture).PadLeft(10); } Console.WriteLine(cout); } }
public static double[] lebesgue_function(int n, double[] x, int nfun, double[] xfun) //****************************************************************************80 // // Purpose: // // LEBESGUE_FUNCTION evaluates the Lebesgue function for a set of points. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 03 March 2014 // // Author: // // John Burkardt. // // Parameters: // // Jean-Paul Berrut, Lloyd Trefethen, // Barycentric Lagrange Interpolation, // SIAM Review, // Volume 46, Number 3, September 2004, pages 501-517. // // Parameters: // // Input, int N, the number of interpolation points. // // Input, double X[N], the interpolation points. // // Input, int NFUN, the number of evaluation points. // // Input, double XFUN[NFUN], the evaluation points. // // Output, double LEBESGUE_FUNCTION[NFUN], the Lebesgue function values. // { int j; double[] lfun = new double[nfun]; switch (n) { // // Handle special case. // case 1: { for (j = 0; j < nfun; j++) { lfun[j] = 1.0; } return(lfun); } } double[] llfun = FEM_1D_Lagrange.lagrange_value_OLD(n, x, nfun, xfun); for (j = 0; j < nfun; j++) { double t = 0.0; int i; for (i = 0; i < n; i++) { t += Math.Abs(llfun[i + j * n]); } lfun[j] = t; } return(lfun); }
private static void fem1d_lagrange_stiffness_test(int x_num, int q_num) //****************************************************************************80 // // Purpose: // // FEM1D_LAGRANGE_STIFFNESS_TEST tests FEM1D_LAGRANGE_STIFFNESS. // // Discussion: // // The results are very sensitive to the quadrature rule. // // In particular, if X_NUM points are used, the mass matrix will // involve integrals of polynomials of degree 2*(X_NUM-1), so the // quadrature rule should use at least Q_NUM = X_NUM - 1 points. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 17 November 2014 // // Author: // // John Burkardt // // Parameters: // // Input, int X_NUM, the number of nodes. // // Input, int Q_NUM, the number of quadrature points. // { int i; int j; Console.WriteLine(""); Console.WriteLine("FEM1D_LAGRANGE_STIFFNESS_TEST"); Console.WriteLine(" FEM1D_LAGRANGE_STIFFNESS computes the stiffness matrix,"); Console.WriteLine(" the mass matrix, and right hand side vector for a"); Console.WriteLine(" finite element problem using Lagrange interpolation"); Console.WriteLine(" basis polynomials."); Console.WriteLine(""); Console.WriteLine(" Solving:"); Console.WriteLine(" -u''+u=x on 0 < x < 1"); Console.WriteLine(" u(0) = u(1) = 0"); Console.WriteLine(" Exact solution:"); Console.WriteLine(" u(x) = x - sinh(x)/sinh(1)"); Console.WriteLine(""); Console.WriteLine(" Number of mesh points = " + x_num + ""); Console.WriteLine(" Number of quadrature points = " + q_num + ""); const double x_lo = 0.0; const double x_hi = 1.0; double[] x = typeMethods.r8vec_linspace_new(x_num, x_lo, x_hi); double[] a = new double[x_num * x_num]; double[] m = new double[x_num * x_num]; double[] b = new double[x_num]; FEM_1D_Lagrange.fem1d_lagrange_stiffness(x_num, x, q_num, FEM_Test_Methods.f, ref a, ref m, ref b); double[] k = new double[x_num * x_num]; for (j = 0; j < x_num; j++) { for (i = 0; i < x_num; i++) { k[i + j * x_num] = a[i + j * x_num] + m[i + j * x_num]; } } for (j = 0; j < x_num; j++) { k[0 + j * x_num] = 0.0; } k[0 + 0 * x_num] = 1.0; b[0] = 0.0; for (j = 0; j < x_num; j++) { k[x_num - 1 + j * x_num] = 0.0; } k[x_num - 1 + (x_num - 1) * x_num] = 1.0; b[x_num - 1] = 0.0; double[] u = typeMethods.r8mat_fs_new(x_num, k, b); double[] u_e = FEM_Test_Methods.exact(x_num, x); Console.WriteLine(""); Console.WriteLine(" I X U U(exact) Error"); Console.WriteLine(""); for (i = 0; i < x_num; i++) { Console.WriteLine(" " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2) + " " + x[i].ToString(CultureInfo.InvariantCulture).PadLeft(8) + " " + u[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + u_e[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + Math.Abs(u[i] - u_e[i]).ToString(CultureInfo.InvariantCulture).PadLeft(14) + ""); } }