~ubuntu-branches/ubuntu/utopic/mricron/utopic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{ ******************************************************************
  Polynomial regression : Y = B(0) + B(1) * X + B(2) * X^2 + ...
  ****************************************************************** }

unit upolfit;

interface

uses
  utypes, ulineq;

procedure PolFit(X, Y        : PVector;
                 Lb, Ub, Deg : Integer;
                 B           : PVector;
                 V           : PMatrix);
{ ------------------------------------------------------------------
  Unweighted polynomial regression
  ------------------------------------------------------------------
  Input parameters:  X, Y   = point coordinates
                     Lb, Ub = array bounds
                     Deg    = degree of polynomial
  Output parameters: B      = regression parameters
                     V      = inverse matrix
  ------------------------------------------------------------------ }

procedure WPolFit(X, Y, S     : PVector;
                  Lb, Ub, Deg : Integer;
                  B           : PVector;
                  V           : PMatrix);
{ ------------------------------------------------------------------
  Weighted polynomial regression
  ------------------------------------------------------------------
  Additional input parameter:
  S = standard deviations of observations
  ------------------------------------------------------------------ }

implementation

procedure PolFit(X, Y        : PVector;
                 Lb, Ub, Deg : Integer;
                 B           : PVector;
                 V           : PMatrix);
var
  I, I1, J, K, D1 : Integer;
  XI, Det         : Float;

begin
  if Ub - Lb < Deg then
    begin
      SetErrCode(MatErrDim);
      Exit;
    end;

  { Initialize }
  for I := 0 to Deg do
    begin
      for J := 0 to Deg do
        V^[I]^[J] := 0.0;
      B^[I] := 0.0;
    end;

  V^[0]^[0] := Ub - Lb + 1;

  for K := Lb to Ub do
  begin
    XI := X^[K];                 { x^i }
    B^[0] := B^[0] + Y^[K];
    V^[0]^[1] := V^[0]^[1] + XI;
    B^[1] := B^[1] + XI * Y^[K];

    for I := 2 to Deg do
    begin
      XI := XI * X^[K];
      V^[0]^[I] := V^[0]^[I] + XI;   { First line of matrix: 1 --> x^d }
      B^[I] := B^[I] + XI * Y^[K];   { Constant vector: y --> x^d.y  }
    end;

    for I := 1 to Deg do
    begin
      XI := XI * X^[K];
      V^[I]^[Deg] := V^[I]^[Deg] + XI;  { Last col. of matrix: x^d --> x^2d }
    end;
  end;

  { Fill lower matrix }
  D1 := Deg - 1;
  for I := 1 to Deg do
  begin
    I1 := I - 1;
    for J := 0 to D1 do
      V^[I]^[J] := V^[I1]^[J + 1];
  end;

  { Solve system }
  LinEq(V, B, 0, Deg, Det);
end;

procedure WPolFit(X, Y, S     : PVector;
                  Lb, Ub, Deg : Integer;
                  B           : PVector;
                  V           : PMatrix);
var
  I, I1, J, K, D1 : Integer;
  W, WXI, Det     : Float;

begin
  if Ub - Lb < Deg then
    begin
      SetErrCode(MatErrDim);
      Exit;
    end;

  { Initialize }
  for I := 0 to Deg do
    begin
      for J := 0 to Deg do
        V^[I]^[J] := 0.0;
      B^[I] := 0.0;
    end;

  for K := Lb to Ub do
  begin
    if S^[K] <= 0.0 then
      begin
        SetErrCode(MatSing);
        Exit;
      end;

    W := 1.0 / Sqr(S^[K]);
    WXI := W * X^[K];                 { w.x^i }
    V^[0]^[0] := V^[0]^[0] + W;
    B^[0] := B^[0] + W * Y^[K];
    V^[0]^[1] := V^[0]^[1] + WXI;
    B^[1] := B^[1] + WXI * Y^[K];

    for I := 2 to Deg do
    begin
      WXI := WXI * X^[K];
      V^[0]^[I] := V^[0]^[I] + WXI;   { First line of matrix: w --> w.x^d }
      B^[I] := B^[I] + WXI * Y^[K];   { Constant vector: w.y --> w.x^d.y  }
    end;

    for I := 1 to Deg do
    begin
      WXI := WXI * X^[K];
      V^[I]^[Deg] := V^[I]^[Deg] + WXI;  { Last col. of matrix: w.x^d --> w.x^2d }
    end;
  end;

  { Fill lower matrix }
  D1 := Deg - 1;
  for I := 1 to Deg do
  begin
    I1 := I - 1;
    for J := 0 to D1 do
      V^[I]^[J] := V^[I1]^[J + 1];
  end;

  { Solve system }
  LinEq(V, B, 0, Deg, Det);
end;

end.