// c05qb Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class C05QBE { static void Main(String[] args) { StartExample(); } public static void StartExample() { try { int n = 9; // C05.C05NB_FCN fcnC05NB = new C05.C05NB_FCN(fcn); C05.C05QB_FCN fcnC05QB = new C05.C05QB_FCN(fcn); double fnorm, xtol; int i, j; double[] fvec = new double[n]; double[] x = new double[n]; int ifail; Console.WriteLine("c05qb Example Program Results"); Console.WriteLine(""); // The following starting values provide a rough solution. for (j = 1; j <= n; j++) { x[j - 1] = -1.00e0; } xtol = Math.Sqrt(X02.x02aj()); // //C05.c05nb(fcnC05NB, n, x, fvec, xtol, out ifail); C05.c05qb(fcnC05QB, n, x, fvec, xtol, out ifail); // if (ifail == 0) { fnorm = F06.f06ej(n, fvec, 1, out ifail); Console.WriteLine(" {0}{1,12:e4}", "Final 2-norm of the residuals =", fnorm); Console.WriteLine(""); Console.WriteLine(" {0}", "Final approximate solution"); Console.WriteLine(""); for (j = 1; j <= n; j++) { Console.Write(" {0,10:f4}{1}", x[j - 1], j % 3 == 0 ? "\n" : ""); } Console.WriteLine(""); } else if (ifail < 0) { Console.WriteLine(""); Console.WriteLine("** c05qb failed with ifail = {0,5}", ifail); } else { Console.WriteLine(" {0}{1,5}", "ifail = ", ifail); if (ifail > 1) { Console.WriteLine(""); Console.WriteLine(" {0}", "Approximate solution"); Console.WriteLine(""); for (i = 1; i <= n; i++) { Console.Write(" {0}", x[i - 1]); } Console.WriteLine(""); } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } Console.WriteLine(""); } // public static void fcn(int n, double[] x, double[] fvec, ref int iflag) { const double one = 1.00e0; const double two = 2.00e0; const double three = 3.00e0; for (int k = 1; k <= n; k++) { fvec[k - 1] = (three - two * x[k - 1]) * x[k - 1] + one; if (k > 1) { fvec[k - 1] = fvec[k - 1] - x[k - 2]; } if (k < n) { fvec[k - 1] = fvec[k - 1] - two * x[k]; } } } } }