numODEsolver 0.1__py3-none-any.whl → 0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- numODEsolver/__init__.py +1 -0
- numODEsolver/solver.py +68 -0
- {numODEsolver-0.1.dist-info → numODEsolver-0.2.dist-info}/METADATA +15 -2
- numODEsolver-0.2.dist-info/RECORD +9 -0
- numODEsolver-0.2.dist-info/top_level.txt +1 -0
- solver/solver.py +1 -1
- numODEsolver-0.1.dist-info/RECORD +0 -7
- numODEsolver-0.1.dist-info/top_level.txt +0 -1
- {numODEsolver-0.1.dist-info → numODEsolver-0.2.dist-info}/LICENSE +0 -0
- {numODEsolver-0.1.dist-info → numODEsolver-0.2.dist-info}/WHEEL +0 -0
numODEsolver/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .solver import solver
|
numODEsolver/solver.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import matplotlib.pyplot as plt
|
|
2
|
+
import sympy as sp
|
|
3
|
+
from scipy.interpolate import interp1d
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
class solver:
|
|
7
|
+
|
|
8
|
+
# get_function() -> Takes a function of x and y as an input. Make sure to use correct python syntax and put everything in curved brackets.
|
|
9
|
+
# --> Example: numODEsolver.get_function("x+y"), The diff.eq. would then be: y' = x+y and it will be numerically solved for y.
|
|
10
|
+
def get_function(self,func):
|
|
11
|
+
x,y = sp.symbols("x,y")
|
|
12
|
+
func = sp.sympify(func)
|
|
13
|
+
f = sp.lambdify((x,y), func)
|
|
14
|
+
return f
|
|
15
|
+
|
|
16
|
+
# optional plot with matplotlib, pretty self-explanatory
|
|
17
|
+
def plot(self,x,y,xlabel,ylabel,title):
|
|
18
|
+
plt.plot(x,y)
|
|
19
|
+
plt.xlabel(xlabel)
|
|
20
|
+
plt.ylabel(ylabel)
|
|
21
|
+
plt.title(title)
|
|
22
|
+
plt.grid()
|
|
23
|
+
plt.show()
|
|
24
|
+
|
|
25
|
+
# You can choose between 2 methods to solve the ODE numerically : euler and rk4. Those take the function f from before
|
|
26
|
+
# as an input and 2 Initial conditions x0 and y0 as well as an x-axis bound. If n is a high number the result will be more accurate.
|
|
27
|
+
# Both return arrays with the x,y values
|
|
28
|
+
def solve_euler(self,f,x0,y0,n,bound):
|
|
29
|
+
if n <= 0:
|
|
30
|
+
raise ValueError("n must be greater than zero.")
|
|
31
|
+
y = [y0]
|
|
32
|
+
x = [x0]
|
|
33
|
+
h = (bound-x0)/n
|
|
34
|
+
for i in range(n):
|
|
35
|
+
y_e = y[-1] + h * f(x[-1], y[-1])
|
|
36
|
+
x_e = x[-1] + h
|
|
37
|
+
|
|
38
|
+
x.append(x_e)
|
|
39
|
+
y.append(y_e)
|
|
40
|
+
|
|
41
|
+
return x, y
|
|
42
|
+
|
|
43
|
+
def solve_rk4(self,f,x0,y0,n,bound):
|
|
44
|
+
if n <= 0:
|
|
45
|
+
raise ValueError("n must be greater than zero.")
|
|
46
|
+
y = [y0]
|
|
47
|
+
x = [x0]
|
|
48
|
+
h = (bound-x0) / n
|
|
49
|
+
for i in range(n):
|
|
50
|
+
k1 = h * f(x[-1], y[-1])
|
|
51
|
+
k2 = h * f(x[-1] + h / 2, y[-1] + k1 / 2)
|
|
52
|
+
k3 = h * f(x[-1] + h / 2, y[-1] + k2 / 2)
|
|
53
|
+
k4 = h * f(x[-1] + h, y[-1] + k3)
|
|
54
|
+
|
|
55
|
+
y_r = y[-1] + (k1 + 2 * k2 + 2 * k3 + k4) / 6
|
|
56
|
+
x_r = x[-1] + h
|
|
57
|
+
|
|
58
|
+
x.append(x_r)
|
|
59
|
+
y.append(y_r)
|
|
60
|
+
|
|
61
|
+
return x, y
|
|
62
|
+
|
|
63
|
+
# Allows you to get the y-value from a specific x-value
|
|
64
|
+
# You can adjust the decimal places, note that rk4 is more accurate
|
|
65
|
+
def get_value(self,x,y,val,dec):
|
|
66
|
+
function = interp1d(x,y,kind="cubic")
|
|
67
|
+
num = np.round(function(val),dec)
|
|
68
|
+
return num
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: numODEsolver
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2
|
|
4
4
|
Summary: Tool that solves and plots ODE's numerically. Written in Python.
|
|
5
5
|
Home-page: https://github.com/lukasnf/Numerical-ODE-Solver
|
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -19,6 +19,7 @@ Requires-Dist: scipy
|
|
|
19
19
|
This project is a Python implementation for solving ordinary differential equations (ODEs) numerically. The script allows users to choose between two methods: Euler's Method and Runge-Kutta 4th Order Method (RK4), providing a visual representation of the solution.
|
|
20
20
|
|
|
21
21
|
## Requirements
|
|
22
|
+
Python version >= 3.8
|
|
22
23
|
|
|
23
24
|
Ensure the following Python libraries are installed:
|
|
24
25
|
|
|
@@ -28,15 +29,27 @@ pip install numODEsolver
|
|
|
28
29
|
```
|
|
29
30
|
## Example
|
|
30
31
|
```bash
|
|
32
|
+
from numODEsolver import solver
|
|
33
|
+
import numpy as np #optional for constants
|
|
34
|
+
|
|
31
35
|
solver = solver()
|
|
32
|
-
f = solver.get_function("cos(x)")
|
|
36
|
+
f = solver.get_function("cos(x)") , f = solver.get_function("diff(y)")
|
|
33
37
|
x,y = solver.solve_rk4(f,x0=0,y0=1,n=10000,bound=10.1)
|
|
34
38
|
solver.plot(x,y,xlabel="x",ylabel="y",title="rk4")
|
|
35
39
|
print(solver.get_value(x,y,val=10,dec=5)
|
|
36
40
|
```
|
|
41
|
+
|
|
42
|
+
# Version History
|
|
43
|
+
|
|
44
|
+
- 8.12.2024 - v0.1 -> only 1st order ODE's are solvable, more features coming soon
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
---
|
|
37
48
|
For more documentation take a look at the source code on my GitHub.
|
|
38
49
|
|
|
39
50
|
You can modify the script to add more methods, adjust default parameters, or change visualization settings.
|
|
40
51
|
|
|
41
52
|
This project is open-source and free to use. Contributions and suggestions are welcome!
|
|
42
53
|
|
|
54
|
+
Lukas
|
|
55
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
numODEsolver/__init__.py,sha256=_riAnUkq4KYJB1oRnKV8pRgW3aIkytj2H0ZmtL5YfSk,26
|
|
2
|
+
numODEsolver/solver.py,sha256=qIdao9ATou9VwgX_KZUY7-NyLKaFiFQycHgr6IhDfmw,2273
|
|
3
|
+
solver/__init__.py,sha256=_riAnUkq4KYJB1oRnKV8pRgW3aIkytj2H0ZmtL5YfSk,26
|
|
4
|
+
solver/solver.py,sha256=qIdao9ATou9VwgX_KZUY7-NyLKaFiFQycHgr6IhDfmw,2273
|
|
5
|
+
numODEsolver-0.2.dist-info/LICENSE,sha256=B1LP4xi1JhMSHUlkccB-_2VoeKE8r2jbuvGFUzqJ70Y,1065
|
|
6
|
+
numODEsolver-0.2.dist-info/METADATA,sha256=59otoyM4bFOtK4GBcmbmm-T4igrbV-tVmEhTnjaCLaQ,1672
|
|
7
|
+
numODEsolver-0.2.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
8
|
+
numODEsolver-0.2.dist-info/top_level.txt,sha256=G-8gaHtECVQ5G30bl_5w1UUSZ7L380bwmNo-6_r74hc,13
|
|
9
|
+
numODEsolver-0.2.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
numODEsolver
|
solver/solver.py
CHANGED
|
@@ -6,7 +6,7 @@ import numpy as np
|
|
|
6
6
|
class solver:
|
|
7
7
|
|
|
8
8
|
# get_function() -> Takes a function of x and y as an input. Make sure to use correct python syntax and put everything in curved brackets.
|
|
9
|
-
# --> Example:
|
|
9
|
+
# --> Example: numODEsolver.get_function("x+y"), The diff.eq. would then be: y' = x+y and it will be numerically solved for y.
|
|
10
10
|
def get_function(self,func):
|
|
11
11
|
x,y = sp.symbols("x,y")
|
|
12
12
|
func = sp.sympify(func)
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
solver/__init__.py,sha256=_riAnUkq4KYJB1oRnKV8pRgW3aIkytj2H0ZmtL5YfSk,26
|
|
2
|
-
solver/solver.py,sha256=evjJYzMFA7oqqU19N08nCaNRgAL4NkXCYT303EEtsFg,2267
|
|
3
|
-
numODEsolver-0.1.dist-info/LICENSE,sha256=B1LP4xi1JhMSHUlkccB-_2VoeKE8r2jbuvGFUzqJ70Y,1065
|
|
4
|
-
numODEsolver-0.1.dist-info/METADATA,sha256=x8DzyvHHOX59o5NHkbvg7VovnDagEMrgqFZlAKufQeQ,1419
|
|
5
|
-
numODEsolver-0.1.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
6
|
-
numODEsolver-0.1.dist-info/top_level.txt,sha256=RIto_Z3phdVw6gFtSChYmUUkG-B565bDsyves_k6DGg,7
|
|
7
|
-
numODEsolver-0.1.dist-info/RECORD,,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
solver
|
|
File without changes
|
|
File without changes
|