implemented most of the dynamic solvers

parent 0ac663f1
......@@ -3,19 +3,111 @@ import typing as tp
# TODO Метод цифра за цифрой
def cordic_method(fx: tp.Callable[[float], float], a: float, b: float) -> float:
"""Метод цифра за цифрой (CORDIC)"""
pass
# TODO Метод двойного пересчета
def double_recalculate_method(fx: tp.Callable[[float], float], a: float, b: float) -> float:
pass
def double_recalculate_method(
fx: tp.Callable[[float], float], a: float, b: float, steps: int = 1000, perc: float = 0.0001
) -> float:
"""Двойной пересчет"""
h = (b - a) / steps
ln = 0
l2n = 0
R = 0
while R < perc:
S2 = 0
x = a
# TODO Метод Рунге-Кутта
def runge_kutta_method(fx: tp.Callable[[float], float], a: float, b: float) -> float:
pass
while x <= b - h:
S2 += fx(x)
x += h
l2n = h * S2
R = abs(ln - l2n)
ln = l2n
h = h / 2
# TODO Метод Эйлера
def euler_method(fx: tp.Callable[[float], float], a: float, b: float) -> float:
pass
return l2n
def mysterious_method(
fx: tp.Callable[[float], float], a: float, b: float, steps: int = 1000, eps: float = 0.0001
) -> float:
"""Некий секретный алгоритм"""
h = (b - a) / steps
x = a
S1 = 0
while x <= b - steps:
S1 += fx(x)
x += h
S1 *= h
h2 = h / 2
S2 = 0
while True:
x = a + h2
while x <= b - h:
S2 += fx(x)
x += h
S2 *= h
if (S1 - S2) < eps:
break
h = h2
h2 = h2 / 2
S1 = S2
S2 = 0
return S2
def runge_kutty_method(
fx: tp.Callable[[float], float], a: float, b: float, n: int = 10, multi: int = 1
) -> float:
"""Метод Рунге-Кутты"""
y = 1
i = 1
h = ((b - a) / n) / multi
x = a
while x <= (b - h):
k1 = fx(x)
k2 = fx(x + h / 2)
k3 = fx(x + h / 2)
k4 = fx(x + h)
y += h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
x += h
i += 1
return y
def euler_method(
fx: tp.Callable[[float], float],
a: float,
b: float,
n: int = 10,
x0: float = 0.0,
multi: int = 1,
) -> float:
"""Метод Эйлера"""
x = [x0]
y = []
i = 0
h = ((b - a) / n) / multi
j = a
while j < (b - h):
y.append(y[i] + h * fx(x[i]))
x.append(x[i] + h)
j += h
i += 1
return y[i]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment