简体   繁体   中英

Using numpy.polyfit

I have plotted a curve over some data using numpy.polyfit and am trying to find where the curve intersects a different line. However, I seem to be misunderstanding how the function works as when trying to use the coefficients produced to find values on the curve I am getting non-sensical answers.

2458880.2995 1.595
2458880.3046 1.62
2458880.3566 1.609
2458880.3585 1.599
2458880.7 1.667
2458880.7549 1.571

Here JD is the left column and the mag is the right column

x = JD
y = mag

coeffs = numpy.polyfit(x,y,2)

poly = numpy.poly1d(coeffs)

new_x = numpy.linspace(x[0], 2458940)
new_y = poly(new_x)

plt.plot(x,y,'x', new_x,new_y)

a,b,c = coeffs



# y = ax^2 + bx + c


xa = 2458880.2995

ya = a*(xa**2) + b*(xa) + c

print(ya)

This outputs a value of -2.827387571334839 when we expect a value close to 1.595

So the curve fits correctly over the data but when trying to use the coefficients produced I get incorrect answers.

import matplotlib.pyplot as plt
import numpy
import warnings
warnings.simplefilter('ignore', numpy.RankWarning)

JD = [2458880.2995,2458880.3046,2458880.3566,2458880.3585,2458880.7,2458880.7549] 
mag=[1.595,1.62,1.609,1.599,1.667,1.571]

x = JD
y = mag

coeffs = numpy.polyfit(x,y,2)

poly = numpy.poly1d(coeffs)

new_x = numpy.linspace(x[0], 2458940)
new_y = poly(new_x)

plt.plot(x,y,'x', new_x,new_y)

a,b,c = coeffs



# y = ax^2 + bx + c


xa = 2458880.2995

ya = a*(xa**2) + b*(xa) + c

print(ya)

i have added

import warnings
warnings.simplefilter('ignore', numpy.RankWarning)

as i was getting rankwarning error which caused the issue , now the output is 1.6 固定输出

"Rank warning means that the rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if full = False." numpy.ployfit in the last Rank Warning is mentioned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM