numODEsolver 0.1__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.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lukas F.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: numODEsolver
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Tool that solves and plots ODE's numerically. Written in Python.
|
|
5
|
+
Home-page: https://github.com/lukasnf/Numerical-ODE-Solver
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: numpy
|
|
13
|
+
Requires-Dist: matplotlib
|
|
14
|
+
Requires-Dist: sympy
|
|
15
|
+
Requires-Dist: scipy
|
|
16
|
+
|
|
17
|
+
# Numerical ODE Solver
|
|
18
|
+
|
|
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
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
Ensure the following Python libraries are installed:
|
|
24
|
+
|
|
25
|
+
Install it using `pip`:
|
|
26
|
+
```bash
|
|
27
|
+
pip install numODEsolver
|
|
28
|
+
```
|
|
29
|
+
## Example
|
|
30
|
+
```bash
|
|
31
|
+
solver = solver()
|
|
32
|
+
f = solver.get_function("cos(x)")
|
|
33
|
+
x,y = solver.solve_rk4(f,x0=0,y0=1,n=10000,bound=10.1)
|
|
34
|
+
solver.plot(x,y,xlabel="x",ylabel="y",title="rk4")
|
|
35
|
+
print(solver.get_value(x,y,val=10,dec=5)
|
|
36
|
+
```
|
|
37
|
+
For more documentation take a look at the source code on my GitHub.
|
|
38
|
+
|
|
39
|
+
You can modify the script to add more methods, adjust default parameters, or change visualization settings.
|
|
40
|
+
|
|
41
|
+
This project is open-source and free to use. Contributions and suggestions are welcome!
|
|
42
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
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,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
solver
|
solver/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .solver import solver
|
solver/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: solver.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
|