// e04gy Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04GYE { static int m = 15; static int nt = 3; static double[] ruser = new double[m + m * nt]; static int[] iuser = new int[1]; static string datafile = "ExampleData/e04gye.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.E04GY_LSFUN2 lsfun2E04GY = new E04.E04GY_LSFUN2(lsfun2); try { DataReader sr = new DataReader(datafile); double fsumsq = 0.0; int i, ifail, j, k; int n = 3; double[,] t = new double[m, nt]; double[] x = new double[n]; double[] y = new double[m]; Console.WriteLine("e04gy 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) // iuser[0] = nt; k = m; 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); } ruser[i - 1] = y[i - 1]; for (j = 1; j <= nt; j++) { ruser[k + j - 1] = t[i - 1, j - 1]; } k = k + nt; } // x[0] = 0.50e0; x[1] = 1.00e0; x[2] = 1.50e0; // // E04.e04gy(m, n, lsfun2E04GY, x, out fsumsq, out ifail); // if (ifail < 0) { Console.WriteLine(""); Console.WriteLine(" ** e04gy 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) && (ifail != 9)) { 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, 12:f4}", x[j - 1]); } Console.WriteLine(" "); } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void lsfun2(int m, int n, double[] xc, double[] fvec, double[, ] fjac) { // Routine to evaluate the residuals and their 1st derivatives. double denom, dummy; int i, k; k = m; for (i = 1; i <= m; i++) { denom = xc[1] * ruser[k + 2 - 1] + xc[2] * ruser[k + 3 - 1]; fvec[i - 1] = xc[0] + ruser[k + 1 - 1] / denom - ruser[i - 1]; fjac[i - 1, 0] = 1.00e0; dummy = -1.00e0 / (denom * denom); fjac[i - 1, 1] = ruser[k + 1 - 1] * ruser[k + 2 - 1] * dummy; fjac[i - 1, 2] = ruser[k + 1 - 1] * ruser[k + 3 - 1] * dummy; k = k + iuser[0]; } } } }