How much does a curve bend? That looks like an important question to ask. Indeed, it is THE question to ask because curvature is everything we need to know about a curve (modulo some annoying *groups* we will talk about in the future). If you are too shy to ask, you can *compute* it and that is what this post is about. In order to compute the curvature you need a bunch of things and for each one there is a bunch of ways of doing it, so, let’s talk about some of them.

**Derivatives**

Consider the curve (yeah, we are going to work in two dimensions where everything is nice). It has coordinates and that change according with a parameter . The derivative of the curve is the rate of change of its coordinates, so, let’s just focus in one of them.

The derivate is the rate of change. How much change in a minute, for example, if our parameter represent time in minutes as a unit. There are three ways of expressing this numerically:

Forward:

Backwards:

Central:

This is only an *approximation*, because, if you remember your calculus lectures, the *true* derivative occurs in the limit . Your approximation is then as good as small is your in the denominator. If you ask a numerical analyst, she will say: “use the central one!” (hope you spot the reason for that). Awesome! The problem is that in the fringes of your data (yes, you can not get infinite data), that scheme isn’t defined for obvious reasons.

What to do? Well, the general recipe is like this: use the forward difference for your first point, continue with the central for the interior ones and do the backwards for the last one. That is precisely what the function “gradient” from Matlab® does, so, if you are using Matlab, use that function instead of “diff” to compute derivatives. This is not perfect of course because as you can see in the next figure, where I tried to compute the derivative of , the first and last points will be much less precise that the rest (as much as and differ in orders of magnitude).

It seems that we are condemned to lose one data point every time we compute a derivative. If you are not willing to pay that price and feel a bit adventurous, you can try the following scheme.

Fit a polynomial to each of your coordinate functions. For example, if you are using Matlab, you can use the function “polyfit” or you can just do it manually. Then you can differentiate that polynomial “analytically” (because derivatives of polynomials are easy) and then evaluate the result at the points you want. Here is a small code that does exactly that:

degree = 10; r_poly = polyfit( t, gamma, degree ); rp_poly = polyder( r_poly ); rp_poly_vals = polyval(rp_poly, t);

The result depends a lot on the degree of your polynomial and it is a dangerous when your function has sharp changes, so use it wisely! (I mean, you are assuming a lot of things about that curve). Let’s move on to the next thing we need, remember, the aim is to compute the curvature!

**Parametrization**

You can travel along the curve at different speeds. In general, for any *continuous*, *monotonic* function , is the same curve (as a set of points), just traversed at different speed!. is called a *reparametrization* of the curve. A valid reparametrization would be, for example, . As an example of the different reparametrizations, the next figure shows an astroid traversed at three different speeds.

The bottom line is: there are infinite ways of specify a curve. There is of course one special reparametrization called the *arc-length parametrization* which every one likes because corresponds to the traversal of the curve with *unit speed*. In our case that’s helpful because the first derivative for all , and that easies the computation of curvature.

Here is how you arc-length reparametrize a curve. First you need to compute the arc-length. The arc length at is the length of the curve (like, literally) till that time. Funny thing, it is obtained by measuring the length of the tangent vector (derivative) at each point and adding them together. In math:

Assume , then, the total length of the curve is . Setup a uniform partition of . For each , find a such that . The ordered pairs are a discretization of the parametrization sought after. Therefore, is arc length parametrized (check the domains and ranges of each function to see they correspond).

The arc-length parametrized astroid from the previous example is shown in the following figure (notice how on the right, the tangent vector has constant length)

**Curvature**

Now we can compute the curvature. I will mention three ways of doing it. First, we can just use the known formula for the curvature in two dimensions

I already showed how to compute the derivatives numerically, so we can compute the whole thing directly. This approach is very unstable in the sense that even small fast changes will blow up the curvature, so it is not very appropriate for noisy data. Alternatively, as proposed in [1], we can get the required first and second derivatives from a fitting procedure as follows:

It is known that, in the vicinity of , the curve can be approximated by

Now, consider the following linear regression problem:

,

if we solve it, we will get our derivatives for free: would be and , (Ah! this is another method for computing the derivatives). Let’s solve it by least squares. For each , choose a discrete neighborhood of (now we are working with the discrete curve, the one you actually have in your computer): and construct the matrices and vectors:

, and

The regression problem can be formulated as and its solution:

,

which can be solved easily with Matlab or any other scientific computing software (you actually solve the problem for each coordinate separately). Cool! isn’t it?

The last way of computing the curvature is to first find the arc-length parametrization and then chill out because, given that , the formula for reduces to:

,

so, the curvature at each point is just the *length* of the acceleration vector along the path in an arc-length parametrized curve!

Let’s see how all these formulas work in real life. In the following figure I show the curvature of a logarithmic spiral. The theoretical value is shown in red and the computed value in black. The fact that you can barely see the black trace means that all 3 schemes work quite well, except in the fringes (where the small inaccuracies we talked about before are amplified greatly).

In the case of the ellipse all three schemes do well, again. You can see the four extrema that are expected for every closed curve (four vertex theorem). Everything’s smooth, everything is easy. Too easy maybe.

To complicate things a bit, let’s add some noise (imperceptible). As we see in the next figure, things blow up out of control for all but the polyfit scheme which does just fine.

Finally, In our initial example, that of the Astriod, the schemes have some trouble with the singularities which, after observing the analytical expression for the curvature,

,

we discover to be located at (The arc length scheme itself requires some kind of fitting for the interpolation of the required values of time, this can be adjusted to try to get better results).

Noise doesn’t help in this case either. The polyfit scheme starts having problems with the singularities as we start increasing the size of the neighborhood over which we perform the fitting.

A final word about the *sign* of the curvature. In two dimensions the curvature has a sign which can be set up by choosing it based on the normal vector. I will talk about the Frenet frame in a future post, for now, I chose the sign arbitrarily. Also in a future post I will explore – dimensional curvatures.

**References**

[1] Younes, L. (2010). *Shapes and diffeomorphisms* (Vol. 171). Springer Science & Business Media.