// e04hd Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04HDE { static void Main(String[] args) { StartExample(); } public static void StartExample() { E04.E04HC_FUNCT functE04HC = new E04.E04HC_FUNCT(funct); E04.E04HD_FUNCT functE04HD = new E04.E04HD_FUNCT(funct); E04.E04HD_H hE04HD = new E04.E04HD_H(h); try { double f = 0.0; int i, ifail, j, k; int n = 4; int lh = n * (n - 1) / 2; int lw = 5 * n; double[] g = new double[n]; double[] hesd = new double[n]; double[] hesl = new double[lh]; double[] w = new double[lw]; double[] x = new double[n]; int[] iw = new int[1]; Console.WriteLine("e04hd Example Program Results"); // Set up an arbitrary point at which to check the derivatives x[0] = 1.460e0; x[1] = -0.820e0; x[2] = 0.570e0; x[3] = 1.210e0; Console.WriteLine(""); Console.WriteLine(" {0}", "The test point is"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 9:f4}", x[j - 1]); } Console.WriteLine(" "); // Check the 1st derivatives // E04.e04hc(n, functE04HC, x, out f, g, out ifail); if (ifail != 0) { Console.WriteLine(""); Console.WriteLine(" ** e04hc returned with ifail = {0, 3}", ifail); goto L40; } // // Check the 2nd derivatives E04.e04hd(n, functE04HD, hE04HD, x, g, hesl, hesd, out ifail); // Console.WriteLine(""); if (ifail == 1) { Console.WriteLine(" {0}", "A parameter is outside its expected range"); } else if (ifail >= 0) { if (ifail == 0) { Console.WriteLine(" {0}", "2nd derivatives are consistent with 1st derivatives"); } else if (ifail == 2) { Console.WriteLine(" {0}", "Probable error in calculation of 2nd derivatives"); } Console.WriteLine(""); Console.WriteLine(" {0}{1,12:e4}", "At the test point, FUNCT gives the function value", f); Console.WriteLine(" {0}", "and the 1st derivatives"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 12:e3}", g[j - 1]); } Console.WriteLine(" "); Console.WriteLine(""); Console.WriteLine(" {0}", "H gives the lower triangle of the Hessian matrix"); Console.WriteLine(" {0,12:e3}", hesd[0]); k = 1; for (i = 2; i <= n; i++) { for (j = k; j <= k + i - 2; j++) { Console.Write(" " + " {0, 12:e3}", hesl[j - 1]); } Console.Write(" " + " {0, 12:e3}", hesd[i - 1]); Console.WriteLine(" "); k = k + i - 1; } } else { if (ifail < 0) { Console.WriteLine(" IFLAG was set to {0,5}in FUNCT or H", ifail); } } // L40: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void funct(ref int iflag, int n, double[] xc, ref double fc, double[] gc) { // Routine to evaluate objective function and its 1st derivatives. fc = ((xc[0] + 10.00e0 * xc[1])) * ((xc[0] + 10.00e0 * xc[1])) + 5.00e0 * ((xc[2] - xc[3])) * ((xc[2] - xc[3])) + Math.Pow((xc[1] - 2.00e0 * xc[2]), 4) + 10.00e0 * Math.Pow((xc[0] - xc[3]), 4); gc[0] = 2.00e0 * (xc[0] + 10.00e0 * xc[1]) + 40.00e0 * Math.Pow((xc[0] - xc[3]), 3); gc[1] = 20.00e0 * (xc[0] + 10.00e0 * xc[1]) + 4.00e0 * Math.Pow((xc[1] - 2.00e0 * xc[2]), 3); gc[2] = 10.00e0 * (xc[2] - xc[3]) - 8.00e0 * Math.Pow((xc[1] - 2.00e0 * xc[2]), 3); gc[3] = 10.00e0 * (xc[3] - xc[2]) - 40.00e0 * Math.Pow((xc[0] - xc[3]), 3); } // public static void h(ref int iflag, int n, double[] xc, double[] fhesl, double[] fhesd) { // Routine to evaluate 2nd derivatives fhesd[0] = 2.00e0 + 120.00e0 * ((xc[0] - xc[3])) * ((xc[0] - xc[3])); fhesd[1] = 200.00e0 + 12.00e0 * ((xc[1] - 2.00e0 * xc[2])) * ((xc[1] - 2.00e0 * xc[2])); fhesd[2] = 10.00e0 + 48.00e0 * ((xc[1] - 2.00e0 * xc[2])) * ((xc[1] - 2.00e0 * xc[2])); fhesd[3] = 10.00e0 + 120.00e0 * ((xc[0] - xc[3])) * ((xc[0] - xc[3])); fhesl[0] = 20.00e0; fhesl[1] = 0.00e0; fhesl[2] = -24.00e0 * ((xc[1] - 2.00e0 * xc[2])) * ((xc[1] - 2.00e0 * xc[2])); fhesl[3] = -120.00e0 * ((xc[0] - xc[3])) * ((xc[0] - xc[3])); fhesl[4] = 0.00e0; fhesl[5] = -10.00e0; } } }