// e04jc Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04JCE { public static int Main(string[] args) { StartExample(); return 0; } public static void StartExample() { E04.E04JC_OBJFUN objfunE04JC = new E04.E04JC_OBJFUN(objfun); E04.E04JC_MONFUN e04jcpE04JC = new E04.E04JC_MONFUN(E04.e04jcp); try { double f, infbnd, rhobeg, rhoend; int ifail = 0, maxcal = 0, n = 0, nf = 0, npt = 0; Console.WriteLine(" {0}", "E04JC Example Program Results"); maxcal = 500; rhobeg = 1.0e-1; rhoend = 1.0E-6; n = 4; npt = 2 * n + 1; infbnd = Math.Pow(X02.x02al(), 0.25); /* x[2] is unconstrained, so we're going to set bl[2]) to a large negative number and bu[2] to a large positive number. */ double[] bl = { 1.0, -2.0, -infbnd, 1.0 }; double[] bu = { 3.0, 0.0, infbnd, 3.0 }; double[] x = { 3.0, -1.0, 0.0, 1.0 }; /* use monfun and pass this to e04jc instead of e04jcp to enable monitoring output */ E04.e04jc(objfunE04JC, n, npt, x, bl, bu, rhobeg, rhoend, e04jcpE04JC, maxcal, out f, out nf, out ifail); if (ifail == 0) { Console.WriteLine("Successful exit from e04jc.\nFunction value at lowest point found = {0}", f); } else { Console.WriteLine("On exit from e04jc, function value at lowest point found = {0}", f); } Console.WriteLine("The corresponding X is:"); for (int i = 0 ; i < x.Length; i++) { Console.WriteLine(x[i]); } return ; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } public static void objfun(int n, double[] x, out double f, out int inform) { inform = 0; f = Math.Pow((x[0] + 10.0 * x[1]), 2) + 5.0 * Math.Pow(x[2] - x[3], 2) + Math.Pow(x[1] - 2.0 * x[2], 4) + 10.0 * Math.Pow(x[0] - x[3], 4); } public static void monfun(int n, int nf, double[] x, double f, double rho, out int inform) { inform = 0; Console.WriteLine(""); Console.WriteLine("New rho: {0}, number of evaluations: {1}", rho, nf); Console.WriteLine("Current function value = {0}", f); Console.WriteLine("The corresponding x is"); foreach (int i in x) { Console.WriteLine(x[i]); } } } }