Пример #1
0
int my_main()
{
   {
      // Get the data
      ColumnVector X(6);
      ColumnVector Y(6);
      X << 1   << 2   <<  3   <<  4   <<  6   <<  8;
      Y << 3.2 << 7.9 << 11.1 << 14.5 << 16.7 << 18.3;


      // Do the fit
      Model_3pe model(X);                // the model object
      NonLinearLeastSquares NLLS(model); // the non-linear least squares
                                         // object
      ColumnVector Para(3);              // for the parameters
      Para << 9 << -6 << .5;             // trial values of parameters
      cout << "Fitting parameters\n";
      NLLS.Fit(Y,Para);                  // do the fit

      // Inspect the results
      ColumnVector SE;                   // for the standard errors
      NLLS.GetStandardErrors(SE);
      cout << "\n\nEstimates and standard errors\n" <<
         setw(10) << setprecision(2) << (Para | SE) << endl;
      Real ResidualSD = sqrt(NLLS.ResidualVariance());
      cout << "\nResidual s.d. = " << setw(10) << setprecision(2) <<
         ResidualSD << endl;
      SymmetricMatrix Correlations;
      NLLS.GetCorrelations(Correlations);
      cout << "\nCorrelationMatrix\n" <<
         setw(10) << setprecision(2) << Correlations << endl;
      ColumnVector Residuals;
      NLLS.GetResiduals(Residuals);
      DiagonalMatrix Hat;
      NLLS.GetHatDiagonal(Hat);
      cout << "\nX, Y, Residual, Hat\n" << setw(10) << setprecision(2) <<
         (X | Y | Residuals | Hat.as_column()) << endl;
      // recover var/cov matrix
      SymmetricMatrix D;
      D << SE.as_diagonal() * Correlations * SE.as_diagonal();
      cout << "\nVar/cov\n" << setw(14) << setprecision(4) << D << endl;
   }

#ifdef DO_FREE_CHECK
   FreeCheck::Status();
#endif
 
   return 0;
}
Пример #2
0
int my_main()                  // called by main()
{
   Tracer tr("my_main ");      // for tracking exceptions
   
   int n = 7;                 // this is the order we will work with
   int i, j;

   // declare a matrix
   SymmetricMatrix H(n);
   
   // load values for Hilbert matrix
   for (i = 1; i <= n; ++i) for (j = 1; j <= i; ++j)
      H(i, j) = 1.0 / (i + j - 1);

   // print the matrix
   cout << "SymmetricMatrix H" << endl;
   cout << setw(10) << setprecision(7) << H << endl;
   
   // calculate its eigenvalues and eigenvectors and print them
   Matrix U; DiagonalMatrix D;
   eigenvalues(H, D, U);
   cout << "Eigenvalues of H" << endl;
   cout << setw(17) << setprecision(14) << D.as_column() << endl;
   cout << "Eigenvector matrix, U" << endl;
   cout << setw(10) << setprecision(7) << U << endl;

   // check orthogonality
   cout << "U * U.t() (should be near identity)" << endl;   
   cout << setw(10) << setprecision(7) << (U * U.t()) << endl;
   
   // check decomposition
   cout << "U * D * U.t() (should be near H)" << endl;   
   cout << setw(10) << setprecision(7) << (U * D * U.t()) << endl;
   
   return 0;
}