如何从绘图列表中找到数学函数

问题描述 投票:0回答:1

我想找到可以近似拟合以下所有点的数学函数:

plots

x = [560, 387, 280, 231, 196, 168, 148, 136, 124, 112, 104, 101, 93, 88, 84, 80, 76]
y = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90]

coefficients = np.polyfit(x, y, len(x) - 1)
polynomial = np.poly1d(coefficients)

但是如果我测试这个多项式,我会得到错误的值:

x1 = []
y1 = [] 
for i in range(276, 570, 1):
    x1.append(i)
    y1.append(polynomial(i))

plt.figure(figsize=(12,6))
plt.plot(x1, y1, 'o')
plt.show()

wrong_plots

python numpy
1个回答
0
投票

16度有点高了。选择较小的度数:

import numpy as np
from matplotlib import pyplot as plt

x = [560, 387, 280, 231, 196, 168, 148, 136, 124, 112, 104, 101, 93, 88, 84, 80, 76]
y = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90]

plt.figure(figsize=(12,6))
plt.plot(x, y, 'o')

for degree in (2, 3, 4, 5):
    coefficients = np.polyfit(x, y, degree)
    polynomial = np.poly1d(coefficients)

    x1 = np.arange(76, 570, 1)
    y1 = [polynomial(i) for i in x1] 
    plt.plot(x1, y1, '-')

plt.legend('data deg2 deg3 deg4 deg5'.split())
plt.show()

输出:

original data points and degree 2-5 plots

© www.soinside.com 2019 - 2024. All rights reserved.