Let's say that I have some function $z = f(x,y)$.

I know that if I want to calculate the partial derivatives of $x$ and $y$ numerically that I can use finite differences, like the central differences method below:

$$\frac{dz}{dx} = \frac{f(x+\varepsilon, y) - f(x-\varepsilon,y)}{2*\varepsilon}\\ $$ and $$ \frac{dz}{dy} = \frac{f(x, y+\varepsilon) - f(x,y-\varepsilon)}{2*\varepsilon}$$

However, let's say that I have two (or more) triplets of $z=f(x,y)$. Like say $2 = f(4,5)$ and $3=f(8,2)$. Can I use that information to estimate partial derivatives?

My attempt at a guess on how to do this would be: $$\frac{dz}{dx} = \frac{3-2}{8-4} = \frac{1}{4}$$ and $$\frac{dz}{dy} = \frac{3-2}{2-5} = \frac{1}{-3}$$

Is that method valid? Are there better ways? What sort of accuracy can I expect?


$dz = \frac {\partial z}{\partial x} dx + \frac {\partial z}{\partial y} dy$

If you have 3 pairs of (x,y) you can find the vectors $\mathbf u = (x_1-x_0, y_1-y_0), \mathbf v = (x_2-x_0, y_2-y_0)$

$\frac {\partial z}{\partial \mathbf u} = \frac {z(x_1,y_1) - z(x_0,y_0)}{\|\mathbf u\|}\\ \frac {\partial z}{\partial \mathbf v} = \frac {z(x_2,y_2) - z(x_0,y_0)}{\|\mathbf v\|}$

There exists some $a,b$ such that $a\frac {\mathbf u}{\|\mathbf u\|} + b\frac {\mathbf v}{\|\mathbf v\|} = (1,0),$ and $m,n$ such that $m\frac {\mathbf u}{\|\mathbf u\|} + n\frac{\mathbf v}{\|\mathbf v\|} = (0,1)$

$\frac {\partial z}{\partial x} = \frac {a \frac {\partial z}{\partial \mathbf u}+ b\frac {\partial z}{\partial \mathbf v}}{\sqrt {a^2+b^2}}\\ \frac {\partial z}{\partial y} = \frac {m \frac {\partial z}{\partial \mathbf u}+ n\frac {\partial z}{\partial \mathbf v}}{\sqrt {m^2+n^2}}$

if $\|\mathbf u\|,\|\mathbf v\|, \sqrt {a^2+b^2},\sqrt {n^2+m^2}$ are large the errors in your estimates increase.


From two points separated by a vector in the direction $\hat{u}$, the best you can do is to estimate $\hat{u}\cdot \nabla f$.

Using more points you can indeed estimate the partial derivatives (as lnog as the set of sample points is not co-linear). One way to do that is by a weighted least-squares fit to a linear function that approximates $f$; the weighting should emphasize the points near to the point at which you need the gradient. In one dimension, the central difference method is equivalent to fitting to a line, using zero weighting on all but the nearest two points.

This is somewhat of an art, and numerical calculation of derivatives is trickier than numerical integration. For example, if you are allowed to choose the points for evaluation, then moving far from the desired point leave you vulnerable to the effect of higher derivatives, while moving too close leaves you vulnerable to arithmetic precision effects from subtracting two nearly-equal quantities.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.