Advertisement
klasscho

Untitled

Dec 15th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. program Project12;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8. System.SysUtils;
  9.  
  10. function Epsilon(LimitEps: Real): Real;
  11. var
  12. Eps: Real;
  13. IsCorrect: Boolean;
  14. begin
  15. Writeln('Enter the precision value:');
  16. Readln(Eps);
  17. repeat
  18. try
  19. if (Eps < 1) and (Eps > LimitEps) then
  20. begin
  21. IsCorrect := True;
  22. end
  23. else
  24. Writeln('Enter a value between (1; 0.000001)');
  25. except
  26. IsCorrect := False;
  27. Writeln('Enter a number!');
  28. end;
  29. until IsCorrect ;
  30. Epsilon := Eps;
  31. end;
  32.  
  33. function FunctionArgument(LimitX: Real): Real;
  34. var
  35. x: Real;
  36. IsCorrect: Boolean;
  37. begin
  38. Writeln('Enter the value of the function argument:');
  39. Readln(x);
  40. repeat
  41. try
  42. if (x < 0) and (x > LimitX) then
  43. begin
  44. IsCorrect := True;
  45. end
  46. else
  47. Writeln('Enter a value between (0; 10)');
  48. except
  49. IsCorrect := False;
  50. Writeln('Enter a number!');
  51. end;
  52. until IsCorrect ;
  53. FunctionArgument := x;
  54. end;
  55.  
  56. function MaclaurinSeries(Eps, x: Real): Real;
  57. var
  58. y, an: Real;
  59. n: Integer;
  60. begin
  61. y := x;
  62. an := x;
  63. n := 0;
  64. while abs(an) > Eps do
  65. begin
  66. Inc(n);
  67. an := -an * sqr(x)/ ( 2 * n * (2 * n + 1));
  68. y := y + an;
  69. end;
  70. MaclaurinSeries := y;
  71. end;
  72.  
  73. procedure Input(Eps, x, y: Real);
  74. var
  75. i: Integer;
  76. begin
  77. WriteLn('x =',x: 8: 4, ' eps =',eps: 8: 4, ' y =',y: 8: 4);
  78. Writeln;
  79. end;
  80.  
  81. procedure ReadFileOutputName(var MyFile: TextFile);
  82. var
  83. NewFName: string;
  84. CorrectName: Boolean;
  85. begin
  86. Writeln('Enter a file name for data entry in the format Name.txt');
  87. repeat
  88. Readln(NewFName);
  89. if Copy(NewFName, length(NewFName) - 3, 4) = '.txt' then
  90. CorrectName := True
  91. else
  92. begin
  93. Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
  94. CorrectName := False;
  95. end;
  96. until CorrectName;
  97. AssignFile(MyFile, NewFName);
  98. Rewrite(MyFile);
  99. end;
  100.  
  101.  
  102. procedure WriteToFile(const y: Real; var NewFile: TextFile);
  103. var
  104. i, n: Integer;
  105. begin
  106. Dec(n);
  107. for i := 0 to n do
  108. begin
  109. Write(NewFile, 'y= ',y: 8: 4 );
  110. Write(NewFile, ' ');
  111. end;
  112. CloseFile(NewFile);
  113. Writeln('Saved to file. ');
  114. end;
  115.  
  116. const
  117. LimitEps = 0.000001;
  118. LimitX = 124;
  119. var
  120. Eps, x, y: Real;
  121. MyTFile: TextFile;
  122. begin
  123. Writeln('This program calculates the value of a function y = sin(x) with precision e by expanding the function in a Maclaurin series');
  124. Eps := Epsilon(LimitEps);
  125. x := FunctionArgument(LimitX);
  126. y := MaclaurinSeries(Eps, x);
  127. Input(Eps, x, y);
  128. ReadFileOutputName(MyTFile);
  129. WriteToFile(y, MyTFile);
  130. Readln;
  131. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement