-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrmcfit.pas
393 lines (345 loc) · 11 KB
/
frmcfit.pas
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
unit Frmcfit;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Buttons, StdCtrls, ExtCtrls, SDL_rchart,
SDL_stringl, SDL_math2, SDL_numlab, Spin, DynamicSkinForm, SkinBoxCtrls, SkinCtrls,
Menus, spMessages, ComCtrls, SkinTabs;
type
TFrmMain = class(TForm)
Stats1: TCurveFit;
spDynamicSkinForm1: TspDynamicSkinForm;
spSkinMainMenu1: TspSkinMainMenu;
File1: TMenuItem;
spSkinMainMenuBar1: TspSkinMainMenuBar;
Exit1: TMenuItem;
Regression1: TMenuItem;
Linear1: TMenuItem;
Parabolic1: TMenuItem;
Polynomial1: TMenuItem;
Firstorder1: TMenuItem;
SecondOrder1: TMenuItem;
Thirdorder1: TMenuItem;
Fourthorder1: TMenuItem;
spSkinMessage1: TspSkinMessage;
spSkinPageControl1: TspSkinPageControl;
spSkinTabSheet1: TspSkinTabSheet;
spSkinTabSheet2: TspSkinTabSheet;
RChart1: TRChart;
RChart2: TRChart;
Stats2: TCurveFit;
procedure SBNewPntsClick(Sender: TObject);
procedure BButParabolFitClick(Sender: TObject);
procedure BButLinFitClick(Sender: TObject);
procedure SBClearClick(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure BButPolynomialClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure ResClearClick;
procedure LoadData;
procedure LoadResiduals;
private
procedure ShowSpline;
public
{ Public declarations }
end;
var
FrmMain : TFrmMain;
PolyMax : Integer;
Residuals : array[0..600,0..1] of double;
PredVal : array[0..600] of double;
implementation
uses Main, DataModFr, RegDetailsFr, SequenceViewFr;
{$R *.DFM}
const
ChartXRes = 200;
procedure TFrmMain.SBClearClick(Sender: TObject);
begin
Stats1.Init;
RChart1.ClearGraf;
RChart1.ShowGraf;
end;
procedure TFrmMain.ResClearClick;
begin
Stats2.Init;
RChart2.ClearGraf;
RChart2.ShowGraf;
end;
procedure TFrmMain.SBNewPntsClick(Sender: TObject);
begin
while (RChart1.TypeOfLastItem <> tkMarkAt) and
(RChart1.TypeOfLastItem <> tkNone) do { remove any curve from graph }
RChart1.RemoveLastItem;
end;
procedure TFrmMain.BButParabolFitClick(Sender: TObject);
var
k0, k1, k2 : double;
FitQUal : double;
xstep : double;
i, j : integer;
x : double;
rx, ry, rz : double;
begin
spSkinPageControl1.ActivePageIndex := 0;
while (RChart2.TypeOfLastItem <> tkMarkAt) and
(RChart2.TypeOfLastItem <> tkNone) do { remove any curve from graph }
RChart2.RemoveLastItem;
RChart2.ShowGraf;
while (RChart1.TypeOfLastItem <> tkMarkAt) and
(RChart1.TypeOfLastItem <> tkNone) do { remove any curve from graph }
RChart1.RemoveLastItem;
Stats1.CalcParabolFit (k0, k1, k2, FitQual);
RChart1.MoveTo (RChart1.Scale1X.RangeLow,k0+k1*RChart1.Scale1X.RangeLow+k2*sqr(RChart1.Scale1X.RangeLow));
xstep := (RChart1.Scale1X.RangeHigh-RChart1.Scale1X.RangeLow) / ChartXRes;
for i := 1 to ChartXRes do
begin
x := RChart1.Scale1X.RangeLow+i*xstep;
RChart1.DrawTo (x,k0+k1*x+k2*sqr(x));
end;
RChart1.ShowGraf;
RegDetails.spSkinStringGrid1.Cells[1,0] := 'y = k0 + k1*x + k2*sqr(x)';
RegDetails.spSkinStringGrid1.Cells[1,1] := 'k0 = '+strff(k0,1,3)+' k1 = '+strff(k1,1,3)+' k2 = '+strff(k2,1,3);
RegDetails.spSkinStringGrid1.Cells[1,2] := FloatToStrF (FitQual,ffFixed,9,6);
RegDetails.ShowModal;
for i := 0 to MaxYrs do
for j := 0 to 1 do Residuals[i,j] := 0.0;
for i := 1 to SequenceView.rzListView2.Items.Count do
begin
rz := StrToFloat (SequenceView.rzListView2.Items.Item[i-1].Caption);
rz := rz/1000.00;
rx := StrToFloat (SequenceView.rzListView2.Items.Item[i-1].Caption);
ry := StrToFloat (SequenceView.rzListView3.Items.Item[i-1].Caption);
Residuals[i,0] := rx;
Residuals[i,1] := ry - (k0 + k1*rx + k2*sqr(rx));
PredVal[i] := k0 + k1*rx + k2*sqr(rx);
end;
LoadResiduals;
end;
procedure TFrmMain.BButLinFitClick(Sender: TObject);
var
i,j : integer;
k,d : double;
FitQUal : double;
rx,ry,rz : double;
begin
spSkinPageControl1.ActivePageIndex := 0;
while (RChart2.TypeOfLastItem <> tkMarkAt) and
(RChart2.TypeOfLastItem <> tkNone) do { remove any curve from graph }
RChart2.RemoveLastItem;
RChart2.ShowGraf;
while (RChart1.TypeOfLastItem <> tkMarkAt) and
(RChart1.TypeOfLastItem <> tkNone) do { remove any curve from graph }
RChart1.RemoveLastItem;
Stats1.CalcLinFit (k, d, FitQual);
RChart1.MoveTo (RChart1.Scale1X.RangeLow,k*RChart1.Scale1X.RangeLow+d);
RChart1.DrawTo (RChart1.Scale1X.RangeHigh,k*RChart1.Scale1X.RangeHigh+d);
RChart1.ShowGraf;
RegDetails.spSkinStringGrid1.Cells[1,0] := 'y = k*x + d';
RegDetails.spSkinStringGrid1.Cells[1,1] := 'k = '+strff(k,1,3)+' d = '+strff(d,1,3);
RegDetails.spSkinStringGrid1.Cells[1,2] := FloatToStrF (FitQual,ffFixed,9,6);
RegDetails.ShowModal;
for i := 0 to MaxYrs do
for j := 0 to 1 do Residuals[i,j] := 0.0;
for i := 1 to SequenceView.rzListView2.Items.Count do
begin
rz := StrToFloat (SequenceView.rzListView2.Items.Item[i-1].Caption);
rz := rz/1000.00;
rx := StrToFloat (SequenceView.rzListView2.Items.Item[i-1].Caption);
ry := StrToFloat (SequenceView.rzListView3.Items.Item[i-1].Caption);
Residuals[i,0] := rx;
Residuals[i,1] := ry - (rx*k+d);
PredVal[i] := rx*k+d;
end;
LoadResiduals;
end;
procedure TFrmMain.FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
begin
Screen.cursor := crDefault;
end;
procedure TFrmMain.BButPolynomialClick(Sender: TObject);
var
k : array[0..MaxPolyFitOrder] of double;
FitQUal : double;
xstep : double;
i,j : integer;
x, xprod : double;
sum : double;
astr : string;
PolyOrder : byte;
rx, ry, rz : double;
Result : Boolean;
begin
spSkinPageControl1.ActivePageIndex := 0;
while (RChart2.TypeOfLastItem <> tkMarkAt) and
(RChart2.TypeOfLastItem <> tkNone) do { remove any curve from graph }
RChart2.RemoveLastItem;
RChart2.ShowGraf;
PolyOrder := TComponent(Sender).Tag;
if Stats1.NumData <=9 then
PolyMax := Stats1.NumData-1;
if PolyOrder > PolyMax then
PolyOrder := PolyMax;
if PolyOrder > PolyMax then
begin
spSkinMessage1.MessageDlg ('The maximum polynomial is '+inttostr (PolyMax)+'.', mtInformation,[mbOk], 0);
PolyOrder := PolyMax;
end;
while (RChart1.TypeOfLastItem <> tkMarkAt) and
(RChart1.TypeOfLastItem <> tkNone) do
RChart1.RemoveLastItem;
if not Result then
spSkinMessage1.MessageDlg ('Could not calculate polynomial fit.', mtInformation,[mbOk], 0)
else
begin
xstep := (RChart1.Scale1X.RangeHigh-RChart1.Scale1X.RangeLow) / ChartXRes;
x := RChart1.Scale1X.RangeLow + xstep;
xprod := 1.0;
sum := 0.0;
for j := 0 to PolyOrder do
begin
sum := sum + k[j]*Xprod;
xprod := xprod*x;
end;
RChart1.MoveTo (x,sum);
for i := 1 to ChartXRes do
begin
x := RChart1.Scale1X.RangeLow+i*xstep;
xprod := 1.0;
sum := 0.0;
for j := 0 to PolyOrder do
begin
sum := sum + k[j]*Xprod;
xprod := xprod*x;
end;
RChart1.DrawTo (x,sum);
end;
RChart1.ShowGraf;
astr := 'y = k0+k1*x';
for i := 2 to PolyOrder do
astr := astr + '+k'+intToStr(i)+'*x^'+intToStr(i)+' ';
RegDetails.spSkinStringGrid1.Cells[1,0] := astr;
astr := '';
for i := 0 to PolyOrder do
astr := astr + 'k'+intToStr(i)+'='+strff(k[i],1,3)+' ';
RegDetails.spSkinStringGrid1.Cells[1,1] := astr;
RegDetails.spSkinStringGrid1.Cells[1,2] := FloatToStrF (FitQual,ffFixed,9,6);
RegDetails.ShowModal;
for i := 0 to MaxYrs do
for j := 0 to 1 do Residuals[i,j] := 0.0;
for i := 1 to SequenceView.rzListView2.Items.Count do
begin
rz := StrToFloat(SequenceView.rzListView2.Items.Item[i-1].Caption);
rz := rz/1000.00;
rx := StrToFloat (SequenceView.rzListView2.Items.Item[i-1].Caption);
ry := StrToFloat (SequenceView.rzListView3.Items.Item[i-1].Caption);
xprod := 1;
sum := 0;
for j := 0 to PolyOrder do
begin
sum := sum + k[j]*Xprod;
xprod := xprod*rx;
end;
Residuals[i,0] := rx;
Residuals[i,1] := ry - sum;
PredVal[i] := sum;
end;
LoadResiduals;
end;
end;
procedure TFrmMain.ShowSpline;
var
FitQUal : double;
xstep : double;
i : integer;
x, y : double;
valid : boolean;
begin
while (RChart1.TypeOfLastItem <> tkMarkAt) and
(RChart1.TypeOfLastItem <> tkNone) do { remove any curve from graph }
RChart1.RemoveLastItem;
xstep := (RChart1.Scale1X.RangeHigh-RChart1.Scale1X.RangeLow) / ChartXRes;
x := Stats1.MinX;
y := Stats1.SmoothedSpline (x, FitQual, valid);
RChart1.MoveTo (x,y);
for i := 1 to ChartXRes do
begin
x := RChart1.Scale1X.RangeLow+i*xstep;
y := Stats1.SmoothedSpline (x, FitQual, valid);
if valid then
RChart1.DrawTo (x,y);
end;
RChart1.ShowGraf;
end;
procedure TFrmMain.FormShow(Sender: TObject);
begin
spSKinPageControl1.ActivePageIndex := 0;
LoadData;
ResClearClick;
end;
procedure TFrmMain.LoadData;
var
i : integer;
rx, ry, rz : double;
begin
SBClearClick (Self);
for i := 1 to SequenceView.rzListView2.Items.Count do
begin
rz := StrToFloat(SequenceView.rzListView2.Items.Item[i-1].Caption);
rz := rz/1000.00;
rx := StrToFloat(SequenceView.rzListView2.Items.Item[i-1].Caption);
ry := StrToFloat(SequenceView.rzListView3.Items.Item[i-1].Caption);
RChart1.MarkAt (rx, ry, 26);
Stats1.EnterStatValue (rx,ry);
end;
PolyMax := Stats1.NumData-1;
{
if Stats1.NumData <=9 then
SEPolyOrder.MaxValue := Stats1.NumData-1;
if SEPolyOrder.Value > SEPolyOrder.MaxValue then
SEPolyOrder.Value := SEPolyOrder.MaxValue;
}
RChart1.AutoRange (1,5);
RChart1.ShowGraf;
end;
procedure TFrmMain.LoadResiduals;
var
i : integer;
rx, ry : double;
f : textfile;
begin
ResClearClick;
for i := 1 to SequenceView.rzListView2.Items.Count do
begin
rx := Residuals[i,0];
ry := Residuals[i,1];
RChart2.MarkAt (rx, ry, 26);
Stats2.EnterStatValue (rx,ry);
end;
RChart2.AutoRange (1,5);
RChart2.ShowGraf;
if FileExists (SNRFile) then
begin
AssignFile (f,SNRFile);
Append (f);
writeln (f);
writeln (f,frmMain.Caption);
writeln (f);
writeln (f, RegDetails.spSkinStringGrid1.Cells[0,0] +' = '+ RegDetails.spSkinStringGrid1.Cells[1,0]);
writeln (f, RegDetails.spSkinStringGrid1.Cells[0,1] +' = '+ RegDetails.spSkinStringGrid1.Cells[1,1]);
writeln (f, RegDetails.spSkinStringGrid1.Cells[0,2] +' = '+ RegDetails.spSkinStringGrid1.Cells[1,2]);
writeln (f);
writeln (f,' Year Mean Residual Estimate');
for i := 1 to SequenceView.rzListView2.Items.Count do
begin
writeln (f,Format ('%14.2f%10s%10.1f%10.1f',[Residuals[i,0],SequenceView.rzListView2.Items.Item[i-1].Caption,
Residuals[i,1], PredVal[i]]));
end;
CloseFile (f);
end;
end;
procedure TFrmMain.Exit1Click(Sender: TObject);
begin
Close;
end;
end.