// c05qd Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class C05QDE { static void Main(String[] args) { StartExample(); } public static void StartExample() { try { const int n=9; const double one=1.00e0; const double two=2.00e0; const double three=3.00e0; double epsfcn, factor=0.0, fnorm=0.0, xtol=0.0; int icount, irevcm, j=0, k=0, ml=0, mode=0, mu=0; double[] diag = new double[n]; double[,] fjac = new double[n, n]; double[] fvec = new double[n]; double[] qtf = new double[n]; double[] r = new double[n*(n+1)/2]; double[] x = new double[n]; int ifail; Console.WriteLine("c05qd Example Program Results"); // The following starting values provide a rough solution. for (j = 1 ; j <= n ; j++) { x[j - 1] = -1.00e0; } xtol = Math.Sqrt(X02.x02aj()); for (j = 1 ; j <= n ; j++) { diag[j - 1] = 1.00e0; } ml = 1; mu = 1; epsfcn = 0.00e0; mode = 2; factor = 100.00e0; icount = 0; irevcm = 0; // //C05.c05ndCommunications communications = new C05.c05ndCommunications(n); C05.c05qdCommunications communications = new C05.c05qdCommunications(n); do { /* C05.c05nd(ref irevcm, n, x, fvec, xtol, ml, mu, epsfcn, diag, mode , factor, fjac, r, qtf, communications, out ifail );*/ C05.c05qd(ref irevcm, n, x, fvec, xtol, ml, mu, epsfcn, mode, diag, factor, fjac, r, qtf, communications, out ifail ); // if (irevcm == 1) { icount = icount + 1; // Insert print statements here to monitor progess if desired. } else if (irevcm == 2) { // Evaluate functions at given point for (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]; } } } } while (irevcm != 0); // Console.WriteLine(""); if (ifail == 0) { fnorm = F06.f06ej(n, fvec, 1, out ifail); Console.WriteLine(" {0}{1,4}{2}{3,12:e4}","Final 2-norm of the residuals after",icount," iterations is ",fnorm); Console.WriteLine(""); Console.WriteLine(" {0}","Final approximate solution"); for (j = 1 ; j <= n ; j++) { Console.Write(" {0, 10:f4}{1}", x[j - 1], j%3==0?"\n":""); } Console.WriteLine(""); } else if (ifail == 1) { Console.WriteLine(""); Console.WriteLine("** c05qd failed with ifail = {0,5}", ifail); } else { Console.WriteLine(" {0}{1,4}","ifail =",ifail); if (ifail >= (2)) { Console.WriteLine(" {0}","Approximate solution"); for (j = 1 ; j <= n ; j++) { Console.Write(" {0, 10:f4}{1}", x[j - 1], x[j - 1], j%3==0?"\n":""); } Console.WriteLine(""); } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } Console.WriteLine(""); } } }