home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l042 / 2.ddi / CHAP9.ARC / LEASTSQR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-12-30  |  6.1 KB  |  114 lines

  1. unit LeastSqr;
  2.  
  3. {----------------------------------------------------------------------------}
  4. {-                                                                          -}
  5. {-     Turbo Pascal Numerical Methods Toolbox                               -}
  6. {-     Copyright (c) 1986, 87 by Borland International, Inc.                -}
  7. {-                                                                          -}
  8. {-  This unit provides procedures for modelling data with a function of a   -}
  9. {-  given type, given a set of data points.                                 -}
  10. {-                                                                          -}
  11. {----------------------------------------------------------------------------}
  12.  
  13. {$I Float.inc} { Determines the setting of the $N compiler directive }
  14.  
  15. interface
  16.  
  17. {$IFOPT N+}
  18. type
  19.   Float = Double; { 8 byte real, requires 8087 math chip }
  20.  
  21. const
  22.   TNNearlyZero = 1E-015;
  23. {$ELSE}
  24. type
  25.   Float = real;   { 6 byte real, no math chip required }
  26.  
  27. const
  28.   TNNearlyZero = 1E-07;
  29. {$ENDIF}
  30.  
  31.   TNRowSize = 6;                { Maximum number of terms  }
  32.                                 { in Least Squares fit     }
  33.   TNColumnSize = 50;            { Maximum number of data points  }
  34.  
  35. type
  36.   TNColumnVector = array[1..TNColumnSize] of Float;
  37.   TNRowVector = array[1..TNRowSize] of Float;
  38.   TNmatrix = array[1..TNColumnSize] of TNRowVector;
  39.   TNSquareMatrix =array[1..TNRowSize] of TNRowVector;
  40.   TNString40 = string[40];
  41.   FitType = (Expo, Fourier, Log, Poly, Power, User);
  42.  
  43. function ModuleName(Fit : FitType) : TNString40;
  44.  
  45. procedure LeastSquares(NumPoints         : integer;
  46.                    var XData             : TNColumnVector;
  47.                    var YData             : TNColumnVector;
  48.                        NumTerms          : integer;
  49.                    var Solution          : TNRowVector;
  50.                    var YFit              : TNColumnVector;
  51.                    var Residuals         : TNColumnVector;
  52.                    var StandardDeviation : Float;
  53.                    var Variance          : Float;
  54.                    var Error             : byte;
  55.                        Fit               : FitType);
  56.  
  57. {----------------------------------------------------------------------------}
  58. {-                                                                          -}
  59. {-   Input: NumPoints, XData, YData, NumTerms                               -}
  60. {-   Output: Solution, YFit, Residuals, StandardDeviation, Error            -}
  61. {-                                                                          -}
  62. {-   Purpose:  Given NumPoints data points of the form (X, Y), this         -}
  63. {-             procedure finds the least square solution of NumTerms terms  -}
  64. {-             (NumTerms <= NumPoints) to then matrix equation AC = B       -}
  65. {-             where A is a NumPoints by NumTerms matrix, B is a            -}
  66. {-             NumPoints vector and C is the least squares solution.  The   -}
  67. {-             elements of A are A[i, j] = Tj(X[i]) where Tj is the jth     -}
  68. {-             basis vector and X[i] is the X-value of the ith data point.  -}
  69. {-             The basis vectors are created by a separate include file,    -}
  70. {-             or module.  The choice of module will determine whether      -}
  71. {-             the least squares solution is a polynomial fit, trigono-     -}
  72. {-             metric fit (Fourier series), power fit (e.g. Y=ax^b, where   -}
  73. {-             b is fractional), exponential fit (e.g. Y=a-Exp(bx)), or     -}
  74. {-             logarithmic fit (e.g. Y=a-Ln(bx)).  The user may also        -}
  75. {-             create modules for other functional forms.  See the          -}
  76. {-             documentation for details.                                   -}
  77. {-                                                                          -}
  78. {- User-Defined Types: TNColumnVector = array[1..TNColumnSize] of real;     -}
  79. {-                     TNRowVector = array[1..TNRowSize] of real;           -}
  80. {-                                                                          -}
  81. {-     Global Variables: NumPoints         : integer; Number of data points -}
  82. {-                       XData             : TNColumnVector; X-value data   -}
  83. {-                       YData             : TNColumnVector; Y-value data   -}
  84. {-                       NumTerms          : integer; Number of terms in    -}
  85. {_                                                    least squares fit     -}
  86. {-                       Solution          : TNRowVector; Least squares     -}
  87. {_                                                        solution in the   -}
  88. {-                                                        given basis       -}
  89. {-                       YFit              : TNColumnVector; Y-values       -}
  90. {-                                                           predicted by   -}
  91. {-                                                          the LS solution -}
  92. {-                       Residuals         :TNColumnVector Difference       -}
  93. {-                                                      between predicted   -}
  94. {-                                                      and actual Y values -}
  95. {-                       StandardDeviation : real; Root of variance         -}
  96. {-                       Error             : byte; Indicates an error       -}
  97. {-                                                                          -}
  98. {-               Errors: 0: No errors                                       -}
  99. {-                       1: NumPoints < 2                                   -}
  100. {-                       2: NumTerms < 1                                    -}
  101. {-                       3: NumTerms > NumPoints                            -}
  102. {-                       4: solution not possible (exact reason             -}
  103. {-                          will depend upon the particular basis)          -}
  104. {-                                                                          -}
  105. {----------------------------------------------------------------------------}
  106.  
  107. implementation
  108.  
  109. {$I Least1.inc}      { Include procedure code }
  110.  
  111. {$I Least2.inc}
  112.  
  113. end. { LeastSqr }
  114.