![Header pic](https://github.com/walchko/pyrk/raw/master/pics/math2.jpg)
# Runge-Kutta
[![Actions Status](https://github.com/walchko/pyrk/workflows/pytest/badge.svg)](https://github.com/walchko/pyrk/actions)
![PyPI - License](https://img.shields.io/pypi/l/pyrk.svg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyrk.svg)
![PyPI - Format](https://img.shields.io/pypi/format/pyrk.svg)
![PyPI](https://img.shields.io/pypi/v/pyrk.svg)
A simple implementation of
[Runge-Kutta](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods)
for python.
## Usage
Integrates a function x_dot = f(time, x, u). See the examples in the
[docs](https://github.com/walchko/pyrk/blob/master/doc/runge-kutta.ipynb)
folder or a simple one:
``` python
from pyrk import RK4
import numpy as np
import matplotlib.pyplot as plt
def vanderpol(t, xi, u):
dx, x = xi
mu = 4.0 # damping
ddx = mu*(1-x**2)*dx-x
dx = dx
return np.array([ddx, dx])
rk = RK4(vanderpol)
t, y = rk.solve(np.array([0, 1]), .01, 200)
y1 = []
y2 = []
for v in y:
y1.append(v[0])
y2.append(v[1])
plt.plot(y1, y2)
plt.ylabel('velocity')
plt.xlabel('position')
plt.grid(True)
plt.show()
```
## Alternative
If you want to use `scipy` (which is good, but big), you can do:
```python
from scipy.integrate import solve_ivp as rk45
def func(t,x,u):
# cool differential equations
# ...
return x
t = 0
dt = 0.01
y = np.array([0,0,0]) # initial state
for _ in tqdm(range(steps)):
u = np.array([1,2,3]) # some inputs to func (i.e., control effort)
y = rk45(func, [t, t+step], y, args=(u,))
if y.success == False:
print("Oops")
y = y.y[:,-1]
# probably save t, u and y into arrays for plotting
t += step
```
# MIT License
**Copyright (c) 2015 Kevin J. Walchko**
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Raw data
{
"_id": null,
"home_page": "https://pypi.org/project/pyrk/",
"name": "pyrk",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "ode,rk4,runge,kutta",
"author": "walchko",
"author_email": "walchko@users.noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/61/94/ab523aae584bb60b0a6bcaa4b550737d56854f09ba507deaed05067e5f0a/pyrk-2023.11.9.tar.gz",
"platform": null,
"description": "![Header pic](https://github.com/walchko/pyrk/raw/master/pics/math2.jpg)\n\n# Runge-Kutta\n\n[![Actions Status](https://github.com/walchko/pyrk/workflows/pytest/badge.svg)](https://github.com/walchko/pyrk/actions)\n![PyPI - License](https://img.shields.io/pypi/l/pyrk.svg)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyrk.svg)\n![PyPI - Format](https://img.shields.io/pypi/format/pyrk.svg)\n![PyPI](https://img.shields.io/pypi/v/pyrk.svg)\n\nA simple implementation of\n[Runge-Kutta](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods)\nfor python.\n\n## Usage\n\nIntegrates a function x_dot = f(time, x, u). See the examples in the\n[docs](https://github.com/walchko/pyrk/blob/master/doc/runge-kutta.ipynb)\nfolder or a simple one:\n\n``` python\nfrom pyrk import RK4\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndef vanderpol(t, xi, u):\n dx, x = xi\n mu = 4.0 # damping\n\n ddx = mu*(1-x**2)*dx-x\n dx = dx\n\n return np.array([ddx, dx])\n\nrk = RK4(vanderpol)\nt, y = rk.solve(np.array([0, 1]), .01, 200)\n\ny1 = []\ny2 = []\nfor v in y:\n y1.append(v[0])\n y2.append(v[1])\n\nplt.plot(y1, y2)\nplt.ylabel('velocity')\nplt.xlabel('position')\nplt.grid(True)\nplt.show()\n```\n\n## Alternative\n\nIf you want to use `scipy` (which is good, but big), you can do:\n\n```python\nfrom scipy.integrate import solve_ivp as rk45\n\ndef func(t,x,u):\n # cool differential equations\n # ...\n return x\n\nt = 0\ndt = 0.01\ny = np.array([0,0,0]) # initial state\n\nfor _ in tqdm(range(steps)):\n u = np.array([1,2,3]) # some inputs to func (i.e., control effort)\n\n y = rk45(func, [t, t+step], y, args=(u,))\n\n if y.success == False:\n print(\"Oops\")\n\n y = y.y[:,-1]\n\n # probably save t, u and y into arrays for plotting\n t += step\n```\n\n# MIT License\n\n**Copyright (c) 2015 Kevin J. Walchko**\n\nPermission is hereby granted, free of charge, to any person obtaining a\ncopy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\nOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "ode integration rk4 rk runge kutta",
"version": "2023.11.9",
"project_urls": {
"Homepage": "https://pypi.org/project/pyrk/",
"Repository": "https://github.com/walchko/pyrk"
},
"split_keywords": [
"ode",
"rk4",
"runge",
"kutta"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bf7ec44fd9807f79dfd0a4568af2422d431ed5419544023c57e2d189fb077868",
"md5": "1134d0ccc37f32a4a8215e13f68e8729",
"sha256": "b899bfb834ff7de4ad7605631589cdc56cf699b8188397c8674e8d5adcdd90f7"
},
"downloads": -1,
"filename": "pyrk-2023.11.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1134d0ccc37f32a4a8215e13f68e8729",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 3887,
"upload_time": "2023-11-10T05:58:12",
"upload_time_iso_8601": "2023-11-10T05:58:12.060503Z",
"url": "https://files.pythonhosted.org/packages/bf/7e/c44fd9807f79dfd0a4568af2422d431ed5419544023c57e2d189fb077868/pyrk-2023.11.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6194ab523aae584bb60b0a6bcaa4b550737d56854f09ba507deaed05067e5f0a",
"md5": "660502bb6793ae6a81c9eb368a1b2023",
"sha256": "e76fc7296eab2d39d6d258cf311f37a6b6d16d6585f7273ea3b8137297ef5c3d"
},
"downloads": -1,
"filename": "pyrk-2023.11.9.tar.gz",
"has_sig": false,
"md5_digest": "660502bb6793ae6a81c9eb368a1b2023",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 3380,
"upload_time": "2023-11-10T05:58:13",
"upload_time_iso_8601": "2023-11-10T05:58:13.634606Z",
"url": "https://files.pythonhosted.org/packages/61/94/ab523aae584bb60b0a6bcaa4b550737d56854f09ba507deaed05067e5f0a/pyrk-2023.11.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-10 05:58:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "walchko",
"github_project": "pyrk",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pyrk"
}