Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. from math import log, e, sqrt, exp
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. def func(x, y):
  7. return exp(x + y)
  8.  
  9.  
  10. h = 0.005
  11. a = 0
  12. b = 0.5
  13. y0 = 0
  14.  
  15. x = np.arange(a + h, b + h, h)
  16. y = []
  17. y.append(y0)
  18. for xn, n in zip(x, range(len(x))):
  19. f1 = func(xn, y[n])
  20. f2 = func(xn + h / 2, y[n] + (h / 2) * f1)
  21. f3 = func(xn + h, y[n] - h * f1 + 2 * h * f2)
  22. y.append(y[n] + (h / 6) * (f1 + 4 * f2 + f3))
  23.  
  24. x = x.tolist()
  25. x.insert(0, 0)
  26.  
  27. plt.plot(x, y)
  28. plt.tick_params(length=4, width=2, labelsize=10)
  29. r = [i for i in np.arange(0, 1.5, 0.1)]
  30. plt.yticks(r)
  31. plt.xticks(r)
  32. plt.grid(True)
  33. # plt.show()
  34. for i, j in zip(x, y):
  35. print('x: ', round(i, 5), ' ', 'y: ', round(j, 5), sep=' ')
  36. # def solv(func, a, b, h, y0, x0):
  37.  
  38. # # valid(a, b, h, y0, x0)
  39. # x = np.arange(a+h, b+h, h)
  40. # y = []
  41. # y.append(y0)
  42.  
  43. # for xn,n in zip(x,range(len(x))):
  44. # f1 = func(xn, y[n])
  45. # f2 = func(xn + h/2, y[n] + (h/2)*f1)
  46. # f3 = func(xn + h, y[n] - h*f1 + 2*h*f2)
  47. # y.append(y[n] + (h/6)*(f1 + 4*f2 + f3))
  48.  
  49. # x = x.tolist()
  50. # x.insert(0, 0)
  51. # plt.plot(x, y)
  52. # plt.tick_params( length=4, width=2, labelsize=10)
  53. # r = [i for i in np.arange(0, 1.5,0.1)]
  54. # plt.yticks(r)
  55. # plt.xticks(r)
  56. # plt.grid(True)
  57. # # plt.show()
  58.  
  59. # return round(y[int((x0 - a)*(1/h))], 5)
  60.  
  61. # print(solv(func, 0, 0.5, 0.0001, 0, 0.5))
  62. # print(-log(2 - exp(0.5)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement