I need to compute the slope of a non-monotonic 2D function. Initially, based on the following example Cubic spline for non-monotonic data (not a 1d function) I was able to compute the cubic spline for such function.
# Define two non monotonic arrays
x = np.array # shape (101,)
y = np.array # shape (101,)
# defining arbitrary parameter to parameterize the curve
path_t = np.linspace(0, 1, x.size)
# this is the position vector with
# x coord (1st row) given by path_x, and
# y coord (2nd row) given by path_y
r = np.vstack((x.reshape((1, x.size)), y.reshape((1, y.size))))
# creating the spline object
spline = scipy.interpolate.interp1d(path_t, r, kind=3)
# defining values of the arbitrary parameter over which
# you want to interpolate x and y
# it MUST be within 0 and 1, since you defined
# the spline between path_t=0 and path_t=1
t = np.linspace(np.min(path_t), np.max(path_t), 101)
# interpolating along t
# r[0,:] -> interpolated x coordinates
# r[1,:] -> interpolated y coordinates
r = spline(t)
But I wasn't able to compute its derivative. Later, I used the scipy.interpolate.CubicSpline https://pageperso.lis-lab.fr/~francois.denis/IAAM1/scipy-html-1.0.0/generated/scipy.interpolate.CubicSpline.html#scipy.interpolate.CubicSpline to obtain the same output and compute the cubic spline derivative:
# Define two non monotonic arrays
x = np.array # shape (101,)
y = np.array # shape (101,)
# defining arbitrary parameter to parameterize the curve
path_t = np.linspace(0, 1, x.size)
# this is the position vector with
# x coord (1st row) given by path_x, and
# y coord (2nd row) given by path_y
r = np.vstack((x.reshape((1, x.size)), y.reshape((1, y.size))))
# creating the spline object
spline = scipy.interpolate.interp1d(path_t, r, kind=degree)
# defining values of the arbitrary parameter over which
# you want to interpolate x and y
# it MUST be within 0 and 1, since you defined
# the spline between path_t=0 and path_t=1
t = np.linspace(np.min(path_t), np.max(path_t), 101)
# interpolating along t
cSpline_func = CubicSpline(path_t, r, axis=1)
cSpline = cSpline_func(path_t)
cSpline_der = cSpline_func(path_t, 1)
My doubt is if cSpline_der is the slope of the non-monotonic function defined by 'x' and 'y'.
Thank you in advance for any help.