Skip to content

Commit 0636108

Browse files
phmbressangiovaniceotto
andcommitted
ENH: add second order derivatives.
Co-authored-by: giovaniceotto <[email protected]>
1 parent 1d39356 commit 0636108

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

rocketpy/Function.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,23 +2325,29 @@ def integral(self, a, b, numerical=False):
23252325
ans, _ = integrate.quad(self, a, b, epsabs=0.001, limit=10000)
23262326
return integrationSign * ans
23272327

2328-
def differentiate(self, x, dx=1e-6):
2329-
"""Evaluates the derivative of a 1-D Function at x with a step size of dx.
2328+
def differentiate(self, x, dx=1e-6, order=1):
2329+
"""Differentiate a Function object at a given point.
23302330
23312331
Parameters
23322332
----------
23332333
x : float
2334-
Point at which to evaluate the derivative.
2334+
Point at which to differentiate.
23352335
dx : float
2336-
Step size to use in the derivative calculation. Default is 1e-6.
2337-
2336+
Step size to use for numerical differentiation.
2337+
order : int
2338+
Order of differentiation.
23382339
23392340
Returns
23402341
-------
2341-
result : float
2342+
ans : float
23422343
Evaluated derivative.
23432344
"""
2344-
return (self.getValue(x + dx) - self.getValue(x - dx)) / (2 * dx)
2345+
if order == 1:
2346+
return (self.getValue(x + dx) - self.getValue(x - dx)) / (2 * dx)
2347+
elif order == 2:
2348+
return (
2349+
self.getValue(x + dx) - 2 * self.getValue(x) + self.getValue(x - dx)
2350+
) / dx**2
23452351

23462352
def derivativeFunction(self):
23472353
"""Returns a Function object which gives the derivative of the Function object.

0 commit comments

Comments
 (0)