mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
45 lines
1014 B
Python
45 lines
1014 B
Python
import numpy as np
|
|
|
|
|
|
def runge_kutta(f, y0, x0, h, x_end):
|
|
"""
|
|
Calculate the numeric solution at each step to the ODE f(x, y) using RK4
|
|
|
|
https://en.wikipedia.org/wiki/Runge-Kutta_methods
|
|
|
|
Arguments:
|
|
f -- The ode as a function of x and y
|
|
y0 -- the initial value for y
|
|
x0 -- the initial value for x
|
|
h -- the stepsize
|
|
x_end -- the end value for x
|
|
|
|
>>> # the exact solution is math.exp(x)
|
|
>>> def f(x, y):
|
|
... return y
|
|
>>> y0 = 1
|
|
>>> y = runge_kutta(f, y0, 0.0, 0.01, 5)
|
|
>>> y[-1]
|
|
148.41315904125113
|
|
"""
|
|
n = int(np.ceil((x_end - x0) / h))
|
|
y = np.zeros((n + 1,))
|
|
y[0] = y0
|
|
x = x0
|
|
|
|
for k in range(n):
|
|
k1 = f(x, y[k])
|
|
k2 = f(x + 0.5 * h, y[k] + 0.5 * h * k1)
|
|
k3 = f(x + 0.5 * h, y[k] + 0.5 * h * k2)
|
|
k4 = f(x + h, y[k] + h * k3)
|
|
y[k + 1] = y[k] + (1 / 6) * h * (k1 + 2 * k2 + 2 * k3 + k4)
|
|
x += h
|
|
|
|
return y
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|