using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using NagCFunctionsAPI; namespace E02ADC { class Program { 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; NagError fail = new NagError(); try { NagFunctions.e02adc(m, kplus1, lda, x, y, w, a, s, ref fail); switch (fail.code) { case 249: throw new Exception("The weights are not all strictly positive."); case 66: throw new Exception("The values of x[r], for r = 0, 1, 2, . . . ,m-1, are not in nondecreasing order."); case 67: throw new Exception("All x[r] have the same value: thus the normalization of x is not possible."); case 11: throw new Exception("On entry, kplus1 < 1 (so the maximum degree required is negative) or kplus1 > mdist, where mdist is the number of distinct x values in the data (so there cannot be a unique solution for degree k = kplus1 - 1)."); case 17: throw new Exception("lda < kplus1."); default: break; } } catch (Exception ex) { Console.WriteLine(ex.Message); } for (int j = 0; j <= k; j++) { ak[j] = a[k, j]; } if (fail.code == 0) { 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]); try { NagFunctions.e02aec(kplus1, ak, xcap, ref fit, ref fail); switch (fail.code) { case 280: throw new Exception("ABS(xcap)> 1.0 + 4*eps, where eps is the machine precision. In this case the value of p is set arbitrarily to zero"); case 11: throw new Exception("On entry, nplus1 < 1."); default: break; } } catch (Exception ex) { Console.WriteLine(ex.Message); break; } 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]); try { NagFunctions.e02aec(kplus1, ak, xcap, ref fit, ref fail); switch (fail.code) { case 280: throw new Exception("ABS(xcap)> 1.0 + 4*eps, where eps is the machine precision. In this case the value of p is set arbitrarily to zero."); case 11: throw new Exception("On entry, nplus1 < 1."); default: break; } } catch (Exception ex) { Console.WriteLine(ex.Message); break; } Console.WriteLine("{0} \t {1, 10:f4}", xarg, fit); } } } } } }