Skip to main content

ode integration rk4 rk runge kutta

Project description

Header pic

Runge-Kutta

Actions Status PyPI - License PyPI - Python Version PyPI - Format PyPI

A simple implementation of Runge-Kutta for python.

Usage

Integrates a function x_dot = f(time, x, u). See the examples in the docs folder or a simple one:

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:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrk-0.9.5.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

pyrk-0.9.5-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pyrk-0.9.5.tar.gz.

File metadata

  • Download URL: pyrk-0.9.5.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.5 Darwin/21.6.0

File hashes

Hashes for pyrk-0.9.5.tar.gz
Algorithm Hash digest
SHA256 cc0931cd949aff70b0650f5285338b100e36d9d394f34228a1da17bfe145a891
MD5 3c7cef58e7aa87b1182a6e131c03eefa
BLAKE2b-256 fb8a3aace08b54685f251872ba46be2f31a7e36e243382f3e1845d48a1e366ff

See more details on using hashes here.

File details

Details for the file pyrk-0.9.5-py3-none-any.whl.

File metadata

  • Download URL: pyrk-0.9.5-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.5 Darwin/21.6.0

File hashes

Hashes for pyrk-0.9.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6c782fd5932d91cb38d8e9135db4bdcd04a9e55f874c1eb0ece0d53220994d36
MD5 44642886f7f5174d518f168f2e4328d6
BLAKE2b-256 eb87a906324fd177bf0f9ca42d703695bff1e68c583c184c44afaacec3c0ab5f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page