// e04fc Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04FCE { static int m = 15; static int nt = 3; public static double[,] t = new double[m, nt]; public static double[] y = new double[m]; static string datafile = "ExampleData/e04fce.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.E04FC_LSQFUN lsqfunE04FC = new E04.E04FC_LSQFUN(lsqfun); E04.E04FC_LSQMON lsqmonE04FC = new E04.E04FC_LSQMON(lsqmon); try { DataReader sr = new DataReader(datafile); double eta, fsumsq, stepmx, xtol; int i, ifail, iprint, j, maxcal, nf, niter; int n = 3; double[,] fjac = new double[m, n]; double[] fvec = new double[m]; double[] g = new double[n]; double[] s = new double[n]; double[,] v = new double[n, n]; double[] x = new double[n]; int[] iw = new int[1]; Console.WriteLine("e04fc Example Program Results"); // Skip heading in data file sr.Reset(); // 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 <= nt; j++) { t[i - 1, j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } // * Set iprint to 1 to obtain output from lsqmon at each iteration * iprint = -1; maxcal = 400 * n; eta = 0.50e0; xtol = 10.00e0 * Math.Sqrt(X02.x02aj()); // We estimate that the minimum will be within 10 units of the // starting point stepmx = 10.00e0; // Set up the starting point x[0] = 0.50e0; x[1] = 1.00e0; x[2] = 1.50e0; // E04.e04fc(m, n, lsqfunE04FC, lsqmonE04FC, iprint, maxcal, eta, xtol, stepmx, x, out fsumsq, fvec, fjac, s, v, out niter, out nf, out ifail); // if (ifail < 0) { Console.WriteLine(""); Console.WriteLine(" ** e04fc returned with ifail = {0, 3}", ifail); } else { if (ifail != 0) { Console.WriteLine(""); Console.WriteLine(" {0}{1,3}{2}", "Error exit type", ifail, " - see method document"); } if (ifail != 1) { Console.WriteLine(""); Console.WriteLine(" {0}{1,12:f4}", "On exit, the sum of squares is", fsumsq); Console.Write(" " + " {0}", "at the point"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0}", x[j - 1]); } Console.WriteLine(" "); lsqgrd(m, n, fvec, fjac, g); Console.Write(" " + " {0}", "The estimated gradient is"); for (j = 1; j <= n; j++) { Console.WriteLine(" " + " {0}", g[j - 1]); } Console.WriteLine(" "); Console.WriteLine(" {0}", " (machine dependent)"); Console.WriteLine(" {0}\n", "and the residuals are"); for (i = 1; i <= m; i++) { Console.WriteLine(" " + " {0, 9:e1}", fvec[i - 1]); } Console.WriteLine(" "); } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); // Console.WriteLine(e.Message); } } // public static void lsqfun(ref int iflag, int m, int n, double[] xc, double[] fvec) { // Routine to evaluate the residuals int i = 0; for (i = 1; i <= m; i++) { fvec[i - 1] = xc[0] + t[i - 1, 0] / (xc[1] * t[i - 1, 1] + xc[2] * t[i - 1, 2]) - y[i - 1]; } } // public static void lsqmon(int m, int n, double[] xc, double[] fvec, double[,] fjac, double[] s, int igrade, int niter, int nf) { // Monitoring method double fsumsq, gtg; int j = 0; int ifail; double[] g = new double[3]; fsumsq = F06.f06ea(m, fvec, 1, fvec, 1, out ifail); lsqgrd(m, n, fvec, fjac, g); gtg = F06.f06ea(n, g, 1, g, 1, out ifail); Console.WriteLine(""); Console.WriteLine(" {0}", " Itn F evals SUMSQ GTG Grade"); Console.WriteLine(" {0,4} {1,5} {2,13:e5} {3,9:e1} {4,3}", niter, nf, fsumsq, gtg, igrade); Console.WriteLine(""); Console.WriteLine(" {0}", " x g Singular values"); for (j = 1; j <= n; j++) { Console.WriteLine(" {0,13:e5} {1,9:e1} {2,9:e1}", xc[j - 1], g[j - 1], s[j - 1]); } // } // public static void lsqgrd(int m, int n, double[] fvec, double[,] fjac, double[] g) { // Routine to evaluate gradient of the sum of squares double sum = 0.0; int i, j; for (j = 1; j <= n; j++) { sum = 0.00e0; for (i = 1; i <= m; i++) { sum = sum + fjac[i - 1, j - 1] * fvec[i - 1]; } g[j - 1] = sum + sum; } } } }