0

Am using the np.polyfit and i observe the following kink when i do order 2. If i do order 1 it all seems fine.

Here is the code.

import numpy as np
import matplotlib.pyplot as plt

x = [-14.35,  -9.35,   0.65, -14.35  ,-9.35,   0.65] 
y = [ 0.10172312,  0.08831127,  0.07764486,  0.11606595 , 0.10447722,  0.1000171 ]

coeffs = np.polyfit(x, y, 2)
poly = np.poly1d(coeffs)

fig = plt.figure()
xs = np.array([-14.35,  -9.35 ,  0.65])
ys = poly(xs) 
plt.plot(x, y, 'o')
plt.plot(xs, ys)

This is what i see with order 2

This is what i see with order 1

Bit surprised why i see a kink with order 2 (polyfit(x, y, 2)

AMM
  • 17,130
  • 24
  • 65
  • 77

1 Answers1

3

There is nothing wrong with your polyfit, but you are using just 3 points to plot the resulting 2nd order polynomial. No wonder that it looks like that. Just generate a few more points using linspace. Just replace

xs = np.array([-14.35,  -9.35 ,  0.65])

with

xs = np.linspace(min(x), max(x), 200)

Result:

enter image description here

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62