odesolver
A simple tool to solve ODEs numerically and plot the solution.
Features
- Solve ODEs with the Euler method
- Solve ODEs with the RK4 method
Installation
You can install the package via PyPI or from source.
Install from PyPI
pip install jp-odesolver
Install from Source (GitHub)
git clone https://github.com/patrikj-info/odesolver.git
cd odesolver
pip install .
Usage
Creating ODE
from odesolver.ode import ODE
# load homogenous ODE
ode = ODE.readHomogenousODE("4.0x'' + 0.1x' + 2.0x")
import numpy as np
def funct(t,x):
return np.cos(t)
# load inhomogenous ODE
ode = ODE.readODE("4.0x'' + 0.1x' + 2.0x", inhom=funct)
Solving using Euler's Method
from odesolver.odesolver import ODESolver
# find solution using Euler's method
sol = ODESolver.solveODE(ode=ode, method="euler", initial_conditions=[1.0, 0.0], t0=0, t_final=10, h=0.01)
Solving using Runge-Kutta Order 4
# find solution using Runge-Kutta 4
sol = ODESolver.solveODE(ode=ode, method="rk4", initial_conditions=[1.0, 0.0], t0=0, t_final=10, h=0.01)
Plotting Solution
from odesolver.solver import unpackData
from odesolver.plotter import Plotter
# retrieve data from solution
t,x = unpackData(data=sol, axes=(0,1))
# plot solution
Plotter.plot(t, x, filename="harm_oscillator_euler.png", fileformat="png")
Creating Animated Graphs
# plot solution
# NOTE: the creation of the graph may take a relatively long time. Changing the fps may prove useful.
Plotter.plot(t, x, animation=True, filename="harm_oscillator_euler.gif", fileformat="gif")
N-dimensional ODEs
Following, an example with N = 2 will be regarded.
from numpy import sqrt
import numpy as np
from odesolver.ode import ODE
from odesolver.plotter import Plotter
from odesolver.odesolver import ODESolver
g = 4 * np.pi ** 2 # Gravitational constant Astronomical Units
m = 3.00349e-6 # Earth's mass in solar mass units (sun=1 mass)
dist = 1 # Earth-Sun distance AU
v = 2 * np.pi # Orbital speed Earth (r=1AU) T=1 yr
def betrag(x):
sum = 0
for i in range(len(x)):
sum += x[i]**2
return sqrt(sum)
x0 = np.array([0, 0])
def funct(t, x):
return -g * x[0]/betrag(x[0] - x0)**3
ode = ODE(2, funct, 2)
sol = ODESolver.solveODE(ode, method="rk4", initial_conditions = [[0,dist], [v,0]], t0=0, t_final=200, h=0.01)
t, r, v = unpackData(sol)
Plotter.plot(xdata=r.T[0], ydata=r.T[1], filename="test.png", fileformat="png", xlabel="x", ylabel="y")
Metadata
Release files for jp-odesolver 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| jp_odesolver-0.0.4.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| jp_odesolver-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.8 kB
Release files / jp_odesolver-0.0.4.tar.gz
| Download URL | jp_odesolver-0.0.4.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e1cdcc334f7e12789fc8143607f5e77f2bad6f510ce16a5b0f7c0b61e81594f
|
|
BLAKE2b-256 checksum How to use checksums |
665ab9fb50954f05876516033ec6d79f2994c51ff6f57d389eeeb070bd119b40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|
Release files / jp_odesolver-0.0.4-py3-none-any.whl
| Download URL | jp_odesolver-0.0.4-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b1943259fbc26eb9fa5b53802626d385f98840584a2e11f2777ee6dbcccbff17
|
|
BLAKE2b-256 checksum How to use checksums |
cd3403ad6ed6ecac04367f08a8163dba8016d7631fd9ae66b96b39f13fbb811b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|