-
Notifications
You must be signed in to change notification settings - Fork 0
/
TIntegralUnit.pas
383 lines (330 loc) · 10.3 KB
/
TIntegralUnit.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
unit TIntegralUnit;
interface
uses SysUtils, Variants, Math, Chart, Grids, WordXP, ExcelXP, ComObj, Classes;
type
TIntegral = class
private
a,b:extended; // limits of integration
h:extended; // step of integration
n:integer; // number of results
myList:TStringList; // sheet for storing previous results
function GetA():extended;
function GetB():extended;
function GetH():extended;
function GetN():integer;
procedure SetA(aa:extended);
procedure SetB(bb:extended);
procedure SetH(hh:extended);
public
constructor Create(aa,bb,hh:extended);
destructor Destroy; Override;
// properties
property Low:extended read GetA write SetA ;
property Top:extended read GetB write SetB ;
property Step:extended read GetH write SetH ;
property NumberResults:integer read GetN;
// logic
function k(x,s:extended):extended;
function f(x:extended):extended;
function y(x:extended):extended;
function yy(i:integer):extended;
procedure updateN(); // update parametr N
procedure displayOnGraph(str:TStringGrid;chr:TChart); // draw a graph of the results
procedure FillInTheTable(str:TStringGrid); // fill in the table of results of desicions
// work with MS Office and Excel
procedure TextWord(word:TWordDocument;appl:TWordApplication);overload;
procedure TextWord(word:TWordDocument;appl:TWordApplication;str:TStringGrid; chr:TChart);overload;
procedure TextExcel();overload;
procedure TextExcel(str:TStringGrid;chr:TChart);overload;
// work with file
procedure writeTextFile(filename:string);overload;
procedure writeTextFile(filename:string;str:TStringGrid);overload;
procedure readTextFile(filename:string);
end;
implementation
procedure TIntegral.updateN();
begin
n:=trunc((b-a)/h+1);
end;
procedure TIntegral.displayOnGraph(str:TStringGrid;chr:TChart);
var
int:integer;
begin
chr.Series[0].Clear;
chr.Series[1].Clear;
for int:=1 to str.RowCount-1 do
begin
chr.Series[0].AddXY(StrToFloat(str.Cells[0,int]), StrToFloat(str.Cells[1,int]));
chr.Series[1].AddXY(StrToFloat(str.Cells[0,int]), StrToFloat(str.Cells[2,int]));
end;
end;
procedure TIntegral.FillInTheTable(str:TStringGrid);
var
i:integer;
x:extended;
buf:extended;
begin
n:=trunc((b-a)/h) + 1;
str.RowCount:=n + 1;
for i:=1 to n do
begin
x:=a+(i-1)*h;
buf:=yy(i);
myList.Add(FloatToStr(buf));
str.Cells[0,i]:=FloatToStr(x);
str.Cells[1,i]:=FloatToStr(SimpleRoundTo(buf, -5));
str.Cells[2,i]:=FloatToStr(SimpleRoundTo(y(x), -5));
end;
end;
// solution of the equation by the method of quadrature formulas
// Yi = 2/K(Xi,Xi) * (F(Xi)/H - sum(from 1 to i-1)(Aj*K(Xi,Xj)*Yj))
// Aj=0,5 if j=1 else Aj=1
function TIntegral.yy(i:integer):extended;
var
g:extended;
j:integer;
x,k1:extended;
x1:extended;
sum:extended;
begin
if i=1 then
begin
result:=0;
end
else
begin
x:=a+(i-1)*h;
g:=f(x)/h;
sum:=0;
for j:=1 to i-1 do
begin
x1:=a+(j-1)*h;
k1:=k(x,x1);
if j=1 then
begin
k1:=k1/2;
end;
sum:=sum + k1*StrToFloat(myList[j-1]);
end;
g:=g-sum;
result:=2*g/k(x,x);
end;
end;
// K(x, s) = 2 + x^2 - s^2
function TIntegral.k(x,s:extended):extended;
begin
result:=2+(x-s)*(x+s);
end;
// F(x) = x^2
function TIntegral.f(x:extended):extended;
begin
result:=power(x,2);
end;
// y = x * e^(-x^2/2)
function TIntegral.y(x:extended):extended;
begin
result:=x*exp(-power(x,2)/2);
end;
constructor TIntegral.Create(aa,bb,hh:extended);
begin
a:=aa;
b:=bb;
h:=hh;
myList:= TStringList.Create;
end;
destructor TIntegral.Destroy;
begin
inherited Destroy;
end;
function TIntegral.GetA():extended;
begin
result:=a;
end;
function TIntegral.GetB():extended;
begin
result:=b;
end;
function TIntegral.GetH():extended;
begin
result:=h;
end;
function TIntegral.GetN():integer;
begin
result:=n;
end;
procedure TIntegral.SetA(aa:extended);
begin
a:=aa;
updateN();
end;
procedure TIntegral.SetB(bb:extended);
begin
b:=bb;
updateN();
end;
procedure TIntegral.SetH(hh:extended);
begin
h:=hh;
updateN();
end;
procedure TIntegral.TextWord(word:TWordDocument;appl:TWordApplication);
begin
//appl.Connect; // connect server word
appl:= TWordApplication.Create(nil);
word:= TWordDocument.Create(nil);
appl.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);// add document
word.ConnectTo(appl.ActiveDocument);// connection WordDocument with WordApplication
// turn off spell check for faster work
appl.Options.CheckSpellingAsYouType := False;
appl.Options.CheckGrammarAsYouType := False;
// font
appl.Selection.Font.Size:=16;
// record date to word
appl.Selection.TypeText('First limits of integration: ' + FloatToStr(a) + #13#10);
appl.Selection.TypeText('Second limits of integration: ' + FloatToStr(b) + #13#10);
appl.Selection.TypeText('Step of integration: ' + FloatToStr(h) + #13#10);
appl.Visible:=true; // doing word visible
//appl.Disconnect; // disconnect server word
end;
procedure TIntegral.TextWord(word:TWordDocument;appl:TWordApplication;str:TStringGrid; chr:TChart);
var
int:integer;
end1:OleVariant;
begin
//appl.Connect; // activate server word
appl:= TWordApplication.Create(nil);
word:= TWordDocument.Create(nil);
appl.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);// add document
word.ConnectTo(appl.ActiveDocument);// connection WordDocument with WordApplication
// turn off spell check for faster work
appl.Options.CheckSpellingAsYouType := False;
appl.Options.CheckGrammarAsYouType := False;
// font
appl.Selection.Font.Size:=16;
// record date to word
appl.Selection.TypeText('First limits of integration: ' + FloatToStr(a) + #13#10);
appl.Selection.TypeText('Second limits of integration: ' + FloatToStr(b) + #13#10);
appl.Selection.TypeText('Step of integration: ' + FloatToStr(h) + #13#10);
appl.Selection.Select;
// created 3 columns of N lines
word.Tables.Add(appl.Selection.Range, str.RowCount, 3, EmptyParam, EmptyParam);
// set the width of columns
word.Tables.Item(1).Columns.Item(1).Width:=100;
word.Tables.Item(1).Columns.Item(2).Width:=200;
word.Tables.Item(1).Columns.Item(3).Width:=200;
// create column headers
word.Tables.Item(1).Cell(1,1).select;
appl.Selection.TypeText('X');
word.Tables.Item(1).Cell(1,2).select;
appl.Selection.TypeText('Y1');
word.Tables.Item(1).Cell(1,3).select;
appl.Selection.TypeText('Y2');
// write data to columns
for int:=1 to str.RowCount do
begin
word.Tables.Item(1).Cell(int+1,1).select;
appl.Selection.TypeText(str.Cells[0,int]);
word.Tables.Item(1).Cell(int+1,2).select;
appl.Selection.TypeText(str.Cells[1,int]);
word.Tables.Item(1).Cell(int+1,3).select;
appl.Selection.TypeText(str.Cells[2,int]);
end;
chr.CopyToClipboardBitmap; // copy chart to clipboard
end1:=word.Range.End_-1; // end of document
word.Range(end1).Paste; // past graph in document
appl.Visible:=true;// doing word visible
//appl.Disconnect; // disconnect server word
end;
procedure TIntegral.TextExcel();
var
Excel,Work:variant;
begin
Excel:=CreateOleObject('Excel.Application'); // create COM-object
Excel.Application.EnableEvents := false; // Disable response to external events-to speed up the data transfer process
Work:= Excel.WorkBooks.Add; // Add workbook
// write data to columns
Work.WorkSheets[1].Cells[1, 1]:='First limits of integration';
Work.WorkSheets[1].Cells[1, 4]:=a;
Work.WorkSheets[1].Cells[2, 1]:='Second limits of integration';
Work.WorkSheets[1].Cells[2, 4]:=b;
Work.WorkSheets[1].Cells[3, 1]:='Step of integration';
Work.WorkSheets[1].Cells[3, 4]:=h;
Excel.Visible:=true; // show Excel
end;
procedure TIntegral.TextExcel(str:TStringGrid;chr:TChart);
var
int:integer;
Excel,Work:variant;
begin
Excel:= CreateOleObject('Excel.Application'); // create COM-object
Excel.Application.EnableEvents := false; // Disable response to external events-to speed up the data transfer process
Work:= Excel.WorkBooks.Add; // add workbook
// write data to columns
Work.WorkSheets[1].Cells[1, 1]:='First limits of integration';
Work.WorkSheets[1].Cells[1, 4]:=a;
Work.WorkSheets[1].Cells[2, 1]:='Second limits of integration';
Work.WorkSheets[1].Cells[2, 4]:=b;
Work.WorkSheets[1].Cells[3, 1]:='Step of integration';
Work.WorkSheets[1].Cells[3, 4]:=h;
Work.WorkSheets[1].Cells[6, 1]:='X';
Work.WorkSheets[1].Cells[6, 2]:='Y1';
Work.WorkSheets[1].Cells[6, 3]:='Y2';
for int:= 1 to str.RowCount do
begin
Work.WorkSheets[1].Cells[int+7, 1]:= str.Cells[0,int];
Work.WorkSheets[1].Cells[int+7, 2]:= str.Cells[1,int];
Work.WorkSheets[1].Cells[int+7, 3]:= str.Cells[2,int];
end;
Work.WorkSheets[1].Cells[5, 10].Select;
chr.SaveToBitmapFile(GetCurrentDir + '\Excel.bmp');// save graph to file
Work.WorkSheets[1].Shapes.AddPicture(GetCurrentDir + '\Excel.bmp',True, True, 300, 100,chr.Width * 0.8,chr.Height * 0.8); // inserrt picture graph in excel
Excel.Visible := true;// show Excel
end;
procedure TIntegral.writeTextFile(filename:string);
var
f:TextFile;
begin
AssignFile(f,filename); // associate a file variable with the file name
Rewrite(f); // open for overwrite
// write in file
writeln(f,a:5:2);
writeln(f,b:5:2);
writeln(f,h:5:2);
writeln(f,n:5);
CloseFile(f); // close file
end;
procedure TIntegral.writeTextFile(filename:string;str:TStringGrid);
var
f:TextFile;
int:integer;
begin
AssignFile(f,filename);// associate a file variable with the file name
Rewrite(f);// open for overwrite
// write in file
writeln(f,'First limits of integration: ', a:5:2);
writeln(f,'Second limits of integration: ', b:5:2);
writeln(f,'Step of integration: ', h:5:2);
writeln(f,'Number of results: ', n:5);
writeln(f,'Grid: ');
// write if file data like a table
writeln(f,str.Cells[0,0],' ',str.Cells[1,0],'; ', str.Cells[2,0]);
for int:=1 to str.RowCount do
begin
writeln(f,str.Cells[0,int],': ',str.Cells[1,int],'; ', str.Cells[2,int]);
end;
CloseFile(f); // close file
end;
procedure TIntegral.readTextFile(filename:string);
var
f:TextFile;
begin
AssignFile(f,filename);// associate a file variable with the file name
Reset(f); // open for reading
// read from file
readln(f,a);
readln(f,b);
readln(f,h);
readln(f,n);
CloseFile(f); // close file
end;
end.