In [1]:
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

In [7]:
x = np.array([5.05, 5.25, 5.91, 6.54, 7.11, 7.68, 8.26, 8.85, 9.44, 10.01, 10.62, 11.17, 11.75, 12.32, 12.89, 13.46, 14.05, 14.66, 15.28, 15.87, 16.52, 17.12, 17.75, 18.38, 19.04, 19.61, 20.33, 20.94, 21.60, 22.23, 22.91, 23.50, 24.12, 24.77, 25.43, 26.02, 26.68, 27.29, 27.95, 28.58, 29.22, 29.87, 30.53, 31.21, 31.86, 32.39, 32.97, 33.54, 34.09, 34.60, 35.03])

In [8]:
y = [-7.82, -7.78, -7.41, -7.04, -6.63, -6.14, -5.57, -4.96, -4.43, -3.94, -3.41, -2.88, -2.27, -1.70, -1.09, -0.52, -0.07, 0.41, 0.82, 1.23, 1.60, 1.85, 2.10, 2.31, 2.40, 2.56, 2.53, 2.58, 2.55, 2.52, 2.40, 2.25, 2.10, 1.90, 1.67, 1.48, 1.24, 0.97, 0.82, 0.58, 0.39, 0.24, 0.12, -0.03, -0.06, -0.05, -0.05, -0.08, -0.07, -0.07, -0.06]

In [9]:
x += 5

In [11]:
order = 4
plt.figure()
p = np.poly1d(np.polyfit(x, y, order))
xp = np.linspace(5, 50, 1000)
_ = plt.plot(x, y, '.', xp, p(xp), '-')



In [20]:
for order in range(8):
    plt.figure()
    p = np.poly1d(np.polyfit(x, y, order))
    xp = np.linspace(0, 50, 1000)
    _ = plt.plot(x, y, '.', xp, p(xp), '-')



In [6]:
p = np.poly1d(np.polyfit(x, y, 5))

In [7]:
p


Out[7]:
poly1d([ -3.98163970e-06,   7.21082206e-04,  -4.89847094e-02,
         1.53337076e+00,  -2.14171150e+01,   1.00303174e+02])

In [12]:
d = np.polyder(p, m=1)
roots = np.roots(d)
print(roots)


[ 37.51225502  26.19101762   7.31087591]

In [9]:
p(43.03356301)


Out[9]:
-0.15567085638127764

In [10]:
ynew = y - p(43.03356301)

In [11]:
p = np.poly1d(np.polyfit(x, ynew, 5))
d = np.polyder(p, m=1)
roots = np.roots(d)
print(roots)


[ 56.80676989  43.03356301  30.7123811   14.32874381]

In [12]:
p = np.poly1d(np.polyfit(x, ynew, 5))

In [13]:
p


Out[13]:
poly1d([ -3.98163970e-06,   7.21082206e-04,  -4.89847094e-02,
         1.53337076e+00,  -2.14171150e+01,   1.00458845e+02])

In [14]:
pp =p

In [15]:
# a = np.poly1d([1.24e-4, -9.28e-3, 2.02e-1, -7.92e-1, -8.08])
# a = np.poly1d([-3.9816e-06, 5.22e-4, -2.4123e-2, 4.5666e-1, -2.75987, -2.54728])
a = np.poly1d([-3.98164e-6, 7.2108e-4, -4.8985e-2, 1.53337, -2.1417e1, 1.004588e2])
p = a
xp = np.linspace(10, 45, 1000)
_ = plt.plot(x, ynew, '.', xp, p(xp), '-')



In [49]:
print(p)


            5             4           3         2
-3.982e-06 x + 0.0007211 x - 0.04899 x + 1.533 x - 21.42 x + 100.5

In [50]:
d = np.polyder(p, m=1)
roots = np.roots(d)
print(roots)


[ 56.79221699  43.0570073   30.70275464  14.32902487]

In [56]:
d


Out[56]:
poly1d([ -1.99082000e-05,   2.88432000e-03,  -1.46955000e-01,
         3.06674000e+00,  -2.14170000e+01])

In [51]:
d(14.32902487)


Out[51]:
1.3071179694179591e-09

In [53]:
dold = np.polyder(pp, m=1)

In [ ]:
d = np.polyder(p, m=1)

In [55]:
xp = np.linspace(10, 50, 10)
_ = plt.plot(xp, d(xp), '-', xp, dold(xp), ".")



In [ ]: