Advertisement
Guest User

Untitled

a guest
May 25th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #-*- coding:UTF-8 -*-
  2. import math
  3.  
  4.  
  5. from BuildPlot import create_plot
  6.  
  7.  
  8. def function(x, y):
  9. return (y/(1+x))-0.5*y*y
  10.  
  11.  
  12. def tabulate_function(a, b, tabulate_step):
  13. y = []
  14. x = []
  15. current_x = a
  16. while current_x <= b:
  17. y.append(function(current_x))
  18. x.append(current_x)
  19. current_x += tabulate_step
  20. return [x, y]
  21.  
  22.  
  23. def method_runge_kutta_3(x, y, h):
  24. phi_0 = h * function(x, y)
  25. phi_1 = h * function(x + h / 2, y + phi_0 / 2)
  26. phi_2 = h * function(x + h, y - phi_0 + 2 * phi_1)
  27. return [x + h, (phi_0 + 4 * phi_1 + phi_2)/6 + y]
  28.  
  29. def method_runge_kutta_4(x, y, h):
  30. phi_0 = h * function(x, y)
  31. phi_1 = h * function(x + h / 3, y + phi_0 / 3)
  32. phi_2 = h * function(x + 2*h / 3, y - phi_0 / 3 + phi_1)
  33. phi_3 = h * function(x + h, y + phi_0+phi_1+phi_2)
  34. return [x + h, y+ (phi_0+3*phi_1+3*phi_2+phi_3)/8]
  35.  
  36. def method_prediction_and_correction(prev_x, prev_y, h):
  37. x = prev_x + h
  38. y = prev_y + h * function(prev_x, prev_y)
  39. y = prev_y + h * function(x, y) + (function(x, y) - function(prev_x, prev_y)) * h / 2
  40. return [x, y]
  41.  
  42.  
  43. if __name__ == "__main__":
  44.  
  45.  
  46. h = 0.1
  47. x = 0 # float(input("Write a: "))
  48. y = 1 # float(input("Write b: "))
  49. data1 = [[x], [y]]
  50. data2 = [[x], [y]]
  51. data3 = [[x], [y]]
  52. print("Лабораторная работа №3, вариант 12, часть 2: "
  53. "Используя метод Рунге-Кутта третьего, четвертого порядка точности и\n" +
  54. "метод прогноза и коррекции, вычислить значения y1, y2, y3 приближенного\n" +
  55. "решения заданного дифференциального уравнения первого порядка. Вычис-\n" +
  56. "ления производить с пятью знаками после запятой. Шаг h = 0.1 . Результаты\n" +
  57. "вычислений отобразить на графике.\n")
  58. print("Method Runge Kutta 3")
  59. value = [x, y]
  60. for i in range(3):
  61. value = method_runge_kutta_3(value[0], value[1], h)
  62. print("x[{2}] = {0}\ny[{2}] = {1}".format(value[0], value[1], i))
  63. data1[0].append(value[0])
  64. data1[1].append(value[1])
  65.  
  66. print("\nMethod Runge Kutta 4")
  67. value = [x, y]
  68. for i in range(3):
  69. value = method_runge_kutta_4(value[0], value[1], h)
  70. print("x[{2}] = {0}\ny[{2}] = {1}".format(value[0], value[1], i))
  71. data2[0].append(value[0])
  72. data2[1].append(value[1])
  73.  
  74. print("\nMethod prediction and correction")
  75. value = [x, y]
  76. for i in range(3):
  77. value = method_prediction_and_correction(value[0], value[1], h)
  78. print("x[{2}] = {0}\ny[{2}] = {1}".format(value[0], value[1], i))
  79. data3[0].append(value[0])
  80. data3[1].append(value[1])
  81.  
  82. create_plot(data1, data2, data3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement