// e04hy Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04HYE { 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/e04hye.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.E04HY_LSFUN2 lsfun3E04HY = new E04.E04HY_LSFUN2(lsfun3); E04.E04HY_LSHES2 lshes3E04HY = new E04.E04HY_LSHES2(lshes3); try { DataReader sr = new DataReader(datafile); double fsumsq = 0.0; int i, ifail, j, k; int n = 3; int lw = 8 * n + 2 * n * n + 2 * m * n + 3 * m; double[,] t = new double[m, nt]; double[] w = new double[lw]; double[] x = new double[n]; double[] y = new double[m]; Console.WriteLine("e04hy 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.e04hy(m, n, lsfun3E04HY, lshes3E04HY, x, out fsumsq, out ifail); // if (ifail < 0) { Console.WriteLine(""); Console.WriteLine(" ** e04hy 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 lsfun3(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]; } } // public static void lshes3(int m, int n, double[] fvec, double[] xc, double[] b) { // Routine to compute the lower triangle of the matrix B // (stored by rows in the array B). double dummy, sum22, sum32, sum33; int i, k; b[0] = 0.00e0; b[1] = 0.00e0; sum22 = 0.00e0; sum32 = 0.00e0; sum33 = 0.00e0; k = m; for (i = 1; i <= m; i++) { dummy = 2.00e0 * ruser[k + 1 - 1] / Math.Pow((xc[1] * ruser[k + 2 - 1] + xc[2] * ruser[k + 3 - 1]), 3); sum22 = sum22 + fvec[i - 1] * dummy * (ruser[k + 2 - 1]) * (ruser[k + 2 - 1]); sum32 = sum32 + fvec[i - 1] * dummy * ruser[k + 2 - 1] * ruser[k + 3 - 1]; sum33 = sum33 + fvec[i - 1] * dummy * (ruser[k + 3 - 1]) * (ruser[k + 3 - 1]); k = k + iuser[0]; } b[2] = sum22; b[3] = 0.00e0; b[4] = sum32; b[5] = sum33; } } }