[SciPy-user] Maximum of spline?

Andrew York Andrew.G.York+scipy@gmail....
Tue May 12 10:41:43 CDT 2009

 I know this is not exactly what you asked about, but I recently had a
similar problem. I approached it by using parabolas for interpolation,
since I know the location and value of the maximum/minimum of a
parabola. For example:

from scipy import array, arange, poly1d, polyfit, take, linspace

data_X = array([0, 1, 2, 3, 4])
data_Y = array([0, 5, 6, 4, 0])

sort_order = data_Y.argsort()

#Interpolate the three points closest to the data maximum.
#(assuming your maximum isn't at the edge of the dataset):
interp_points = sort_order[-1] + arange(-1, 2)
my_fit = poly1d(polyfit(
            take(data_X, interp_points),
            take(data_Y, interp_points),
            deg = 2

my_fit_data_X = linspace(0,4,20)
my_fit_data_Y = my_fit(my_fit_data_X)

#Since we used a parabola to interpolate, we know
# where the maximum is, and its value.
extremum_X = -my_fit[1]/(2*my_fit[2])
extremum_Y = my_fit(extremum_X)

#Now let's make sure the fit looks good.
from matplotlib.pyplot import figure, plot, hold, show, close

plot(data_X, data_Y)
plot(my_fit_data_X, my_fit_data_Y)
plot([extremum_X], [extremum_Y], 'rx')

print "Hit enter to continue..."

On Tue, May 12, 2009 at 6:35 AM, Alex <alex.liberzon@gmail.com> wrote:
> but spline is not a polynom - even using SymPy (symbolic computation)
> and deriving analytically the spline coefficients you'll remain with
> the "another" spline, i.e. set of coefficients that you need to
> evaluate. I believe there's not such a thing 'roots of the spline'.
> but maybe i'm wrong.
> On May 11, 9:12 pm, David F <dael...@gmail.com> wrote:
>> Alex <alex.liberzon <at> gmail.com> writes:
>> > maybe, if you know the range of the values, you can use the derivative
>> > of the spline, provided by
>> > scipy.interpolate.splev(xtuple,yourspline,der=1) or even second
>> > derivative using der=2?
>> Yes but that would still be approximate, and require evaluating
>> the (derivative of the) spline on some grid of points. However
>> since the splines are piecewise polynomials, I was looking for a
>> way to get the maximum just from the coefficients...
>> --D
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-u...@scipy.orghttp://mail.scipy.org/mailman/listinfo/scipy-user
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-user mailing list