Advertisement
DarkDevourer

Практика - задание 1 (Pascal)

Jun 29th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. Uses GraphABC;
  2.  
  3. function func(x: double): double;
  4. begin
  5. Result:=(sin(x)/cos(x) - (power(sin(x)/cos(x), 3) / 3) + (power(sin(x)/cos(x), 5) / 5) - (1.0 / 3));
  6. end;
  7.  
  8. var counter:array of integer;
  9. e: array of double;
  10. N, w, h, height, length, i, mastx, masty: integer;
  11. a, b, x, fa, fb, fx, e1, maxcounter: double;
  12. conv: string;
  13. begin
  14. w:=1900; h:=1000; length:= w - 50; height:= h - 100; a:= 0; b:= 0.8;
  15. setwindowwidth(w);
  16. setwindowheight(h);
  17. fa:=func(a); fb:=func(b);
  18. writeln('Нахождение корня уравнения tg(x)-(tg(x)^3)/3+(tg(x)^5)/5-1/3=0 методом половинного деления на отрезке [0;0.8].');
  19. if(fa*fb>0) then
  20. writeln('На заданном отрезке нет корня.')
  21. else
  22. begin
  23. writeln('Введите, при скольких различных точностях вы хотите решить данное уравнение (целое число):');
  24. read(N);
  25. writeln('N = ', N);
  26. writeln('Введите начальное значение точности (действительное число). Дальнейшие точности будут в 2 раза меньше предыдущей:');
  27. read(e1);
  28. writeln('E = ', e1);
  29. SetLength(counter,n); SetLength(e,n);
  30. for i:=0 to n-1 do
  31. begin
  32. a:= 0; b:= 0.8;
  33. counter[i]:=0;
  34. if i=0 then
  35. e[i]:=e1
  36. else
  37. begin
  38. e[i]:=e[i-1]/2;
  39. e1:=e[i];
  40. end;
  41. while(true) do
  42. begin
  43. x:=(b + a) / 2;
  44. fx:=func(x);
  45. if fx*fa<0 then
  46. begin
  47. b:= x;
  48. fb:= fx;
  49. end
  50. else
  51. begin
  52. a:= x;
  53. fa:= fx;
  54. end;
  55. counter[i]:=counter[i]+1;
  56. if(abs(b-a)<=e1) then
  57. begin
  58. x:=(b + a) / 2;
  59. counter[i]:=counter[i]+1;
  60. writeln('Ответ: x=',x);
  61. writeln('Количество итераций для точности e=', e[i],': ', counter[i]);
  62. break;
  63. end;
  64. end;
  65. end;
  66.  
  67. mastx:= round(length/n);
  68. maxcounter:=counter[0];
  69. for i:=1 to n-1 do
  70. if counter[i]>maxcounter then
  71. maxcounter:=counter[i];
  72. masty:=round(height/maxcounter);
  73. writeln('Нажмите Enter, чтобы вывести диаграмму сравнения количества итераций к точности.');
  74. readln();
  75. readln();
  76. ClearWindow;
  77. line(30, 30, 30, 30 + height);
  78. line(30, 30, 25, 40);
  79. line(30, 30, 35, 40);
  80. line(30, 30 + height, 20 + length, 30 + height);
  81. line(20 + length, 30 + height, length + 15, 25 + height);
  82. line(20 + length, 30 + height, length + 15, 35 + height);
  83. textout(length + 20, 45 + height, 'i');
  84. textout(length + 20, 60 + height, 'e');
  85. textout(40, 30, 'Counter');
  86. for i:=0 to n-1 do
  87. begin
  88. line(30 + i * mastx, 20 + height, 30 + i * mastx, 40 + height);
  89. conv:=inttostr(i);
  90. textout(30 + i * mastx, 45 + height, conv);
  91. conv:=floattostr(e[i]);
  92. textout(30 + i * mastx, 60 + height, conv);
  93. line(20, 30 + height - counter[i] * masty, 40, 30 + height - counter[i] * masty);
  94. conv:=inttostr(counter[i]);
  95. textout(5, 30 + height - counter[i] * masty, conv);
  96. end;
  97. for i:=0 to n-1 do
  98. begin
  99. line(30 + i * mastx, 30 + height, 30 + i * mastx, 30 + height - counter[i] * masty);
  100. line(30 + i * mastx, 30 + height - counter[i] * masty, 30 + (i + 1) * mastx, 30 + height - counter[i] * masty);
  101. line(30 + (i + 1) * mastx, 30 + height, 30 + (i + 1) * mastx, 30 + height - counter[i] * masty);
  102. end;
  103. readln();
  104. ClearWindow;
  105. writeln('Нажмите Enter, чтобы вывести график сравнения количества итераций к точности.');
  106. readln();
  107. ClearWindow;
  108. line(30, 30, 30, 30 + height);
  109. line(30, 30, 25, 40);
  110. line(30, 30, 35, 40);
  111. line(30, 30 + height, 20 + length, 30 + height);
  112. line(20 + length, 30 + height, length + 15, 25 + height);
  113. line(20 + length, 30 + height, length + 15, 35 + height);
  114. textout(length + 20, 45 + height, 'i');
  115. textout(length + 20, 60 + height, 'e');
  116. textout(40, 30, 'Counter');
  117. for i:=0 to n-1 do
  118. begin
  119. line(30 + i * mastx, 20 + height, 30 + i * mastx, 40 + height);
  120. conv:=inttostr(i);
  121. textout(30 + i * mastx, 45 + height, conv);
  122. conv:=floattostr(e[i]);
  123. textout(30 + i * mastx, 60 + height, conv);
  124. line(20, 30 + height - counter[i] * masty, 40, 30 + height - counter[i] * masty);
  125. conv:=inttostr(counter[i]);
  126. textout(5, 30 + height - counter[i] * masty, conv);
  127. end;
  128. for i:=0 to n-2 do
  129. begin
  130. line(30 + i * mastx, 30 + height - counter[i] * masty, 30 + (i + 1) * mastx, 30 + height - counter[i + 1] * masty);
  131. end;
  132. end;
  133. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement