Метод хорд
Автор: qwerty22222 • Декабрь 16, 2020 • Лабораторная работа • 390 Слов (2 Страниц) • 219 Просмотры
import math
import matplotlib.pyplot as plt
def f(x, y, z):
return -math.cosh(x) * z - 2 * y - 1
def runge_kutta(lambda_):
runge_kutta_y.clear()
runge_kutta_z.clear()
runge_kutta_y.append(y_0)
runge_kutta_z.append(lambda_)
curr_x = a + h
while curr_x <= b:
q0 = f(curr_x, runge_kutta_y[-1], runge_kutta_z[-1])
k0 = runge_kutta_z[-1]
q1 = f(curr_x + h / 2, runge_kutta_y[-1] + k0 * h / 2, runge_kutta_z[-1] + q0 * h /2)
k1 = runge_kutta_z[-1] + q0 * h / 2
q2 = f(curr_x + h / 2, runge_kutta_y[-1] + k1 * h / 2, runge_kutta_z[-1] + q1 * h / 2)
k2 = runge_kutta_z[-1] + q1 * h /2
q3 = f(curr_x + h, runge_kutta_y[-1] + k2 * h, runge_kutta_y[-1] + q2 * h)
k3 = runge_kutta_z[-1] + q2 * h
curr_x += h
runge_kutta_z.append(runge_kutta_z[-1] + h / 6 * (q0 + 2 * q1 + 2 * q2 + q3))
runge_kutta_y.append(runge_kutta_y[-1] + h / 6 * (k0 + 2 * k1 + 2 * k2 + k3))
return runge_kutta_y[-1]
runge_kutta_y = []
runge_kutta_z = []
arr_x = []
a = 0
b = 1
y_0 = 2
y_1 = 2
h = 1e-05
eps = 1e-05
cur_x = a
while cur_x <= b:
arr_x.append(cur_x)
cur_x += h
# метод стрельбы
count = 0
h_lambda = 10
runge_kutta_lambda = 10
prev_y = runge_kutta(runge_kutta_lambda)
runge_kutta_lambda += h_lambda
curr_y = runge_kutta(runge_kutta_lambda)
...