a b/ApproxLib.def
1
DEFINITION MODULE ApproxLib;
2
3
  (*------------------------------------------------------------------------*)
4
  (* Verschiedene Routinen um Polynme oder Rationale Funktionen an eine     *)
5
  (* Funktion oder einen Datensatz anzupassen.                              *)
6
  (* Module for fitting polynomals and splines to data sets                 *)
7
  (*------------------------------------------------------------------------*)
8
  (* Implementation : Michael Riedl                                         *)
9
  (* Licence        : GNU Lesser General Public License (LGPL)              *)
10
  (*------------------------------------------------------------------------*)
11
12
  (* $Id: ApproxLib.def,v 1.4 2018/04/09 20:48:43 mriedl Exp mriedl $ *)
13
14
FROM LMathLib IMPORT Funktion1;
15
16
PROCEDURE Ausgleichsgerade(VAR A,B   : LONGREAL;
17
                           VAR dA,dB : LONGREAL;
18
                           VAR Xq,Yq : LONGREAL;
19
                           VAR R     : LONGREAL;
20
                               dim   : CARDINAL;
21
                               T     : LONGREAL;
22
                           VAR X,Y   : ARRAY OF LONGREAL);
23
24
          (*----------------------------------------------------------------*)
25
          (* Berechnet eine Ausgleichsgerade durch X,Y nach der Methode     *)
26
          (* der kleinsten Fehlerquadratsumme.                              *)
27
          (*                                                                *)
28
          (*   A,B     : f(x) = Ax + B                                      *)
29
          (*                                                                *)
30
          (*   R       : Korellationsfaktor.                                *)
31
          (*   T       : Student-Faktor                                     *)
32
          (*   Xq,Yq   : Mittelwerte von X,Y.                               *)
33
          (*----------------------------------------------------------------*)
34
35
CONST FitStraightLine = Ausgleichsgerade; (* For english users ... *)
36
37
          (*----------------------------------------------------------------*)
38
          (* Calculates the coefficients of a least square fitted straigt   *)
39
          (* through the data provided in X,Y                               *)
40
          (*                                                                *)
41
          (*   A,B     : f(x) = Ax + B                                      *)
42
          (*                                                                *)
43
          (*   R       : Corellationsfactor.                                *)
44
          (*   T       : Student-Faktor                                     *)
45
          (*   Xq,Yq   : Mean values of X,Y                                 *)
46
          (*----------------------------------------------------------------*)
47
48
PROCEDURE TschebKoeff(    U,O    : LONGREAL;
49
                      VAR TKoeff : ARRAY OF LONGREAL;
50
                          Grad   : CARDINAL;
51
                          Funkt  : Funktion1);
52
53
          (*----------------------------------------------------------------*)
54
          (* Berechnet die Tschebytscheffkoeffizienten TKoeff einer T-Ent-  *)
55
          (* wicklung des Grades Grad f"ur die Funktion Funkt im Intervall  *)
56
          (* [U,O].                                                         *)
57
          (*----------------------------------------------------------------*)
58
59
PROCEDURE PolyKoeff(VAR PKoeff : ARRAY OF LONGREAL; (* Polynomkoeffizienten *)
60
                    VAR TKoeff : ARRAY OF LONGREAL; (* Tschebytscheffkoeff. *)
61
                        U,O    : LONGREAL;          (* Intervall d. Polynom *)
62
                        Grad   : CARDINAL);
63
64
          (*----------------------------------------------------------------*)
65
          (* Transformiert die Koeffizienten einer Tschebytscheffentwick-   *)
66
          (* lung im Intervall [-1,1] in die entsprechenden Polynomkoef-    *)
67
          (* fizienten f"ur das Intervall [U,O] mit                         *)
68
          (*   f(x) = \sum_{i=0}^Grad PKoeff_i x^i                          *)
69
          (*----------------------------------------------------------------*)
70
71
PROCEDURE RatKoeff(    Funkt  : Funktion1;
72
                       U,O    : LONGREAL;
73
                   VAR Ak,Bk  : ARRAY OF LONGREAL;
74
                       n,m    : CARDINAL);
75
76
          (*----------------------------------------------------------------*)
77
          (* Berechnet f"ur das Intervall [U,O] der Eingabefunktion         *)
78
          (* Funkt(x) die Koeffizienten der rationalen Funktion             *)
79
          (* R^{n,m}(x) = {\sum_{k=0}^n A_k x^k                             *)
80
          (*                   \over                                        *)
81
          (*               1.0 + \sum_{k=1}^m B_k x^k}                      *)
82
          (* die die Funkt(x) im quadratischen Mittel bestapproximiert.     *)
83
          (*----------------------------------------------------------------*)
84
85
PROCEDURE FitTscheb(dim    : CARDINAL;
86
                    Grad   : CARDINAL;
87
                VAR X,Y    : ARRAY OF LONGREAL;
88
                VAR TKoeff : ARRAY OF LONGREAL;
89
                VAR U,O    : LONGREAL;
90
                VAR StdAbw : LONGREAL;
91
                VAR IFehl  : CARDINAL);
92
93
          (*----------------------------------------------------------------*)
94
          (* Berechnet die Entwicklungkoeffizienten TKoeff einer            *)
95
          (* Tschebytscheffentwicklung nach der Methode der minimalen       *)
96
          (* Fehlerquadratsumme als Anpassung an die Datenpaare X,Y         *)
97
          (*                                                                *)
98
          (* dim    : Anzahl der Datenpaare (X,Y)                           *)
99
          (* Grad   : Grad der T-Entwicklung                                *)
100
          (* U,O    : Intervall der T-Entwicklung (R"uckgabeparameter !)    *)
101
          (* IFehl  : Siehe PROCEDURRE PolyFit.                             *)
102
          (*----------------------------------------------------------------*)
103
104
PROCEDURE FitPoly(    dim     : CARDINAL;  (* Anzahl Datenpaare X,Y      *)
105
                  VAR X       : ARRAY OF LONGREAL;
106
                  VAR Y       : ARRAY OF LONGREAL;
107
                      Grad    : CARDINAL;  (* Grad des Polynoms          *)
108
                  VAR PKoeff  : ARRAY OF LONGREAL;(* Koeff. des Polynoms *)
109
                  VAR StdAbw  : LONGREAL;
110
                  VAR IFehl   : CARDINAL);
111
112
          (*----------------------------------------------------------------*)
113
          (* Fittet ein Polynom des Grades Grad an die Datenpaare  (X,Y)    *)
114
          (* nach der Methode der minimalen Fehlerquadratsumme.             *)
115
          (*                                                                *)
116
          (* IFehl :                                                        *)
117
          (*  1 : Anzahl der Datenpunkte kleiner zwei.                      *)
118
          (*  2 : Anzahl der Datenpunkte zu gro3.                           *)
119
          (*  3 : Grad des Polynoms kleiner eins.                           *)
120
          (*  4 : Grad des Polynoms gr"o\3er als die Anzahl der             *)
121
          (*      Datenpunkte.                                              *)
122
          (*  5 : Fehlergleichungssystem zu gro\3.                          *)
123
          (*  6 : Keine L"osung gefunden.                                   *)
124
          (*----------------------------------------------------------------*)
125
126
PROCEDURE FitRat(VAR X,Y    : ARRAY OF LONGREAL;  (* Datenpunkte        *)
127
                     dim    : CARDINAL;           (* Anzahl Datenpunkte *)
128
                 VAR Ak,Bk  : ARRAY OF LONGREAL;
129
                     n,m    : CARDINAL;
130
                 VAR StdAbw : LONGREAL);          (* Standartabweichung *)
131
132
          (*----------------------------------------------------------------*)
133
          (* Berechent die Koeffizenten einer rationalen Funktion           *)
134
          (* R^{n,m}(x) = {\sum_{k=0}^n A_k x^k                             *)
135
          (*                   \over                                        *)
136
          (*               1.0 + \sum_{k=1}^m B_k x^k}                      *)
137
          (* die die Datenpunkt X,Y im Sinne der minimierten Fehler-        *)
138
          (* quadratsumme approximiert.                                     *)
139
          (* R_i = Y_i - Q^{m,n}(x) (Residuenvektor)                        *)
140
          (*----------------------------------------------------------------*)
141
142
PROCEDURE SplineKoeff(VAR X : ARRAY OF LONGREAL;
143
                      VAR Y : ARRAY OF LONGREAL;
144
                      VAR B : ARRAY OF LONGREAL;
145
                      VAR C : ARRAY OF LONGREAL;
146
                      VAR D : ARRAY OF LONGREAL;
147
                          N : CARDINAL);
148
149
          (*----------------------------------------------------------------*)
150
          (* Berechnet die kubischen Splinkoeffizeienten der in X,Y ueber-  *)
151
          (* gebenen Wertepaare in den Feldern B,C und D. Die Y[i] werden   *)
152
          (* als der konstanter Teil des Spline-Polynoms behandelt, d.h.    *)
153
          (*                                                                *)
154
          (*   $ f(x) = Y_i + B_i x + C_i x^2 + D x^3 $                     *)
155
          (*                                                                *)
156
          (* Der Parameter N gibt die Anzahl der Datenpunkte in X,Y an.     *)
157
          (*                                                                *)
158
          (* Calculates the coefficients of the cubic spline interpolation  *)
159
          (* through the N data points provided in the arrays X and Y.      *)
160
          (* The coefficients are returned in arrays B,C and D. The values  *)
161
          (* of Y form the constant part of the polinominals of degree 3    *)
162
          (*                                                                *)
163
          (* Ref.: Forsythe, George;  Malcolm, Michael; Moler, Cleve;       *)
164
          (*       "Computer Methods for Mathematical Computations",        *)
165
          (*       Prentice-Hall, Upper Saddle River, US (1977)             *)
166
          (*----------------------------------------------------------------*)
167
 
168
PROCEDURE EvalSpline(    x : LONGREAL;
169
                     VAR X : ARRAY OF LONGREAL;
170
                     VAR Y : ARRAY OF LONGREAL;
171
                     VAR B : ARRAY OF LONGREAL;
172
                     VAR C : ARRAY OF LONGREAL;
173
                     VAR D : ARRAY OF LONGREAL;
174
                         N : CARDINAL) : LONGREAL;
175
176
          (*----------------------------------------------------------------*)
177
          (* Auswerten einer Spline-Interpolation am Punkt x [X_1<=x<=X_N]  *)
178
          (* Die Koeffizienten B,C und D koennen z.B. mit der Prozedur      *)
179
          (* SplineKoeff berechnet werden. Die Y[i] werden als der          *)
180
          (* konstanter Teil des Spline-Polynoms behandelt.                 *)
181
          (*                                                                *)
182
          (*   $ f(x) = Y_i + B_i x + C_i x^2 + D x^3 $                     *)
183
          (*                                                                *)
184
          (* Evaluate the cubic spline interpolation at point x where       *)
185
          (*                                                                *)
186
          (*    X[i] <= x <= X[i+1] , i = 0..N-1                            *)
187
          (*                                                                *)
188
          (* The spline coefficients B,C and D should have been calculated  *)
189
          (* by procedure "SplineKoeff".                                    *)
190
          (*                                                                *)
191
          (* Ref.: See SplineKoeff                                          *)
192
          (*----------------------------------------------------------------*)
193
194
PROCEDURE EvalSplineDerivative(    x     : LONGREAL;
195
                               VAR X     : ARRAY OF LONGREAL;
196
                               VAR Y     : ARRAY OF LONGREAL;
197
                               VAR B     : ARRAY OF LONGREAL;
198
                               VAR C     : ARRAY OF LONGREAL;
199
                               VAR D     : ARRAY OF LONGREAL;
200
                                   N     : CARDINAL;
201
                               VAR y     : LONGREAL;
202
                               VAR dy    : LONGREAL;
203
                               VAR d2y   : LONGREAL;
204
                               VAR iFehl : INTEGER);
205
206
          (*----------------------------------------------------------------*)
207
          (* Auswerten einer Spline-Interpolation am Punkt x [X_1<=x<=X_N]  *)
208
          (* Es wird in y der Interpolationswert zurueckgegeben, in dy die  *)
209
          (* erste Ableitung am Punkt x und in d2y die zweite Ableitung.    *)
210
          (* iFehl = 1 wenn x ausserhalb des Wertebereichs liegen sollte.   *)
211
          (*                                                                *)
212
          (* Calculates the spline-interpolation as in function EvalSpline  *)
213
          (* and returns the value in y. In dy the first and in d2y the     *)
214
          (* second derivative at point x are returend as well.             *)
215
          (* iFehl = 1 if x is outside of the interval [X_1 ... X_N]        *)
216
          (*----------------------------------------------------------------*)
217
218
PROCEDURE Spline(VAR Xa,Ya : ARRAY OF LONGREAL; (* Eingabevektoren         *)
219
                     N     : CARDINAL;          (* Anzahl der Eingabewerte *)
220
                     X1,Xm : LONGREAL;  
221
                 VAR Xn,Yn : ARRAY OF LONGREAL; (* Ausgabevektor           *)
222
                     M     : CARDINAL);         (* Anzahl Splinepunkte     *)
223
224
          (*----------------------------------------------------------------*)
225
          (* Berechnet aus den Werten in Xa,Ya kubisch splineinterpolierte  *)
226
          (* Werte in Xn,Yn im Bereich X1,XM.                               *)
227
          (*                                                                *)
228
          (* Xa,Ya     : Vorgegebene Datenpunte / original data points      *)
229
          (* N         : Anzahl der Datenpunkte / number of data points     *)
230
          (* X1,Xm     : Bereich, in dem die berechneten Spline-Punkte      *)
231
          (*             liegen sollen / range for the new values           *)
232
          (* Xn,Yn     : Splineinterpolierte Wertepaare / new data points   *)
233
          (* M         : Anzahl der erzeugten Datenpunkte in den Feldern    *)
234
          (*             Xn,Yn / number of values to be calculated in Xn,Yn *)
235
          (*                                                                *)
236
          (* Calculates M cubic spline interpolated values in the range     *)
237
          (* X1 .. Xm. These interpolated values fit to the N original data *)
238
          (* pairs provided in Xa,Ya. This routine is a driver routine for  *)
239
          (* SplineKoeff and EvalSpline.                                    *)
240
          (*----------------------------------------------------------------*)
241
242
CONST MaxR    = 32; (* Max degree of nominator   polynominal A[0..MaxL-1] *)
243
      MaxL    = 32; (* Max degree of denominator polynominal B[0..MaxL-1] *)
244
245
PROCEDURE RatTsch1(    l,r    : CARDINAL;
246
                       ap,ep  : LONGREAL;
247
                       eps    : LONGREAL;
248
                       Fct    : Funktion1;
249
                       W      : Funktion1;
250
                   VAR A,B    : ARRAY OF LONGREAL;
251
                   VAR etabar : LONGREAL;
252
                   VAR iFehl  : INTEGER);
253
254
          (* ---------------------------------------------------------------*)
255
          (* RatTsch1 determines the coefficients A[0:l] and B[0:r] of the  *)
256
          (* best fit rational function                                     *)
257
          (*                                                                *)
258
          (*   ra(x) := (A[0]+A[1]*x + ... + A[l]*x**l) /                   *)
259
          (*            (B[0]+B[1]*x + ....+ B[r]*x**r)                     *)
260
          (*                                                                *)
261
          (* for the function Fct and the weight function W, which          *)
262
          (* minimizes the maximum modulus of the weighted error function   *)
263
          (*                                                                *)
264
          (*   (Fct(x) - ra(x)) * W(x)                                      *)
265
          (*                                                                *)
266
          (* on the interval [ap,ep]. Input parameters are the integer      *)
267
          (* degree l of the numerator of ra, the integer degree r of the   *)
268
          (* denominator of ra, the real parameters ap and ep specifying    *)
269
          (* the left and right endpoint of the approximation interval,     *)
270
          (* respectively, the real procedure Fct giving the function to be *)
271
          (* approximated and the weight function W, also of type real      *)
272
          (* procedure. Fct must be continuous, W continuous and positive   *)
273
          (* on [ap,ep]. The real input parameter eps should be 10^(-t+1)   *)
274
          (* if t is the number of decimal digits in the mantissa of the    *)
275
          (* arithmetic used. Output parameters are the real arrays         *)
276
          (*                                                                *)
277
          (*   A[0:l], B[0:r] (with B[0]= 1),                               *)
278
          (*                                                                *)
279
          (* the real etabar with the property                              *)
280
          (*                                                                *)
281
          (*   abs(etabar) = max(abs(Fct(x) - ra(x))*W(x) : ap <= x <= ep). *)
282
          (*                                                                *)
283
          (* The integer iFehl specifies, whether the approximation was     *)
284
          (* successful (iFehl=0) or not (iFehl=1,2,3,4,5): if iFehl=1,     *)
285
          (* then it was impossible to obtain a sufficiently accurate ra(x) *)
286
          (* within 10 iterations. The values iFehl=2,3 or 4, indicate the  *)
287
          (* presence of a pole of the approximant ra(x) within [ap,ep].    *)
288
          (* If iFehl=5 then the rational interpolation failed              *)
289
          (*                                                                *)
290
          (*   ==> l,r    : Degree of rational approximation                *)
291
          (*                See also definition of constans MaxR and MaxL   *)
292
          (*   ==> ap,ep  : Anfangspunkt,Endpunkt (start and end point)     *)
293
          (*   ==> eps    : see text                                        *)
294
          (*   ==> Fct    : Function to be approximated                     *)
295
          (*   ==> W      : Weight function, if unsure us w(x) = 1          *)
296
          (*  <==  A,B    : Computes coefficients of the rational approx.   *)
297
          (*  <==  etabar : see text                                        *)
298
          (*  <==  iFehl  : Error indicator                                 *)
299
          (*                  0 : Approximation was successful              *)
300
          (*                  1 : The number of iterations admitted by the  *)
301
          (*                      limit ito dit not suffice to complete the *)
302
          (*                      Remes-Algorithm. Increasing the number    *)
303
          (*                      ito might help.                           *)
304
          (*                  2 : A rational function with a pole has       *)
305
          (*                      occured - deteted by hitting a point in   *)
306
          (*                      the search procedure where error function *)
307
          (*                      > 10**6 or rational function > 10**20     *)
308
          (*                  3 : A pole of a rational function had been    *)
309
          (*                      detected - denominator changes sign       *)
310
          (*                      within the interval                       *)
311
          (*                  4 : Case 1 and 2 or 3 are combined            *)
312
          (*                  5 : Rational interpolation fails              *)
313
          (*                                                                *)
314
          (*  HINT: If iFehl = -1 on ENTRY the nuber of iterations and the  *)
315
          (*        return code of iFehl are printed to the console at the  *)
316
          (*        end of the routine.                                     *)
317
          (*                                                                *)
318
          (* This procedure is a Modula-2 translation of the Algol60        *)
319
          (* procedure rattsch1 - a Algol60 version is available as well    *)
320
          (*                                                                *)
321
          (* Ref: Werner, H; Stoer, J.; Bommas, W.; "Rational Chebyshev     *)
322
          (*      Approximation", Handbook Series Methods of Approximation, *)
323
          (*      Numerische Mathematik, Springer, Berlin (DE) (1967)       *)
324
          (*                                                                *)
325
          (* For L2 approximations the procedure ApproxLib.RatKoeff can     *)
326
          (* be used.                                                       *)
327
          (* ---------------------------------------------------------------*)
328
329
END ApproxLib.