L3.PAS 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. Uses CRT,GRAPH;
  2. Const
  3. MinX : real = 1;
  4. MaxX : real = 10;
  5. MinY : real = MaxInt;
  6. MaxY : real = -MaxInt;
  7. {---------------------}
  8. LagrTable = 20;
  9. {---------------------}
  10. CStep = 1000;
  11. TVer = 0.01;
  12. Left = 30;
  13. Right = 400;
  14. Top = 20;
  15. Bottom = 400;
  16. {-----------------------------}
  17. Type
  18. LD = array [0..LagrTable] of record
  19. x,y : real;
  20. end;
  21. TB = array [0..LagrTable] of boolean;
  22. var
  23. Thresh : real;
  24. Pts : LD;
  25. BegiP,
  26. EndiP : TB;
  27. Function Func(x: real): real;
  28. Begin
  29. { Func := ln(abs(x));}
  30. Func := Sin(x)*x;
  31. { Func := 1.4*x+2;}
  32. { Func := X*X/10 * sin (x/2)}
  33. End;
  34. {-----------------------------}
  35. Function Lagr(X:real; var T : TB):real;
  36. var
  37. i,j,n : word;
  38. c,ret : real;
  39. begin
  40. n := LagrTable;
  41. ret:=0;
  42. for i := 0 to n do if T[i] then
  43. begin
  44. c:=1;
  45. for j := 0 to n do
  46. if (j <> i) AND T[j] then
  47. C := C * (X-Pts[j].X) / (Pts[i].X-Pts[j].X);
  48. ret := ret + Pts[i].Y*C;
  49. end;
  50. Lagr := ret;
  51. end;
  52. {-----------------------------}
  53. Procedure SetMinMax(var T:tb);
  54. var
  55. c:real;
  56. i:word;
  57. begin
  58. if MaxX < MinX then
  59. begin
  60. C := MaxX;MaxX := MinX;MinX := C;
  61. end;
  62. for i := 0 to Cstep do
  63. begin
  64. C := Lagr(MinX+i/CStep*(MaxX-MinX),T);
  65. if C > MaxY then MaxY:=C;
  66. if C < MinY then MinY:=C;
  67. end;
  68. end;
  69. {-----------------------------}
  70. Procedure InitG;
  71. Var
  72. grDriver: Integer;
  73. grMode: Integer;
  74. i : integer;
  75. Begin
  76. grDriver := Detect;
  77. InitGraph(grDriver, grMode,'');
  78. if GraphResult <> grOk then
  79. begin
  80. WriteLn('Graph error!');
  81. Halt(255);
  82. end;
  83. End;{InitG}
  84. {---------------------------------}
  85. Procedure DrawAxis;
  86. var
  87. i : integer;
  88. s : string;
  89. begin
  90. SetColor(White);
  91. Line(Left,Bottom,Right+10,Bottom);
  92. Line(Right-5,Bottom-5,Right+10,Bottom);
  93. Line(Right-5,Bottom+5,Right+10,Bottom);
  94. Line(Left,Top-10,Left,Bottom);
  95. Line(Left,Top-10,Left-5,Top+5);
  96. Line(Left,Top-10,Left+5,Top+5);
  97. OutTextXY(Left-15,Top,'Y');
  98. OutTextXY(Right,Bottom+10,'X');
  99. Str(MaxX:0:2,S);
  100. OutTextXY(Right,Bottom-10,S);
  101. Str(MinX:0:2,S);
  102. OutTextXY(Left,Bottom-10,S);
  103. Str(MaxY:0:2,S);
  104. OutTextXY(Left+10,Top,S);
  105. Str(MinY:0:2,S);
  106. OutTextXY(Left+10,Bottom,S);
  107. end;
  108. {--------------------}
  109. Procedure LoadData;
  110. var
  111. i : longint;
  112. begin
  113. for i := 0 to LagrTable do
  114. begin
  115. Pts[i].x := MinX + sqrt(i)/Sqrt(LagrTable)*(MaxX-MinX);
  116. { Pts[i].x := MinX + i/LagrTable*(MaxX-MinX);}
  117. Pts[i].y := Func(Pts[i].x);
  118. BegiP[i] := true;
  119. EndiP[i] := true;
  120. end;
  121. end;
  122. {--------------------}
  123. Procedure FillTable(var T : TB);
  124. var
  125. i,j:word;
  126. ax,ay : string;
  127. begin
  128. SetFillStyle(SolidFill,Black);
  129. Bar(440,30,630,380);
  130. SetColor(Cyan);
  131. Rectangle(440,30,630,380);
  132. SetColor(Blue);
  133. j := 0;
  134. for i := 0 to LagrTable do if T[i] then
  135. begin
  136. Str(Pts[i].x:0:2,aX); aX:='X'+Chr(j+$30)+'='+aX;
  137. Str(Pts[i].y:0:2,aY); aY:='Y'+Chr(j+$30)+'='+aY;
  138. SetTextJustify(LeftText,CenterText);
  139. OutTextXY(450,round(50 + i/LagrTable*300),aX);
  140. OutTextXY(550,round(50 + i/LagrTable*300),aY);
  141. inc(j);
  142. end;
  143. end;
  144. {-----------------------------}
  145. Procedure DrawLagr(var T : TB);
  146. Var
  147. Cx,Cy : real;
  148. i : word;
  149. x, y : real;
  150. Begin
  151. Cx := (Right-Left) / (MaxX-MinX);
  152. Cy := (Bottom-Top) / (MaxY-MinY);
  153. MoveTo(Left,round(Bottom-(Lagr(MinX,T)-MinY)*Cy));
  154. for i := 0 to CStep do
  155. begin
  156. x:=MinX + i/CStep*(MaxX-MinX);
  157. y:=Lagr(X,T);
  158. LineTo(round(Left+(X-MinX)*Cx),round(Bottom-(Y-MinY)*Cy));
  159. end;
  160. SetColor(Red);
  161. for i:=0 to Lagrtable do if T[i] then
  162. Circle(round(Left+(Pts[i].X-MinX)*Cx),round(Bottom-(Pts[i].y-MinY)*Cy),2);
  163. End;{DrawLagr}
  164. {-------------}
  165. Function Check(var T1, T2 : TB):boolean;
  166. var
  167. i : word;
  168. x, y1, y2 : real;
  169. Step : word;
  170. Begin
  171. Check := true;
  172. Step := CStep div 10;
  173. for i := 0 to Step do
  174. begin
  175. x:=MinX + i/Step*(MaxX-MinX);
  176. y1:=Lagr(X,T1);
  177. y2:=Lagr(X,T2);
  178. if abs(y1-y2) >= Thresh then
  179. begin
  180. Check := false;
  181. Exit;
  182. end;
  183. end;
  184. end;
  185. var
  186. CKick : word;
  187. ifDel : boolean;
  188. cS : word;
  189. Begin
  190. LoadData;
  191. InitG;
  192. SetMinMax(BegIp);
  193. Thresh := TVer * (MaxY-MinY);
  194. DrawAxis;
  195. { WriteGraph(CStep);}
  196. FillTable(BegIp);
  197. SetColor(Green);
  198. DrawLagr(BegIp);
  199. CKick :=0;
  200. for cS := 1 to LagrTable-1 do
  201. begin
  202. EndiP[cS] := false;
  203. if Check(BegiP,EndiP) then
  204. { if abs(Lagr(Pts[cS].X,BegIP)-Lagr(Pts[cS].X,EndIP)) < Thresh then}
  205. inc(CKick)
  206. else
  207. EndiP[cs] := true;
  208. end;
  209. ReadKey;
  210. FillTable(EndiP);
  211. SetColor(White);
  212. DrawLagr(EndiP);
  213. ReadLn;
  214. CloseGraph;
  215. WriteLn('Выкинуто точек - ',CKick);
  216. End.