Get the minimum point of the curve numpy.poly1d

I have a numpy.poly1d polynomial as follows:

 c = np.poly1d([2,-4,-28,62,122,-256,-196,140,392,240,72]) 

The curve looks like this when displayed in the range -2.5 <= x <= 2.5 :

enter image description here

How to find the minimum point of this curve in a given range without using the discrete values ​​used to plot the graph (by this I mean using only the continuous object poly1d )?

+5
source share
2 answers

OK, slightly different functions than the goal of @matiasg is to make as much code as possible and use as much vectorized code as possible.

 import numpy as np from matplotlib.pyplot import * c = np.poly1d([2,-4,-28,62,122,-256,-196,140,392,240,72]) crit = c.deriv().r r_crit = crit[crit.imag==0].real test = c.deriv(2)(r_crit) # compute local minima # excluding range boundaries x_min = r_crit[test>0] y_min = c(x_min) plot( x_min, y_min, 'o' ) xc = np.arange(-2.5, 2.6, 0.02) yc = c(xc) plot( xc, yc) xlim([-2.5,2.5]) show() 

result

Image 1 : result. Note that there are other local lows outside your borders;)

+8
source

This gives you critical points, i.e. those x in the interval that the derivative is zero or they lie on the boundary of the interval. From this one should only evaluate and take min .

 In [22]: p = numpy.poly1d([2,-4,-28,62,122,-256,-196,140,392,240,72]) In [23]: bounds = [-2.5, 2.5] In [24]: crit_points = bounds + [x for x in p.deriv().r if x.imag == 0 and bounds[0] < x.real < bounds[1]] In [25]: crit_points Out[25]: [-2.5, 2.5, (-2.0243100544390678+0j), (1.8753707038871632+0j), (1.2307367723613383+0j), (-0.41217268372324861+0j)] 

From the graph in this case, it seems that min is the last.

+3
source

All Articles