How do I calculate the gradient of a discrete function? In the continuous case, I have
$$\lim_{x\to x_0} \frac{f(x) - f(x_0)}{x - x_0} = \lim_{h\to 0} \frac{f(x_0 +h) - f(x_0)}{h}$$
But what is the gradient of the function $f: \mathbb{N} \rightarrow \mathbb{N}$, for example $f(n) = n^2$? For example, at $n=4$ I would expect it to be either
$$5^2 - 4^2 = 25 - 16 = 9$$ or
$$(3^2 - 4^2)/(-1) = (9 - 16)/(-1) = 7$$
Which of both is the correct solution? Or is it something different like the mean of both?
 A: In discrete mathematics the role of derivatives is played by finite differences (and the role of integrals by partial sums of series). Your first definition is the more usual one for the $n$-th finite difference of a sequence, but the second one has the advantage that every sequence is the finite difference sequence of its own partial sum sequence (compare this to the main theorem of calculus that the derivative of a definite integral with respect to the upper limit of integration is the original function).
A: This is usually done by first extending the function to a differentiable function on a continuous domain and then taking the gradient of this extended function in each of the original grid points.
There are many possible ways to extend a discrete function. If a function is given on an integer grid (such as $\mathbb{Z}$ or $\mathbb{Z}^2$) then a very good and efficient way is interpolation with separable B-splines. Splines of degree two or three suffice for most purposes. In your example the extended function would be exactly $x \mapsto x^2$ and you would get $2n$ as the gradient at $x=n$.
An excellent exposition of this approach can be found here and here.
A: I think the proper "extension" of derivative in discrete mathematics that you are looking for, is the operation of finite difference. Clearly now you have the possibility of defining a forward difference operator (e.g. $(\Delta_{+} f)(k) = f_{k+1}-f_k$, your example one) and a backward one (say $(\Delta_{-} f)(k) = f_{k}-f_{k-1}$).
In a similar vein the analog of integration becomes summation. An interesting point is that a lot of results for the continuous case have a discrete analog. For example, partial integration, relating integration with derivative has an analogue "partial summation" which relates finite differences with summation. See here
