// e04ly Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04LYE { static void Main(String[] args) { StartExample(); } public static void StartExample() { E04.E04LY_FUNCT2 funct2E04LY = new E04.E04LY_FUNCT2(funct2); E04.E04LY_HESS2 hess2E04LY = new E04.E04LY_HESS2(hess2); try { double f = 0.0; int ibound, ifail, j; int n = 4; int liw = n + 2; int lw = n * (n + 7); double[] bl = new double[n]; double[] bu = new double[n]; double[] g = new double[n]; double[] ruser = new double[1]; double[] w = new double[lw]; double[] x = new double[n]; int[] iuser = new int[1]; int[] iw = new int[liw]; Console.WriteLine("e04ly Example Program Results"); x[0] = 3.00e0; x[1] = -1.00e0; x[2] = 0.00e0; x[3] = 1.00e0; ibound = 0; bl[0] = 1.00e0; bu[0] = 3.00e0; bl[1] = -2.00e0; bu[1] = 0.00e0; // // x[2] is unconstrained, so we set bl[2] to a large negative // number and bu[2] to a large positive number. // bl[2] = -1.00e6; bu[2] = 1.00e6; bl[3] = 1.00e0; bu[3] = 3.00e0; // E04.e04ly(n, ibound, funct2E04LY, hess2E04LY, bl, bu, x, out f, g, out ifail); // if (ifail < 0) { Console.WriteLine(""); Console.WriteLine(" ** e04ly 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,9:f4}", "Function value on exit is ", f); Console.Write(" " + " {0}", "at the point"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 8:f4}", x[j - 1]); } Console.WriteLine(" "); Console.WriteLine(" {0}", "The corresponding (machine dependent) gradient is"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 12:e4}", g[j - 1]); } Console.WriteLine(" "); } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void funct2(int n, double[] xc, ref double fc, double[] gc) { // Routine to evaluate objective function and its 1st derivatives. double x1, x2, x3, x4; x1 = xc[0]; x2 = xc[1]; x3 = xc[2]; x4 = xc[3]; fc = ((x1 + 10.00e0 * x2)) * ((x1 + 10.00e0 * x2)) + 5.00e0 * ((x3 - x4)) * ((x3 - x4)) + Math.Pow((x2 - 2.00e0 * x3), 4) + 10.00e0 * Math.Pow((x1 - x4), 4); gc[0] = 2.00e0 * (x1 + 10.00e0 * x2) + 40.00e0 * Math.Pow((x1 - x4), 3); gc[1] = 20.00e0 * (x1 + 10.00e0 * x2) + 4.00e0 * Math.Pow((x2 - 2.00e0 * x3), 3); gc[2] = 10.00e0 * (x3 - x4) - 8.00e0 * Math.Pow((x2 - 2.00e0 * x3), 3); gc[3] = -10.00e0 * (x3 - x4) - 40.00e0 * Math.Pow((x1 - x4), 3); } // public static void hess2(int n, double[] xc, double[] heslc, double[] hesdc) { // Routine to evaluate 2nd derivatives. double x1, x2, x3, x4; x1 = xc[0]; x2 = xc[1]; x3 = xc[2]; x4 = xc[3]; hesdc[0] = 2.00e0 + 120.00e0 * ((x1 - x4)) * ((x1 - x4)); hesdc[1] = 200.00e0 + 12.00e0 * ((x2 - 2.00e0 * x3)) * ((x2 - 2.00e0 * x3)); hesdc[2] = 10.00e0 + 48.00e0 * ((x2 - 2.00e0 * x3)) * ((x2 - 2.00e0 * x3)); hesdc[3] = 10.00e0 + 120.00e0 * ((x1 - x4)) * ((x1 - x4)); heslc[0] = 20.00e0; heslc[1] = 0.00e0; heslc[2] = -24.00e0 * ((x2 - 2.00e0 * x3)) * ((x2 - 2.00e0 * x3)); heslc[3] = -120.00e0 * ((x1 - x4)) * ((x1 - x4)); heslc[4] = 0.00e0; heslc[5] = -10.00e0; } } }