Advertisement
Georgiy031

Untitled

Dec 20th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import math
  3.  
  4.  
  5. def f(x):
  6.     return 1 / math.sqrt(2) * (x - 1) / math.sqrt(x - math.log(x))
  7.  
  8.  
  9. def df(x, y):
  10.     return y / (x - 1) - y ** 3 / (x * x - x)
  11.  
  12.  
  13. a = 2
  14. b = 20
  15.  
  16. n = 5
  17.  
  18. x0 = a
  19. y0 = f(x0)
  20.  
  21. x1 = []
  22. y1 = []
  23. y2 = []
  24. h1 = (b - a) / n
  25.  
  26. x1.append(x0)
  27. y1.append(y0)
  28. y2.append(y0)
  29.  
  30. for i in range(1, n + 1, 1):
  31.     k1 = h1 * df(x1[-1], y1[-1])
  32.     k2 = h1 * df(x1[-1] + h1 / 2, y1[-1] + k1 / 2)
  33.     k3 = h1 * df(x1[-1] + h1 / 2, y1[-1] + k2 / 2)
  34.     k4 = h1 * df(x1[-1] + h1, y1[-1] + k3)
  35.  
  36.     y1.append(y1[-1] + 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4))
  37.    
  38.     x1.append(x0 + i * h1)
  39.     y2.append(f(x1[-1]))
  40.  
  41.  
  42. eps_lim = 0.01
  43. eps_max = 0
  44.  
  45. for i in range(1, n + 1):
  46.   l = x1[i - 1]
  47.   r = x1[i]
  48.   yl = y1[i - 1]
  49.   yr = y1[i]
  50.   ycur = yl
  51.   xcur = l
  52.   while xcur < r:
  53.     xcur += (r - l) / 100
  54.     ycur += (yr - yl) / 100
  55.     eps_max = max(eps_max, abs(f(xcur) - ycur))
  56.    
  57.  
  58.  
  59. print(f"eps = {eps_max}")
  60.  
  61. print(f"n = {n},\nh = {h1}\ny(b) = {y1[-1]}")
  62. plt.plot(x1, y1, color='red')
  63. plt.plot(x1, y2, color='green')
  64. plt.show()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement