1

assume I have a cubic spline from point [0,0] to [10,10], with boundary values of zero derivative:

spl = scipy.interpolate.CubicSpline(x = [0,10], y = [0,10], bc_type=((1,0),(1,0)))

If I take the output of the spline evaluated at x as its y coordinate, I get a 'path' from the start point to the end point:

y_coordinate = spl(x_coordinate)

Now it is pretty straightforward to evaluate this spline for regularly spaced x, for example with numpy.linspace.

What should I do if I am interested in finding a sequence of x for which the euclidian distances in (x,y) space between sequential points are equal?

J.Galt
  • 377
  • 1
  • 2
  • 10

1 Answers1

1

When I want several things to be equal, I put them in an array and ask SciPy to minimize the variance of that array. It tends to work well.

In the code below, I decided (for better or for worse) to use only interior points as variables (called t), inserting the endpoints a, b before computing distances. The alternative is to use all points as variables, but impose constraints that t[0] == a and t[-1] == b.

import numpy as np
from scipy import interpolate, optimize
import matplotlib.pyplot as plt
spl = interpolate.CubicSpline(x = [0, 10], y = [0, 10], bc_type=((1, 0), (1, 0)))
def distances_var(t, a, b):
    atb = np.concatenate(([a], t, [b]))   # added endpoints
    y = spl(atb)
    dist_squared = np.diff(atb)**2 + np.diff(y)**2
    return np.var(dist_squared)
a, b = 0, 10
n = 7        # how many points to put
res = optimize.minimize(distances_var, np.linspace(a, b, n)[1:-1], args=(a, b))
x = np.concatenate(([a], res.x, [b]))  # the points we want
xx = np.linspace(a, b, 500)            # for plotting the curve
plt.axes().set_aspect('equal')
plt.plot(xx, spl(xx))
plt.plot(x, spl(x), 'ro')
plt.show()

spline