Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get minimum point(s) of numpy.poly1d curve

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:

enter image description here

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)?

like image 959
Conor Taylor Avatar asked Apr 14 '15 17:04

Conor Taylor


People also ask

What does poly1d return?

Return the derivative of the specified order of a polynomial.

How does Numpy poly1d work?

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.

How do I use Polyfit in Numpy?

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().


1 Answers

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()

result

Image 1: Result. Note there is another local minima just outside your bounds ;)

like image 198
joojaa Avatar answered Sep 24 '22 10:09

joojaa