// c05rd Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class C05RDE { static void Main(String[] args) { StartExample(); } public static void StartExample() { try { const int n=9; const double zero=0.00e0; const double one=1.00e0; const double two=2.00e0; const double three=3.00e0; const double four=4.00e0; double factor, fnorm, xtol=0.0; int icount=0, irevcm=0, j=0, k=0, mode; 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("c05rd 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; } mode = 2; factor = 100.00e0; // // C05.c05pdCommunications communications = new C05.c05pdCommunications(n); C05.c05rdCommunications communications = new C05.c05rdCommunications(n); do { /*C05.c05pd(ref irevcm, n, x, fvec, fjac, xtol, diag, mode, factor, r, qtf, communications, out ifail );*/ C05.c05rd(ref irevcm, n, x, fvec, fjac, xtol, mode, diag, factor, 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 current 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]; } } } else if (irevcm == 3) { // Evaluate Jacobian at current point for (k = 1 ; k <= n ; k++) { for (j = 1 ; j <= n ; j++) { fjac[k - 1 , j - 1] = zero; } fjac[k - 1 , k - 1] = three - four * x[k - 1]; if (k != 1) { fjac[k - 1 , k - 2] = -one; } if (k != n) { fjac[k - 1 , k] = -two; } } } } 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("** c05rd 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], j%3==0?"\n":""); } Console.WriteLine(""); } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } Console.WriteLine(""); } } }