// c05rb Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class C05RBE { static double[] ruser = new double[5]; // static void Main(String[] args) { StartExample(); } public static void StartExample() { try { const int n = 9; //C05.C05PB_FCN fcnC05PB = new C05.C05PB_FCN(fcn); C05.C05RB_FCN fcnC05RB = new C05.C05RB_FCN(fcn); double fnorm, tol; int j = 0; double[,] fjac = new double[n, n]; double[] fvec = new double[n]; double[] x = new double[n]; int ifail; Console.WriteLine("c05rb Example Program Results"); Console.WriteLine(""); // The following starting values provide a rough solution. for (j = 1; j <= n; j++) { x[j - 1] = -1.00e0; } // Store the coefficients describing the system ruser[0] = -1.00e0; ruser[1] = 3.00e0; ruser[2] = -2.00e0; ruser[3] = -2.00e0; ruser[4] = -1.00e0; // tol = Math.Sqrt(X02.x02aj()); // // C05.c05pb(fcnC05PB, n, x, fvec, fjac, tol, out ifail); C05.c05rb(fcnC05RB, n, x, fvec, fjac, tol, 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("** c05rb failed with ifail = {0,5}", ifail); } else { Console.WriteLine(" {0}{1,5}", "ifail = ", ifail); if (ifail >= (2)) { Console.WriteLine(""); Console.WriteLine(" {0}", "Approximate solution"); Console.WriteLine(""); for (j = 1; j <= n; j++) { Console.Write(" {0, 10:f4}", x[j - 1], j%3==0?"\n":""); } 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, double[,] fjac, ref int iflag) { const double zero = 0.00e0; int j, k; if (iflag != 2) { for (k = 1; k <= n; k++) { fvec[k - 1] = (ruser[1] + ruser[2] * x[k - 1]) * x[k - 1] - ruser[4]; if (k > 1) { fvec[k - 1] = fvec[k - 1] + ruser[0] * x[k - 2]; } if (k < n) { fvec[k - 1] = fvec[k - 1] + ruser[3] * x[k]; } } } else { for (k = 1; k <= n; k++) { for (j = 1; j <= n; j++) { fjac[k - 1, j - 1] = zero; } fjac[k - 1, k - 1] = ruser[1] + 2.00e0 * ruser[2] * x[k - 1]; if (k > 1) { fjac[k - 1, k - 2] = ruser[0]; } if (k < n) { fjac[k - 1, k] = ruser[3]; } } } } } }