// e04dg Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04DGE { static string datafile = "ExampleData/e04dge.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.E04DG_OBJFUN objfunE04DG = new E04.E04DG_OBJFUN(objfun); try { DataReader sr = new DataReader(datafile); double objf; int i, ifail, iter, n; Console.WriteLine("e04dg Example Program Results"); // Skip heading in data file sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); double[] objgrd = new double[n]; double[] x = new double[n]; // // Read x from data file // sr.Reset(); for (i = 1; i <= n; i++) { x[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Initialise E04.e04dg // E04.e04dgOptions options = new E04.e04dgOptions(); // // Solve the problem // options.Set("List"); options.Set("Print level = 10"); E04.e04dg(n, objfunE04DG, out iter, out objf, objgrd, x, options, out ifail); // // Check for error exits // Console.WriteLine(""); if ((ifail >= 0) && (ifail < 9)) { Console.WriteLine(" ** e04dg returned with ifail = {0, 3}", ifail); Console.WriteLine(""); Console.WriteLine(" Variable Value Gradient value"); Console.WriteLine(""); for (i = 1; i <= n; i++) { Console.WriteLine(" Varbl {0,3} {1,15:g7} {2,9:g1}", i, x[i - 1], objgrd[i - 1]); } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" Final objective value = {0,15:g7}", objf); } else { if (ifail >= (9)) { Console.WriteLine(" ** An input parameter is invalid"); } else if (ifail == -399) { Console.WriteLine(" ** e04dg returned with ifail = {0, 3}", ifail); } else { Console.WriteLine(" MODE < 0 on exit from OBJFUN. Problem abandoned."); } } // // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void objfun(ref int mode, int n, double[] x, out double objf, double[] objgrd, int nstate) { // Routine to evaluate F(x) and its 1st derivatives. double one = 1.00e0; double two = 2.00e0; double four = 4.00e0; double expx1, x1, x2; x1 = x[0]; x2 = x[1]; expx1 = Math.Exp(x1); objf = expx1 * (four * (x1) * (x1) + two * (x2) * (x2) + four * x1 * x2 + two * x2 + one); // if (mode == 2) { objgrd[0] = four * expx1 * (two * x1 + x2) + objf; objgrd[1] = two * expx1 * (two * x2 + two * x1 + one); } // } } }