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 graphed in the range -2.5 <= x <= 2.5
:
How can I find the minimum point(s) of this curve within the given range, without using the discrete values used to graph the curve (by this I mean using only the continuous poly1d
object)?
Return the derivative of the specified order of a polynomial.
For example, poly1d([1, 2, 3]) returns an object that represents x 2 + 2 x + 3 , whereas poly1d([1, 2, 3], True) returns one that represents ( x − 1 ) ( x − 2 ) ( x − 3 ) = x 3 − 6 x 2 + 11 x − 6 . If True, c_or_r specifies the polynomial's roots; the default is False.
In this program, also, first, import the libraries matplotlib and numpy. Set the values of x and y. Then, calculate the polynomial and set new values of x and y. Once this is done, fit the polynomial using the function polyfit().
OK a bit different functions than @matiasg aim is to make more copyable code 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()
Image 1: Result. Note there is another local minima just outside your bounds ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With