// e04ya Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04YAE { static int mdec = 15; static int ndec = 3; static double[,] t = new double[mdec, ndec]; static double[] y = new double[mdec]; static string datafile = "ExampleData/e04yae.d"; // as a command line argument. It defaults to the named file specified below otherwise. // static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { E04.E04YA_LSQFUN lsqfunE04YA = new E04.E04YA_LSQFUN(lsqfun); try { DataReader sr = new DataReader(datafile); int i, j, m, n; double[,] fjac = new double[mdec, ndec]; double[] fvec = new double[mdec]; double[] x = new double[ndec]; int ifail; Console.WriteLine("e04ya Example Program Results"); // Skip heading in data file sr.Reset(); n = ndec; m = mdec; // Observations of tj (j = 1, 2, 3) are held in t[i-1, j-1] // (i = 1, 2, . . . , 15) for (i = 1; i <= m; i++) { sr.Reset(); y[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); for (j = 1; j <= n; j++) { t[i - 1, j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } // Set up an arbitrary point at which to check the 1st // derivatives x[0] = 0.190e0; x[1] = -1.340e0; x[2] = 0.880e0; Console.WriteLine(""); Console.WriteLine(" {0}", "The test point is"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 10:f5}", x[j - 1]); } Console.WriteLine(" "); // E04.e04ya(m, n, lsqfunE04YA, x, fvec, fjac, out ifail); // Console.WriteLine(""); if (ifail < 0) { Console.WriteLine(" ** e04ya returned with ifail = {0, 3}", ifail); } else if (ifail == 1) { Console.WriteLine(" {0}", "A parameter is outside its expected range"); } else { if (ifail == 0) { Console.WriteLine(" {0}", "1st derivatives are consistent with residual values"); } else if (ifail == 2) { Console.WriteLine(" {0}\n", "Probable error in calculation of 1st derivatives"); } Console.WriteLine(""); Console.WriteLine(" {0}", "At the test point, LSQFUN gives"); Console.WriteLine(""); Console.WriteLine("{0}\r\n", " Residuals 1st derivatives"); for (i = 1; i <= m; i++) { Console.Write(" " + "{0, 15:e3}", fvec[i - 1]); for (j = 1; j <= n; j++) { Console.Write(" " + "{0, 15:e3}", fjac[i - 1, j - 1]); } Console.WriteLine(" "); } Console.WriteLine(" "); } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void lsqfun(ref int iflag, int m, int n, double[] xc, double[] fvec, double[,] fjac) { // Routine to evaluate the residuals and their 1st derivatives double denom, dummy; int i = 0; for (i = 1; i <= m; i++) { denom = xc[1] * t[i - 1, 1] + xc[2] * t[i - 1, 2]; if (iflag != 1) { fvec[i - 1] = xc[0] + t[i - 1, 0] / denom - y[i - 1]; } if (iflag != 0) { fjac[i - 1, 0] = 1.00e0; dummy = -1.00e0 / (denom * denom); fjac[i - 1, 1] = t[i - 1, 0] * t[i - 1, 1] * dummy; fjac[i - 1, 2] = t[i - 1, 0] * t[i - 1, 2] * dummy; } } } } }