Skip to main content

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.

Metadata

Release files for pyrk 2023.11.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyrk 2023.11.9
File Size Uploaded
pyrk-2023.11.9.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyrk 2023.11.9
File Interpreter ABI Platform
pyrk-2023.11.9-py3-none-any.whl Python 3 none any Details

Total release size: 7.3 kB

Release files / pyrk-2023.11.9.tar.gz

Download URL pyrk-2023.11.9.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e76fc7296eab2d39d6d258cf311f37a6b6d16d6585f7273ea3b8137297ef5c3d
BLAKE2b-256 checksum
How to use checksums
6194ab523aae584bb60b0a6bcaa4b550737d56854f09ba507deaed05067e5f0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.6 Darwin/21.6.0

Release files / pyrk-2023.11.9-py3-none-any.whl

Download URL pyrk-2023.11.9-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b899bfb834ff7de4ad7605631589cdc56cf699b8188397c8674e8d5adcdd90f7
BLAKE2b-256 checksum
How to use checksums
bf7ec44fd9807f79dfd0a4568af2422d431ed5419544023c57e2d189fb077868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.6 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

2023.11.9 This release

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.1

2 release files

0.9.0

3 release files

0.8.1

2 release files

0.6.1

1 release file

0.6.0

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page