-1

I have max nine points of a function as the following array in Python:

array([0.04625943, 0.04646331, 0.04636401, 0.04636489, 0.04651253,
       0.0462647 , 0.04549576, 0.04484105, 0.04463366], dtype=float32)

I use numpy library and need to polyfit(2nd order) this array and find the maximum of that polyfit.

How can this be achieved?

GNZ
  • 575
  • 2
  • 10
  • Does this help? https://stackoverflow.com/questions/4624970/finding-local-maxima-minima-with-numpy-in-a-1d-numpy-array – MSH Oct 13 '21 at 13:30
  • I dont want the max point of the array, but the max point of the 2nd order polyfit. – GNZ Oct 13 '21 at 13:31
  • https://stackoverflow.com/questions/53471058/how-compute-the-minimum-value-of-a-polynomial-in-a-specific-range-using-python – Chris Oct 13 '21 at 13:40

1 Answers1

1

In order to fit a polynomial, you need arrays of x and y values. Since your data consists of a single array, I am not sure how you use it with np.polyfit.

Assuming though that you have two arrays x and y, then np.polyfit(x, y, 2) will return an array of coefficients [a, b, c] of a second degree polynomial. If this polynomial has the maximum value (i.e. if a is a negative number) this maximal value will be attained at the point x0 = -b/(2*a). Thus, you just need to compute x0 and then evaluate the polynomial at this value.

bb1
  • 7,174
  • 2
  • 8
  • 23
  • This is a good answer, although perhaps you could point out that if the vertex is the min then the max will occur at one of the two endpoints (of whatever interval they are interested in), which is also easy to detect. – John Coleman Oct 13 '21 at 13:54
  • @JohnColeman I understood that the OP was asking for the absolute maximum of the function over the whole real line. If the range of arguments is bounded, then one also needs to consider the case when the vertex gives the maximum, but it is located outside the range. In which case the maximum will be still at one of the endpoints. – bb1 Oct 13 '21 at 14:01
  • @bb I calculate x0 but how do you find y max of the polyfit after that? – GNZ Oct 13 '21 at 14:05
  • @GNZ Plug it into the polynomial: `y = a*x0**2 + b*x0 + c`. – bb1 Oct 13 '21 at 14:06