private static void test01(int prob, int grd, int m) //****************************************************************************80 // // Purpose: // // VANDERMONDE_APPROX_2D_TEST01 tests VANDERMONDE_APPROX_2D_MATRIX. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 11 October 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int PROB, the problem number. // // Input, int GRD, the grid number. // (Can't use GRID as the name because that's also a plotting function.) // // Input, int M, the total polynomial degree. // { Console.WriteLine(""); Console.WriteLine("TEST01:"); Console.WriteLine(" Approximate data from TEST_INTERP_2D problem #" + prob + ""); Console.WriteLine(" Use grid from TEST_INTERP_2D with index #" + grd + ""); Console.WriteLine(" Using polynomial approximant of total degree " + m + ""); int nd = Data_2D.g00_size(grd); Console.WriteLine(" Number of data points = " + nd + ""); double[] xd = new double[nd]; double[] yd = new double[nd]; Data_2D.g00_xy(grd, nd, ref xd, ref yd); double[] zd = new double[nd]; Data_2D.f00_f0(prob, nd, xd, yd, ref zd); switch (nd) { case < 10: typeMethods.r8vec3_print(nd, xd, yd, zd, " X, Y, Z data:"); break; } // // Compute the Vandermonde matrix. // int tm = typeMethods.triangle_num(m + 1); double[] a = VandermondeMatrix.vandermonde_approx_2d_matrix(nd, m, tm, xd, yd); // // Solve linear system. // double[] c = QRSolve.qr_solve(nd, tm, a, zd); // // #1: Does approximant match function at data points? // int ni = nd; double[] xi = typeMethods.r8vec_copy_new(ni, xd); double[] yi = typeMethods.r8vec_copy_new(ni, yd); double[] zi = Polynomial.r8poly_values_2d(m, c, ni, xi, yi); double app_error = typeMethods.r8vec_norm_affine(ni, zi, zd) / ni; Console.WriteLine(""); Console.WriteLine(" L2 data approximation error = " + app_error + ""); }
public static double[] vandermonde_approx_2d_coef(int n, int m, double[] x, double[] y, double[] z) //****************************************************************************80 // // Purpose: // // VANDERMONDE_APPROX_2D_COEF computes a 2D polynomial approximant. // // Discussion: // // We assume the approximating function has the form of a polynomial // in X and Y of total degree M. // // p(x,y) = c00 // + c10 * x + c01 * y // + c20 * x^2 + c11 * xy + c02 * y^2 // + ... // + cm0 * x^(m) + ... + c0m * y^m. // // If we let T(K) = the K-th triangular number // = sum ( 1 <= I <= K ) I // then the number of coefficients in the above polynomial is T(M+1). // // We have n data locations (x(i),y(i)) and values z(i) to approximate: // // p(x(i),y(i)) = z(i) // // This can be cast as an NxT(M+1) linear system for the polynomial // coefficients: // // [ 1 x1 y1 x1^2 ... y1^m ] [ c00 ] = [ z1 ] // [ 1 x2 y2 x2^2 ... y2^m ] [ c10 ] = [ z2 ] // [ 1 x3 y3 x3^2 ... y3^m ] [ c01 ] = [ z3 ] // [ ...................... ] [ ... ] = [ ... ] // [ 1 xn yn xn^2 ... yn^m ] [ c0m ] = [ zn ] // // In the typical case, N is greater than T(M+1) (we have more data and // equations than degrees of freedom) and so a least squares solution is // appropriate, in which case the computed polynomial will be a least squares // approximant to the data. // // The polynomial defined by the T(M+1) coefficients C could be evaluated // at the Nx2-vector x by the command // // pval = r8poly_value_2d ( m, c, n, x ) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 12 October 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of data points. // // Input, int M, the maximum degree of the polynomial. // // Input, double X[N], Y[N] the data locations. // // Input, double Z[N], the data values. // // Output, double VANDERMONDE_APPROX_2D_COEF[T(M+1)], the // coefficients of the approximating polynomial. // { int tm = typeMethods.triangle_num(m + 1); double[] a = VandermondeMatrix.vandermonde_approx_2d_matrix(n, m, tm, x, y); double[] c = QRSolve.qr_solve(n, tm, a, z); return(c); }