A simple Python project for solving and eventually plotting numerically obtained solutions for ODEs
Project description
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")
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
jp_odesolver-0.0.4.tar.gz
(8.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jp_odesolver-0.0.4.tar.gz.
File metadata
- Download URL: jp_odesolver-0.0.4.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e1cdcc334f7e12789fc8143607f5e77f2bad6f510ce16a5b0f7c0b61e81594f
|
|
| MD5 |
4fe286c13594a26f1f4815fc415fe7ad
|
|
| BLAKE2b-256 |
665ab9fb50954f05876516033ec6d79f2994c51ff6f57d389eeeb070bd119b40
|
File details
Details for the file jp_odesolver-0.0.4-py3-none-any.whl.
File metadata
- Download URL: jp_odesolver-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1943259fbc26eb9fa5b53802626d385f98840584a2e11f2777ee6dbcccbff17
|
|
| MD5 |
499f674fe65e1f654c7129642ad70334
|
|
| BLAKE2b-256 |
cd3403ad6ed6ecac04367f08a8163dba8016d7631fd9ae66b96b39f13fbb811b
|