Advertisement
Georgiy031

Untitled

Nov 30th, 2020 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 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 = 0.03
  14. b = 0.97
  15.  
  16. eps = 1.0
  17. n = 1
  18.  
  19. while eps > 0.0001:
  20.  
  21.     x0 = a
  22.     y0 = f(x0)
  23.  
  24.     x = []
  25.     y = []
  26.     y2 = []
  27.     h = (b - a) / n
  28.  
  29.     x.append(x0)
  30.     y.append(y0)
  31.     y2.append(y0)
  32.  
  33.     for i in range(1, n + 1, 1):
  34.         k1 = h * df(x[-1], y[-1])
  35.         k2 = h * df(x[-1] + h / 2, y[-1] + k1 / 2)
  36.         k3 = h * df(x[-1] + h / 2, y[-1] + k2 / 2)
  37.         k4 = h * df(x[-1] + h, y[-1] + k3)
  38.  
  39.         y.append(y[-1] + 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4))
  40.         x.append(x0 + i * h)
  41.         y2.append(f(x[-1]))
  42.  
  43.     eps = math.fabs(f(b) - y[-1])
  44.    
  45.     print(f"n = {n},\nh = {h},\neps = {eps},\ny(b) = {y[-1]}")
  46.     plt.plot(x, y, color='red')
  47.     plt.plot(x, y2, color='green')
  48.     plt.show()
  49.     n *= 2
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement