Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import math
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import numpy
  5.  
  6. def realsolution(x):
  7. return x*(math.sin(x))+(math.cos(x))
  8. # Первый класс точности
  9. z1 = []
  10. z2 = []
  11. y = []
  12. x = []
  13. i = 0
  14. a = 0.0
  15. b = 1.0
  16. h = 0.05
  17. n = (b-a)/h
  18. k = 0.0
  19.  
  20. z1.append(1.0)
  21. z2.append(-1.0)
  22. y.append(1.3818)
  23. x.append(0.0)
  24.  
  25. while i < n:
  26. z1.append(z1[i]+h*(-(z1[i])**2-z1[i]*(math.tan(k))-k))
  27. z2.append(z2[i]+h*(-z2[i]*(z1[i]+math.tan(k))+(1+k)*(math.cos(k))+(k**2)*(math.sin(k))))
  28. i+=1
  29. k+=h
  30. x.append(k)
  31.  
  32. x = np.array(x)
  33. i = 0
  34.  
  35. while i < len(z1)-1:
  36. y.append(y[i]-h*(z1[len(z1)-1-i]*y[i]+z2[len(z1)-1-i]))
  37. i+=1
  38.  
  39. y.reverse()
  40. print('Решение численным методом прогонки: ',y)
  41.  
  42. # Второй класс точности
  43. r1 = []
  44. r2 = []
  45. u = []
  46. i = 0
  47. k = 0.0
  48.  
  49. r1.append(0.98)
  50. r2.append(-1.03)
  51. u.append(1.3818)
  52.  
  53. while i < n:
  54. r1.append(r1[i]+h*(-(r1[i])**2-r1[i]*(math.tan(k))-k))
  55. r2.append(r2[i]+h*(-r2[i]*(r1[i]+math.tan(k))+(1+k)*(math.cos(k))+(k**2)*(math.sin(k))))
  56. i+=1
  57. k+=h
  58.  
  59. i = 0
  60.  
  61. while i < len(r1)-1:
  62. u.append(u[i]-h*(r1[len(r1)-1-i]*u[i]+r2[len(r1)-1-i]))
  63. i+=1
  64.  
  65. u.reverse()
  66. print('Решение численным методом прогонки второго класса точности: ',u)
  67.  
  68. # Реальное решение
  69. i = 0
  70. u0 = []
  71.  
  72. for j in x:
  73. u0.append(realsolution(j))
  74.  
  75. print('Ответ к задаче: ',u0)
  76.  
  77. plt.subplot(111)
  78. plt.xlabel(r'$x$')
  79. plt.ylabel(r'$u(x)$')
  80. plt.title("Boundary problem")
  81. plt.plot(x, u0, "b",label = 'Real solution')
  82. plt.plot(x, y, "--h", label = 'Numerical solution first precision')
  83. plt.plot(x,u,"g",label = 'Numerical solution second precision')
  84. plt.legend()
  85. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement