Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/newmat/nl_ex.cpp @ 4565

Last change on this file since 4565 was 4565, checked in by patrick, 19 years ago

orxonox/trunk: added the newmat library to the project. needs some translation in directory, temp under util/newmat. is needed by the collision detection engine to perform lin alg operations such as eigenvector decomposition. perhaps we will make our own library to do that later.

File size: 2.9 KB
Line 
1// This is an example of a non-linear least squares fit. The example
2// is from "Nonlinear estimation" by Gavin Ross (Springer,1990), p 63.
3// There are better ways of doing the fit in this case so this
4// example is just an example.
5
6// The model is E(y) = a + b exp(-kx) and there are 6 data points.
7
8#define WANT_STREAM
9#define WANT_MATH
10#include "newmatnl.h"
11#include "newmatio.h"
12
13#ifdef use_namespace
14using namespace RBD_LIBRARIES;
15#endif
16
17
18// first define the class describing the predictor function
19
20class Model_3pe : public R1_Col_I_D
21{
22   ColumnVector x_values;         // the values of "x"
23   RowVector deriv;               // values of derivatives
24public:
25   Model_3pe(const ColumnVector& X_Values)
26      : x_values(X_Values) { deriv.ReSize(3); }
27                                                                                         // load X data
28   Real operator()(int);
29   bool IsValid() { return para(3)>0; }
30                                  // require "k" > 0
31   ReturnMatrix Derivatives() { return deriv; }
32};
33
34Real Model_3pe::operator()(int i)
35{
36   Real a = para(1); Real b = para(2); Real k = para(3);
37   Real xvi = x_values(i);
38   Real e = exp(-k * xvi);
39   deriv(1) = 1.0;                    // calculate derivatives
40   deriv(2) = e;
41   deriv(3) = - b * e * xvi;
42   return a + b * e;                  // function value
43}
44
45int main()
46{
47   {
48      // Get the data
49      ColumnVector X(6);
50      ColumnVector Y(6);
51      X << 1   << 2   <<  3   <<  4   <<  6   <<  8;
52      Y << 3.2 << 7.9 << 11.1 << 14.5 << 16.7 << 18.3;
53
54
55      // Do the fit
56      Model_3pe model(X);                // the model object
57      NonLinearLeastSquares NLLS(model); // the non-linear least squares
58                                         // object
59      ColumnVector Para(3);              // for the parameters
60      Para << 9 << -6 << .5;             // trial values of parameters
61      cout << "Fitting parameters\n";
62      NLLS.Fit(Y,Para);                  // do the fit
63
64      // Inspect the results
65      ColumnVector SE;                   // for the standard errors
66      NLLS.GetStandardErrors(SE);
67      cout << "\n\nEstimates and standard errors\n" <<
68         setw(10) << setprecision(2) << (Para | SE) << endl;
69      Real ResidualSD = sqrt(NLLS.ResidualVariance());
70      cout << "\nResidual s.d. = " << setw(10) << setprecision(2) <<
71         ResidualSD << endl;
72      SymmetricMatrix Correlations;
73      NLLS.GetCorrelations(Correlations);
74      cout << "\nCorrelationMatrix\n" <<
75         setw(10) << setprecision(2) << Correlations << endl;
76      ColumnVector Residuals;
77      NLLS.GetResiduals(Residuals);
78      DiagonalMatrix Hat;
79      NLLS.GetHatDiagonal(Hat);
80      cout << "\nX, Y, Residual, Hat\n" << setw(10) << setprecision(2) <<
81         (X | Y | Residuals | Hat.AsColumn()) << endl;
82      // recover var/cov matrix
83      SymmetricMatrix D;
84      D << SE.AsDiagonal() * Correlations * SE.AsDiagonal();
85      cout << "\nVar/cov\n" << setw(14) << setprecision(4) << D << endl;
86   }
87
88#ifdef DO_FREE_CHECK
89   FreeCheck::Status();
90#endif
91 
92   return 0;
93}
Note: See TracBrowser for help on using the repository browser.