2
$\begingroup$

This is a cross-post from Math.SE since the question has received very little attention. If it is too trivial, or in other ways not suited for this site, please let me know and I will remove it.

Suppose $f : \mathbb{R}^n \rightarrow \mathbb{R}$ is a differentiable (for simplicity, let's assume ${C}^\infty$) function, and we would like to find the following partial derivatives numerically (at an arbitrary point):

$$ \frac{\partial^2 f}{\partial x_i \partial x_j}, \frac{\partial^3 f}{\partial^2 x_i \partial x_k}, \frac{\partial^3 f}{\partial x_i \partial x_j \partial x_k} $$

up to some order in the accuracy (say, $\mathcal{O}(\Delta x_i^{q_i}, \Delta x_j^{q_j})$ for the former, $\mathcal{O}(\Delta x_i^{q_i}, \Delta x_j^{q_j}, \Delta x_k^{q_k})$ for the latter, where $q_i, q_j, q_k \in \mathbb{N}$).
For $i = j = k$, I can just use the finite difference coefficients (see for instance the Wiki article), and then for $i \neq j \neq k$ I suppose I could iteratively apply the above provedure, but this may be numerically unstable.
I could instead simply write down the coefficients by hand (see for example here, here, or here for related questions), but this quickly becomes unfeasible and rather error-prone for higher orders in accuracy, especially for the third mixed derivative, so I'm wondering if there is a multi-parameter generalization (i.e. for $i \neq j \neq k$) to the above so I can algorithmically generate the coefficients to some given order in accuracy of each parameter.
For instance, if $n=2$, and I'm looking for $\partial_x \partial_y f$ up to $\mathcal{O}(\Delta x^2, \Delta y^2)$, I should get as output the 2-tuple of points $[(1, 1), (1, -1), (-1, 1), (-1, -1)]$ and their corresponding coefficients $[1/4, -1/4, -1/4, 1/4]$ (as per this question).
Any advice or references would be appreciated!

$\endgroup$

1 Answer 1

2
$\begingroup$

An algorithm for this purpose has been developed by B. Fornberg in Calculation of Weights in Finite Difference Formulas.

It has been implemented in Mathematica, see the documentation:

enter image description here

$\endgroup$
2
  • $\begingroup$ I've tried using the FiniteDifferenceDerivative method, but I'm having trouble interpreting the result; namely, why is it that when I use a fine grid, the output seems to be an array with size > 1? For instance, this code outputs an array of size 9 x = {-1, 0, 1}; FiniteDifferenceDerivative[{1, 1}, {hx x, hy x}, Outer[f, hx x, hy x], DifferenceOrder->1], while if I take x={-1, 1}, I still get 4 element in the array, but they all seem to be identical. Are all those equivalent representations of that particular mixed derivative (i.e. with the same accuracy)? $\endgroup$
    – JCGoran
    Commented Jun 12, 2022 at 11:49
  • $\begingroup$ the output has as many elements as the number of lattice points you specify, $3\times 3=9$ in the first case and $2\times 2=4$ in the second case; end points contribute one-sided derivatives, which is why all elements are the same in the last example (where there are only end points); to increase the accuracy add intermediate points, for example, x = {-1, -1/2, 0, 1/2, 1} gives you an array of size $5\times 5$. $\endgroup$ Commented Jun 12, 2022 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .