将popt喂入n根

时间:2017-10-11 15:20:11

标签: numpy scipy

我正在尝试确定多项式拟合,并用np.roots求解得到的多项式。然而,当将curve_fit数组馈入np.roots()时,它告诉我我有太多的参数。从np.roots()文档我明白它需要一个数组作为输入。 这是错误: TypeError:roots()只取1个参数(给定6个)

from scipy import optimize
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate


x=[25.257000000000001, 25.887, 26.565000000000001, 27.222999999999999, 27.891999999999999, 28.559999999999999, 29.238, 29.896000000000001, 30.555, 31.233000000000001, 31.911000000000001, 32.579000000000001, 33.238, 33.915999999999997, 34.584000000000003, 35.243000000000002, 35.911000000000001, 36.551000000000002, 37.228000000000002, 37.905999999999999, 38.545999999999999, 39.213999999999999, 39.863, 40.530999999999999, 41.200000000000003, 41.878, 42.536000000000001, 43.204999999999998, 43.872999999999998, 44.521999999999998, 45.189999999999998, 45.829999999999998, 46.508000000000003, 47.176000000000002, 47.835000000000001, 48.503, 49.170999999999999, 49.82, 50.478999999999999, 51.137999999999998, 51.816000000000003, 52.484000000000002, 53.143000000000001, 53.811, 54.478999999999999, 55.146999999999998, 55.816000000000003, 56.473999999999997, 57.143000000000001, 57.801000000000002, 58.478999999999999, 59.128, 59.805999999999997, 60.473999999999997, 61.143000000000001, 61.820999999999998, 62.469999999999999, 63.128, 63.796999999999997, 64.474999999999994]
y=[7009.1999999999998, 6920.8999999999996, 6898.5, 7067.1999999999998, 3647.9000000000001, 6973.3999999999996, 6601.8000000000002, 6862.0, 6697.8000000000002, 6673.1000000000004, 6537.8000000000002, 6427.1000000000004, 6888.3000000000002, 6768.3000000000002, 6578.8000000000002, 6678.6999999999998, 6543.3999999999996, 6794.5, 6558.5, 6952.1999999999998, 6741.3999999999996, 6577.3999999999996, 6728.1000000000004, 6740.1000000000004, 6618.8000000000002, 6580.1000000000004, 6801.5, 6498.6000000000004, 6417.1999999999998, 6503.3000000000002, 6386.5, 5991.3999999999996, 6271.1999999999998, 5630.6000000000004, 6001.3000000000002, 6196.1999999999998, 6381.0, 5783.3999999999996, 5721.8000000000002, 6024.0, 6015.6000000000004, 6133.6999999999998, 5987.0, 5607.3999999999996, 6129.6999999999998, 6211.1000000000004, 7037.3000000000002, 8266.7000000000007, 11961.0, 17120.0, 21857.0, 29003.0, 36856.0, 47164.0, 58358.0, 65570.0, 77873.0, 88707.0, 109440.0, 129630.0]
tck = interpolate.splrep(x, y, k=2, s=0)
dev_1 = interpolate.splev(x, tck, der=1)

def polyn(x, a, b, c, d, e, f):
    return a*x+b*np.power(x, 2)+c*np.power(x, 3)+d*np.power(x, 4)+e*np.power(x, 5)+f

def der_p(x, a, b, c, d, e, f):
    return a+2*b*x+3*c*np.power(x, 2)+4*d*np.power(x, 3)+5*e*np.power(x, 4)

p, e = optimize.curve_fit(polyn, x, y)
xd = np.linspace(50, 65, 100)


print(np.amax(dev_1))

print(np.roots(*p))

1 个答案:

答案 0 :(得分:0)

使用p而不是* p解决了问题。我不明白为什么。 *做什么?