numODEsolver 1.0.0__tar.gz → 2.0.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: numODEsolver
3
- Version: 1.0.0
3
+ Version: 2.0.1
4
4
  Summary: Tool that solves ODE's numerically. Written in Python.
5
5
  Home-page: https://github.com/lukasnf/Numerical-ODE-Solver
6
6
  Classifier: Programming Language :: Python :: 3
@@ -10,12 +10,20 @@ Requires-Python: >=3.8
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
12
12
  Requires-Dist: numpy
13
- Requires-Dist: sympy
13
+ Requires-Dist: asteval
14
14
  Requires-Dist: scipy
15
+ Dynamic: classifier
16
+ Dynamic: description
17
+ Dynamic: description-content-type
18
+ Dynamic: home-page
19
+ Dynamic: license-file
20
+ Dynamic: requires-dist
21
+ Dynamic: requires-python
22
+ Dynamic: summary
15
23
 
16
24
  # Numerical ODE Solver
17
25
 
18
- 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.
26
+ 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 (optional). Note: Euler's Method is more inaccurate than RK4.
19
27
 
20
28
  ## Requirements
21
29
  Python version >= 3.8
@@ -28,21 +36,25 @@ pip install numODEsolver
28
36
  ```
29
37
  ## Example
30
38
  ```bash
31
- from numODEsolver import solver
39
+ import numODEsolver as ns
32
40
  import matplotlib.pyplot as plt #optional for plot
33
41
 
34
- solver = solver()
42
+ solver = ns.Solver()
35
43
  f = solver.get_function("y")
36
- x,y = solver.solve_rk4(f,x0=0,y0=1,n=10000,bound=10.1)
37
- print(solver.get_value(x,y,val=10,dec=5))
44
+ values = solver.rk4_1order(f,x0=0,y0=1,n=10000,bound=10.1)
45
+ n = solver.get_value(values,val=10,dec=5)
46
+ print(n)
47
+ plt.plot(results[:,0],results[:,1])
48
+ plt.show()
38
49
  ```
39
50
  ```bash
40
51
  from numODEsolver import solver
41
52
 
42
- solver = solver()
43
- f = solver.get_function_2order("dy")
44
- x,y,dy = solver.solve_euler_2order(f,x0=0,y0=1,dy0=1,n=10000,bound=5)
45
- print(solver.get_value(x,y,val=3,dec=2))
53
+ solver = ns.Solver()
54
+ f = solver.get_function("y'+y") #y' = for 1st derivative. The ode would be y'' = y'+y
55
+ x,y,dy = solver.euler_2order(f,x0=0,y0=1,dy0=1,n=10000,bound=5)
56
+ n = solver.get_value(values,val=3,dec=2)
57
+ print(n)
46
58
  ```
47
59
 
48
60
  # Version History
@@ -50,13 +62,14 @@ print(solver.get_value(x,y,val=3,dec=2))
50
62
  - 8.12.2024 - v0.1 -> only 1st order ODE's are solvable, more features coming soon
51
63
  - 15.12.2024 - v0.2 -> removed plot function
52
64
  - 26.1.2025 - v0.3 -> included 2nd order methods, see more on GitHub
65
+ - 18.03.2025 - v2.0 -> more secure function handeling, more stable numerical methods.
53
66
 
54
67
  ---
55
68
  For more documentation take a look at the source code on my GitHub.
56
69
 
57
- You can modify the script to add more methods, adjust default parameters, or change visualization settings.
70
+ You can modify the script to add more methods, or change visualization settings.
58
71
 
59
- This project is open-source and free to use. Contributions and suggestions are welcome!
72
+ Contributions and suggestions are welcome!
60
73
 
61
74
  Lukas
62
75
 
@@ -1,6 +1,6 @@
1
1
  # Numerical ODE Solver
2
2
 
3
- 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.
3
+ 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 (optional). Note: Euler's Method is more inaccurate than RK4.
4
4
 
5
5
  ## Requirements
6
6
  Python version >= 3.8
@@ -13,21 +13,25 @@ pip install numODEsolver
13
13
  ```
14
14
  ## Example
15
15
  ```bash
16
- from numODEsolver import solver
16
+ import numODEsolver as ns
17
17
  import matplotlib.pyplot as plt #optional for plot
18
18
 
19
- solver = solver()
19
+ solver = ns.Solver()
20
20
  f = solver.get_function("y")
21
- x,y = solver.solve_rk4(f,x0=0,y0=1,n=10000,bound=10.1)
22
- print(solver.get_value(x,y,val=10,dec=5))
21
+ values = solver.rk4_1order(f,x0=0,y0=1,n=10000,bound=10.1)
22
+ n = solver.get_value(values,val=10,dec=5)
23
+ print(n)
24
+ plt.plot(results[:,0],results[:,1])
25
+ plt.show()
23
26
  ```
24
27
  ```bash
25
28
  from numODEsolver import solver
26
29
 
27
- solver = solver()
28
- f = solver.get_function_2order("dy")
29
- x,y,dy = solver.solve_euler_2order(f,x0=0,y0=1,dy0=1,n=10000,bound=5)
30
- print(solver.get_value(x,y,val=3,dec=2))
30
+ solver = ns.Solver()
31
+ f = solver.get_function("y'+y") #y' = for 1st derivative. The ode would be y'' = y'+y
32
+ x,y,dy = solver.euler_2order(f,x0=0,y0=1,dy0=1,n=10000,bound=5)
33
+ n = solver.get_value(values,val=3,dec=2)
34
+ print(n)
31
35
  ```
32
36
 
33
37
  # Version History
@@ -35,13 +39,14 @@ print(solver.get_value(x,y,val=3,dec=2))
35
39
  - 8.12.2024 - v0.1 -> only 1st order ODE's are solvable, more features coming soon
36
40
  - 15.12.2024 - v0.2 -> removed plot function
37
41
  - 26.1.2025 - v0.3 -> included 2nd order methods, see more on GitHub
42
+ - 18.03.2025 - v2.0 -> more secure function handeling, more stable numerical methods.
38
43
 
39
44
  ---
40
45
  For more documentation take a look at the source code on my GitHub.
41
46
 
42
- You can modify the script to add more methods, adjust default parameters, or change visualization settings.
47
+ You can modify the script to add more methods, or change visualization settings.
43
48
 
44
- This project is open-source and free to use. Contributions and suggestions are welcome!
49
+ Contributions and suggestions are welcome!
45
50
 
46
51
  Lukas
47
52
 
@@ -0,0 +1 @@
1
+ from .solver import Solver
@@ -0,0 +1,83 @@
1
+ from scipy.interpolate import interp1d
2
+ import numpy as np
3
+ from asteval import Interpreter
4
+
5
+
6
+ # 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.
7
+ # --> Example: numODEsolver.get_function("x+y"), The diff.eq. would then be: y' = x+y and it will be numerically solved for y.
8
+ class Solver:
9
+ def __init__(self):
10
+ self.aeval = Interpreter()
11
+ def get_function(self, func):
12
+ func = func.lower()
13
+ if not "y'" in func:
14
+ f = self.aeval(f"lambda x,y: {func}")
15
+ return f
16
+ else:
17
+ func = func.replace("y'","dy")
18
+ f = self.aeval(f"lambda x,y,dy: {func}")
19
+ return f
20
+
21
+ def euler_2order(self,f,x0,y0,dy0,n,bound):
22
+ if n <= 0:
23
+ raise ValueError("n must be greater than zero.")
24
+ elif x0 > bound:
25
+ raise ValueError("initial condition must be < than the input bound!")
26
+ vals = np.zeros((n+1,3))
27
+ vals[0] = [x0,y0,dy0]
28
+ h = (bound-x0)/n
29
+ for i in range(n):
30
+ x,y,dy = vals[i]
31
+
32
+ dy_d = dy + h*f(x,y,dy)
33
+ y_d = y + h * dy_d
34
+ x_d = x + h
35
+ vals[i+1] = [x_d,y_d,dy_d]
36
+
37
+ return vals
38
+
39
+ def euler_1order(self,f,x0,y0,n,bound):
40
+ if n <= 0:
41
+ raise ValueError("n must be greater than zero.")
42
+ elif x0 > bound:
43
+ raise ValueError("initial condition must be < than the inpunt bound!")
44
+ vals = np.zeros((n+1, 2))
45
+ vals[0] = [x0,y0]
46
+ h = (bound-x0)/n
47
+ for i in range(n):
48
+ x,y = vals[i]
49
+ x_d = x0 + (i+1)*h
50
+ y_d = y + h*f(x,y)
51
+ vals[i+1] = [x_d,y_d]
52
+
53
+ return vals
54
+
55
+ def rk4_1order(self,f,x0,y0,n,bound):
56
+ if n <= 0:
57
+ raise ValueError("n must be greater than zero.")
58
+ elif x0 > bound:
59
+ raise ValueError("initial condition must be < than the inpunt bound!")
60
+ vals = np.zeros((n+1,2))
61
+ vals[0] = [x0,y0]
62
+ h = (bound-x0) / n
63
+ for i in range(n):
64
+ x,y = vals[i]
65
+
66
+ k1 = f(x,y)
67
+ k2 = f(x+h/2, y+h/2*k1)
68
+ k3 = f(x+h/2, y+h/2*k2)
69
+ k4 = f(x+h, y+h*k3)
70
+
71
+ x_d = x0 + (i+1)*h
72
+ y_d = y + h * (k1+2*k2+2*k3+k4)/6
73
+ vals[i+1] = [x_d,y_d]
74
+
75
+ return vals
76
+
77
+ # You can adjust the decimal places, note that rk4 is more accurate
78
+ def get_value(self,data,val,dec):
79
+ x_vals = data[:,0]
80
+ y_vals = data[:,1]
81
+ function = interp1d(x_vals,y_vals,kind="cubic")
82
+ num = np.round(function(val),dec)
83
+ return num
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: numODEsolver
3
- Version: 1.0.0
3
+ Version: 2.0.1
4
4
  Summary: Tool that solves ODE's numerically. Written in Python.
5
5
  Home-page: https://github.com/lukasnf/Numerical-ODE-Solver
6
6
  Classifier: Programming Language :: Python :: 3
@@ -10,12 +10,20 @@ Requires-Python: >=3.8
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
12
12
  Requires-Dist: numpy
13
- Requires-Dist: sympy
13
+ Requires-Dist: asteval
14
14
  Requires-Dist: scipy
15
+ Dynamic: classifier
16
+ Dynamic: description
17
+ Dynamic: description-content-type
18
+ Dynamic: home-page
19
+ Dynamic: license-file
20
+ Dynamic: requires-dist
21
+ Dynamic: requires-python
22
+ Dynamic: summary
15
23
 
16
24
  # Numerical ODE Solver
17
25
 
18
- 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.
26
+ 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 (optional). Note: Euler's Method is more inaccurate than RK4.
19
27
 
20
28
  ## Requirements
21
29
  Python version >= 3.8
@@ -28,21 +36,25 @@ pip install numODEsolver
28
36
  ```
29
37
  ## Example
30
38
  ```bash
31
- from numODEsolver import solver
39
+ import numODEsolver as ns
32
40
  import matplotlib.pyplot as plt #optional for plot
33
41
 
34
- solver = solver()
42
+ solver = ns.Solver()
35
43
  f = solver.get_function("y")
36
- x,y = solver.solve_rk4(f,x0=0,y0=1,n=10000,bound=10.1)
37
- print(solver.get_value(x,y,val=10,dec=5))
44
+ values = solver.rk4_1order(f,x0=0,y0=1,n=10000,bound=10.1)
45
+ n = solver.get_value(values,val=10,dec=5)
46
+ print(n)
47
+ plt.plot(results[:,0],results[:,1])
48
+ plt.show()
38
49
  ```
39
50
  ```bash
40
51
  from numODEsolver import solver
41
52
 
42
- solver = solver()
43
- f = solver.get_function_2order("dy")
44
- x,y,dy = solver.solve_euler_2order(f,x0=0,y0=1,dy0=1,n=10000,bound=5)
45
- print(solver.get_value(x,y,val=3,dec=2))
53
+ solver = ns.Solver()
54
+ f = solver.get_function("y'+y") #y' = for 1st derivative. The ode would be y'' = y'+y
55
+ x,y,dy = solver.euler_2order(f,x0=0,y0=1,dy0=1,n=10000,bound=5)
56
+ n = solver.get_value(values,val=3,dec=2)
57
+ print(n)
46
58
  ```
47
59
 
48
60
  # Version History
@@ -50,13 +62,14 @@ print(solver.get_value(x,y,val=3,dec=2))
50
62
  - 8.12.2024 - v0.1 -> only 1st order ODE's are solvable, more features coming soon
51
63
  - 15.12.2024 - v0.2 -> removed plot function
52
64
  - 26.1.2025 - v0.3 -> included 2nd order methods, see more on GitHub
65
+ - 18.03.2025 - v2.0 -> more secure function handeling, more stable numerical methods.
53
66
 
54
67
  ---
55
68
  For more documentation take a look at the source code on my GitHub.
56
69
 
57
- You can modify the script to add more methods, adjust default parameters, or change visualization settings.
70
+ You can modify the script to add more methods, or change visualization settings.
58
71
 
59
- This project is open-source and free to use. Contributions and suggestions are welcome!
72
+ Contributions and suggestions are welcome!
60
73
 
61
74
  Lukas
62
75
 
@@ -1,3 +1,3 @@
1
1
  numpy
2
- sympy
2
+ asteval
3
3
  scipy
@@ -8,13 +8,13 @@ classifiers = [
8
8
 
9
9
  setup(
10
10
  name="numODEsolver",
11
- version= "1.0.0",
11
+ version= "2.0.1",
12
12
  description= "Tool that solves ODE's numerically. Written in Python.",
13
13
  long_description=open("README.md").read(),
14
14
  long_description_content_type="text/markdown",
15
15
  url="https://github.com/lukasnf/Numerical-ODE-Solver",
16
16
  packages = ["numODEsolver"],
17
- install_requires=["numpy","sympy","scipy"],
17
+ install_requires=["numpy","asteval","scipy"],
18
18
  classifiers=classifiers,
19
19
  python_requires=">=3.8",
20
20
 
@@ -1 +0,0 @@
1
- from .solver import solver
@@ -1,84 +0,0 @@
1
- import sympy as sp
2
- from scipy.interpolate import interp1d
3
- import numpy as np
4
-
5
- class solver:
6
- # 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.
7
- # --> Example: numODEsolver.get_function("x+y"), The diff.eq. would then be: y' = x+y and it will be numerically solved for y.
8
- def get_function(self,func):
9
- x,y = sp.symbols("x y")
10
- func = sp.sympify(func)
11
- f = sp.lambdify((x,y), func, "numpy")
12
- return f
13
-
14
- # same get_function method, just for 2nd order odes you. Example: y'' = -y,
15
- def get_function_2order(self,func):
16
- x,y,dy = sp.symbols("x y dy")
17
- func = sp.sympify(func)
18
- f = sp.lambdify((x,y,dy), func, "numpy")
19
- return f
20
-
21
- # You can choose between 2 methods to solve the ODE numerically : euler and rk4. Those take the function f from before
22
- # 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.
23
- # Both return arrays with the x,y values
24
-
25
- # solves the ode with initial conditions y(x0) , dy(x0)
26
- def solve_euler_2order(self,f,x0,y0,dy0,n,bound):
27
- if n <= 0:
28
- raise ValueError("n must be greater than zero.")
29
- y = [y0]
30
- dy = [dy0]
31
- x = [x0]
32
- h = (bound-x0)/n
33
- for i in range(n):
34
- x_e2 = x[-1] + h
35
- y_e2 = y[-1] + h * dy[-1]
36
- dy_e2 = dy[-1] + h * f(x[-1],y[-1],dy[-1])
37
-
38
- x.append(x_e2)
39
- y.append(y_e2)
40
- dy.append(dy_e2)
41
-
42
- return x,y,dy
43
-
44
- def solve_euler(self,f,x0,y0,n,bound):
45
- if n <= 0:
46
- raise ValueError("n must be greater than zero.")
47
- y = [y0]
48
- x = [x0]
49
- h = (bound-x0)/n
50
- for i in range(n):
51
- y_e = y[-1] + h * f(x[-1], y[-1])
52
- x_e = x[-1] + h
53
-
54
- x.append(x_e)
55
- y.append(y_e)
56
-
57
- return x, y
58
-
59
- def solve_rk4(self,f,x0,y0,n,bound):
60
- if n <= 0:
61
- raise ValueError("n must be greater than zero.")
62
- y = [y0]
63
- x = [x0]
64
- h = (bound-x0) / n
65
- for i in range(n):
66
- k1 = h * f(x[-1], y[-1])
67
- k2 = h * f(x[-1] + h / 2, y[-1] + k1 / 2)
68
- k3 = h * f(x[-1] + h / 2, y[-1] + k2 / 2)
69
- k4 = h * f(x[-1] + h, y[-1] + k3)
70
-
71
- y_r = y[-1] + (k1 + 2 * k2 + 2 * k3 + k4) / 6
72
- x_r = x[-1] + h
73
-
74
- x.append(x_r)
75
- y.append(y_r)
76
-
77
- return x, y
78
-
79
- # Allows you to get the y-value from a specific x-value
80
- # You can adjust the decimal places, note that rk4 is more accurate
81
- def get_value(self,x,y,val,dec):
82
- function = interp1d(x,y,kind="cubic")
83
- num = np.round(function(val),dec)
84
- return num
File without changes
File without changes