// e04hc Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04HCE { static void Main(String[] args) { StartExample(); } public static void StartExample() { E04.E04HC_FUNCT functE04HC = new E04.E04HC_FUNCT(funct); try { double f = 0.0; int ifail, j; int n = 4; int lw = 3 * n; double[] g = new double[n]; double[] w = new double[lw]; double[] x = new double[n]; int[] iw = new int[1]; Console.WriteLine("e04hc Example Program Results"); // Set up an arbitrary point at which to check the 1st derivatives x[0] = 1.460e0; x[1] = -0.820e0; x[2] = 0.570e0; x[3] = 1.210e0; Console.WriteLine(""); Console.Write(" {0}", "The test point is"); for (j = 1; j <= n; j++) { Console.WriteLine(" " + " {0, 10:f4}", x[j - 1]); } Console.WriteLine(" "); // E04.e04hc(n, functE04HC, x, out f, g, 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}", "1st derivatives are consistent with function values"); } else { Console.WriteLine(" {0}", "Probable error in calculation of 1st 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(" "); } else { if (ifail < 0) { Console.WriteLine(" {0}{1,3}{2}", "IFLAG was set to ", ifail, "in FUNCT"); } } // } 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 = 0.0; if (iflag != 1) { 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); } if (iflag != 0) { 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); } } } }