Closed kaizenlabs closed 2 years ago
Hi,
FinDiff operators will only use the side points if they are available. So at the boundary, they will automatically only use the forward or backward points.
However, if you also only want to use backward points in the interior, you would have to compute the derivative manually from the finite difference coefficients. Those coefficients can be computed for arbitrary points like that:
from findiff import coefficients
deriv = 1
coefs = coefficients(deriv, offsets=[0, -1, -2])
which gives
{'coefficients': [3/2, -2, 1/2], 'offsets': [0, -1, -2], 'accuracy': 2}
The derivative of an array f at an index k would then be something like that:
df_dx_k = 0
for c, o in zip(coefs['coefficients'], coefs['offsets']):
df_dx_k += c * f[k+o]
df_dx_k /= dx
Thank you for asking this question. That reminds me that the arbitrary offset specification still needs to be implemented in the FinDiff operators themselves so that one doesn't have to apply the coefficients manually.
Hey thank you for this! But I just tried using that and k
and dx
are not defined. Could you clarify?
Also to do higher order derivatives using this method (2nd, 3rd, 4th order), you would just change deriv = 2
or whatever the order derivative to get the correct coefficients with offsets=[0,-1,-2]
still, correct?
Hi! Just bumping my own question above, could you clarify your solution?
Sorry, lately I'm a bit slow to respond.
k
is the index of the array where you want to evaluate your derivative and dx
is the grid spacing. If x
is the array with the (equidistant) grid points, then dx = x[1] - x[0]
.
Yes, for higher derivatives, you would set the deriv
argument accordingly, but then you need more points to evaluate. That is, three offsets are no longer sufficient.
Hi, my calculus is terrible. How would I calculate the backward diff only (1st order and 2nd order) between 2-3 points? I noticed the Findiff by default is calculating the center which relies on x+1. Just curious if there's a setting to calculate the diff a 0 using only 2-3 past values?