# computing derivatives of sampled data

I am trying to calculate the derivatives of a sampled data set by following an example in this book. The data is a set of 2d quads(green area in below) and their vertices(4 each). The data is in the following example is sampled from $$z=f(x,y)=e^{-(x^2+y^2)}$$ but, in general, the underlying function might not be known.

The book states that the derivatives of any points are calculated by:

and

I can calculate df/dx and df/dy.

But where I struggle is calculating df/dz. I assume

and therefore df/dz = 0. But this cannot be correct since df/dz should be 1(by making the above analytic function implicit and differentiating it for z).

• Can I ask some questions for clarification?So you have a function, f(x,y), that you have sampled on a regular grid of (x,y) locations, to create a set of bilinear patches? Are those corner points, therefore, of the form (xi, yi, f(xi, yi))? That is Z is pointing up the screen? Oct 28 '19 at 8:47
• Exactly as you said
– ali
Oct 28 '19 at 13:36
• (Sigh. I wish the comments supported equations.) I don't understand why you are trying to manufacture a "df/dz". You have a surface, which to reduce confusion we'll call G(x,y) = (x, y, f(x,y)), and you need to compute on that surface the two partial derivatives (vectors), dG/dx and dG/dy. These are (1, 0, df/dx) and (0, 1, df/dy) respectively, where, unless I'm mistaken, df/dx is -2*x*f(x,y) etc If you just want your piecewise surface to look smooth say using Gouraud shading, evaluate the tangents at the sample points, create your normals (via cross products) and shade. Oct 28 '19 at 14:08
• @SimonF comments do support tex equations. For example: $\frac{\alpha_1}{\gamma}$ Oct 30 '19 at 15:56
• I'll try to write something tomorrow. Nov 5 '19 at 14:15

Rather than fit bilinear patches to your sample points, which are only C0 continuous$$^\spadesuit$$ may I instead suggest fitting the surface using Catmull-Rom splines? These will give you C2 continuity. Catmull-Rom is an interpolating spline which means it will pass through your sample points, as did the bilinear surface, but will do so smoothly from patch to patch.

$$^\spadesuit$$ because the first derivatives won't generally match at the boundaries of your patches

### Background/Assumption

You have a function $$F:\Re^2\to\Re^3$$ of the form $$F(s,t)=[s,t,f(s,t)]$$ where $$f:\Re^2\to\Re$$. The function, $$f$$, can be considered a 'black box' in the sense that we might not be given it explicitly.

$$F$$ has been regularly sampled as $$S_{i,j}=F(x_{scale}.i, y_{scale}.j, f(x_{scale}.i,y_{scale}.j))$$ where $$i,j\in\mathbb{Z}$$. You then want interpolate between these sample points using piecewise bicubic Catmull-Rom patches.

### Constructing patches

To form a particular bicubic Catmull-Rom patch, $$C_{i,j}$$, which has a given 2x2 set of samples $$\{S_{i,j}, S_{i+1,j}, S_{i,j+1}, S_{i+1,j+1}\}$$ at the corners, you need the set of 4x4 samples formed from the 2x2 at the centre and the surrounding 12 samples, i.e.$$\{S_{i-1,j-1}$$ through $$S_{i+2,j+2}\}$$, as shown below. (My apologies that this is 'hand placed' so is far from ideal)

If we just consider the behaviour of the surface at sample, $$S_{i,j}$$, then the partial derivative in the $$i$$ direction is given by $$\frac{\partial C_{ij}}{\partial i}=\frac{S_{i+1,j}-S_{i-1,j}}{2}$$ and similarly, in the $$j$$ direction by $$\frac{\partial C_{ij}}{\partial j}=\frac{S_{i,j+1}-S_{i,j-1}}{2}$$

Remembering these are vectors, you can use the cross product to generate a surface normal at any of the $$S_{i,j}$$ points$$^\clubsuit$$. (Though you may need an extra set of boundary samples)

Ali: You mentioned that both partial derivatives are vectors(in i,j directions). could you write each vector components plz

Certainly: If we say $$S_{i,j}=[x_{i,j},y_{i,j},z_{i,j}]^T$$ then $$\frac{\partial C_{ij}}{\partial i}=\frac{1}{2}.[(x_{i+1,j}-x_{i-1,j}),(y_{i+1,j}-y_{i-1,j}),(z_{i+1,j}-z_{i-1,j})]^T$$ $$\frac{\partial C_{ij}}{\partial j}=\frac{1}{2}.[(x_{i,j+1}-x_{i,j-1}),(y_{i,j+1}-y_{i,j-1}),(z_{i,j+1}-z_{i,j-1})]^T$$

If you've sampled the surface 'finely' enough you could probably choose to render the surface by just using 2 or 4 triangles per original patch.

Alternatively, you can recursively subdivide your quad mesh to get a finer set of triangles. To double the number of points in the 'i' direction, simply insert a new point between each $$S_{i,j}$$ and $$S_{i+1,j}$$ that equals ... $$\frac{9(S_{i,j}+S_{i+1,j}) - (S_{i-1,j}+S_{i+2,j})}{16}$$ Once you have the finer grid in 'i' simply repeat with 'j' with the appropriate equivalent set of points. You can repeat this ad nauseam to get whatever triangle resolution you desire ;-)

$$^\clubsuit$$ (Copying some text from the comments below): I've made a couple of valid assumptions here. Noting that if a given first (partial) derivative is non-zero (which is guaranteed in your case because of the way you construct the surface), then it also is locally tangential to that surface. Further, we know your two partial derivates aren't parallel, so the cross product will construct a valid normal. FWIW For some arrangements of control points, i.e. in more general use cases, you can get so-called 'degenerate' surfaces where you can't naively apply the above rules, but you're safe here.

• Thanks Simon. You mentioned that both partial derivatives are vectors(in i,j directions). could you write each vector components plz.
– ali
Nov 6 '19 at 23:41
• Thanks for the update. My math is not very good. I thought derivative is the rate of change of "function value" with respect to one of its arguments(unless it is a vector-valued function and you are assuming that the point S(x,y,z) is the function value); the gradient is a vector but not the partial derivatives, is that correct?
– ali
Nov 7 '19 at 22:43
• "I thought derivative is the rate of change of "function value" with respect to one of its arguments" That is indeed correct. But for a surface, if a given first (partial) derivative is non-zero (which is guaranteed in your case because of the way you construct the surface), then it also is locally tangential to the surface. Further, we know your two partial derivates aren't parallel, so the cross product will construct a valid normal. (FWIW For some arrangements of control points, in more general use cases, you can get so-called 'degenerate' surfaces, but you're safe here) Nov 8 '19 at 10:56
• "but for a surface, if a given first (partial) derivative is non-zero then it also is locally tangential...". I am not making a fuss :) but your argument already assumes partial derivative to be a vector(otherwise it won't be tangential) whereas I think it is scalar.
– ali
Nov 8 '19 at 14:24
• Perfect. It's all clear now. I had missed the capital F function.
– ali
Nov 8 '19 at 19:45