Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-*- coding:UTF-8 -*-
- import math
- from BuildPlot import create_plot
- def function(x, y):
- return (y/(1+x))-0.5*y*y
- def tabulate_function(a, b, tabulate_step):
- y = []
- x = []
- current_x = a
- while current_x <= b:
- y.append(function(current_x))
- x.append(current_x)
- current_x += tabulate_step
- return [x, y]
- def method_runge_kutta_3(x, y, h):
- phi_0 = h * function(x, y)
- phi_1 = h * function(x + h / 2, y + phi_0 / 2)
- phi_2 = h * function(x + h, y - phi_0 + 2 * phi_1)
- return [x + h, (phi_0 + 4 * phi_1 + phi_2)/6 + y]
- def method_runge_kutta_4(x, y, h):
- phi_0 = h * function(x, y)
- phi_1 = h * function(x + h / 3, y + phi_0 / 3)
- phi_2 = h * function(x + 2*h / 3, y - phi_0 / 3 + phi_1)
- phi_3 = h * function(x + h, y + phi_0+phi_1+phi_2)
- return [x + h, y+ (phi_0+3*phi_1+3*phi_2+phi_3)/8]
- def method_prediction_and_correction(prev_x, prev_y, h):
- x = prev_x + h
- y = prev_y + h * function(prev_x, prev_y)
- y = prev_y + h * function(x, y) + (function(x, y) - function(prev_x, prev_y)) * h / 2
- return [x, y]
- if __name__ == "__main__":
- h = 0.1
- x = 0 # float(input("Write a: "))
- y = 1 # float(input("Write b: "))
- data1 = [[x], [y]]
- data2 = [[x], [y]]
- data3 = [[x], [y]]
- print("Лабораторная работа №3, вариант 12, часть 2: "
- "Используя метод Рунге-Кутта третьего, четвертого порядка точности и\n" +
- "метод прогноза и коррекции, вычислить значения y1, y2, y3 приближенного\n" +
- "решения заданного дифференциального уравнения первого порядка. Вычис-\n" +
- "ления производить с пятью знаками после запятой. Шаг h = 0.1 . Результаты\n" +
- "вычислений отобразить на графике.\n")
- print("Method Runge Kutta 3")
- value = [x, y]
- for i in range(3):
- value = method_runge_kutta_3(value[0], value[1], h)
- print("x[{2}] = {0}\ny[{2}] = {1}".format(value[0], value[1], i))
- data1[0].append(value[0])
- data1[1].append(value[1])
- print("\nMethod Runge Kutta 4")
- value = [x, y]
- for i in range(3):
- value = method_runge_kutta_4(value[0], value[1], h)
- print("x[{2}] = {0}\ny[{2}] = {1}".format(value[0], value[1], i))
- data2[0].append(value[0])
- data2[1].append(value[1])
- print("\nMethod prediction and correction")
- value = [x, y]
- for i in range(3):
- value = method_prediction_and_correction(value[0], value[1], h)
- print("x[{2}] = {0}\ny[{2}] = {1}".format(value[0], value[1], i))
- data3[0].append(value[0])
- data3[1].append(value[1])
- create_plot(data1, data2, data3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement