using System; using System.Text; using System.Runtime.InteropServices; using NagLibrary; class NagE02Functions { static void Main(string[] args) { double[] x = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 }; double[] y = { 0, 0.841470985, 0.909297427, 0.141120008, -0.756802495, -0.958924275, -0.279415498, 0.656986599, 0.989358247, 0.412118485, -0.544021111, -0.999990207, -0.536572918, 0.420167037 }; double[] w = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }; Console.WriteLine("Please pass a value for the degree k of the polynomial (less than 14, i.e., the number of points): "); int k = Convert.ToInt32(Console.ReadLine()); int kplus1 = k + 1; int lda = kplus1; double[] a = new double[kplus1 * kplus1]; double[] s = new double[kplus1]; double[] ak = new double[kplus1]; double xcap, xarg; double fit = 0.0D; int m = x.Length; nag_declarations.NagError fail = new nag_declarations.NagError(); fail.message = new byte[512]; Encoding enc = Encoding.ASCII; /* nag_1d_cheb_fit (e02adc). * Computes the coefficients of a Chebyshev series * polynomial for arbitrary data */ nag_declarations.e02adc(m, kplus1, lda, x, y, w, a, s, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_1d_cheb_fit (e02adc):"); Console.WriteLine(enc.GetString(fail.message)); Environment.Exit(fail.code); } for (int j = 0; j <= k; j++) { ak[j] = a[k * lda + j]; } Console.WriteLine("\nAbscissa Polynomial\n"); //evaluate at different points for (int i = 0; i <= m - 1; i++) { xcap = ((x[i] - x[0]) - (x[m - 1] - x[i])) / (x[m - 1] - x[0]); /* nag_1d_cheb_eval (e02aec). * Evaluates the coefficients of a Chebyshev series * polynomial */ nag_declarations.e02aec(kplus1, ak, xcap, ref fit, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_1d_cheb_eval (e02aec):"); Console.WriteLine(enc.GetString(fail.message)); Environment.Exit(fail.code); } Console.WriteLine("{0} \t {1, 10:f4}", x[i], fit); if (i < m - 1) { xarg = 0.5 * (x[i] + x[i + 1]); xcap = ((xarg - x[0]) - (x[m - 1] - xarg)) / (x[m - 1] - x[0]); /* nag_1d_cheb_eval (e02aec), see above. */ nag_declarations.e02aec(kplus1, ak, xcap, ref fit, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_1d_cheb_eval (e02aec):"); Console.WriteLine(enc.GetString(fail.message)); Environment.Exit(fail.code); } Console.WriteLine("{0} \t {1, 10:f4}", xarg, fit); } } } }