using System; using System.Runtime.InteropServices; using NagCFunctionsAPI; public class NagE04Functions { public static void nag_opt_simplex (int n, NAG_E04CCC_FUN funct, double [] x, ref double fmin, ref Nag_E04_Opt options, ref CommStruct user_comm, ref NagError fail) { NagFunctions.e04xxc(ref options); /* options.list=0; options.optim_tol=1.0e-3; options.print_level=1135; options.max_iter=100; */ /* options.outfile[0] = 'd'; options.outfile[1] = '.'; options.outfile[2] = 'o'; */ NagFunctions.e04ccc(n, funct, x, ref fmin, ref options, ref user_comm, ref fail); } public static void Main() { NAG_E04CCC_FUN objfun = new NAG_E04CCC_FUN (funct); int n=2; double [] x = new double [2]; x[0] = 0.4; x[1] = -0.8; double fmin = 0.0; Nag_E04_Opt options = new Nag_E04_Opt(); CommStruct user_comm = new CommStruct(); NagError fail = new NagError(); nag_opt_simplex(n, objfun, x, ref fmin, ref options, ref user_comm, ref fail); if (fail.code != 0) Console.WriteLine(fail.char_array); } public static void funct(int n, IntPtr xc_ptr, ref double objf, ref CommStruct comm) { double [] xc = new double[n]; Marshal.Copy( xc_ptr, xc, 0, n ); objf = Math.Exp(xc[0]) * (xc[0] * 4.0 * (xc[0] + xc[1]) + xc[1] * 2.0 * (xc[1] + 1.0) + 1.0); } }