matlab - Rational function curve fitting in python -
i trying fit curve x , y data points using rational function. can done in matlab using cftool (http://de.mathworks.com/help/curvefit/rational.html). however, looking same in python. have tried use scipy.optimize.curve_fit, requires function, don't have.
you have function, rational function. need set function , perform fitting. curve_fit requires supply arguments not lists, supplied additional function fitting on specific case of third degree polynomial in both numerator denominator.
def rational(x, p, q): """ general rational function description. p list polynomial coefficients in numerator q list polynomial coefficients (except first one) in denominator first coefficient of denominator polynomial fixed @ 1. """ return np.polyval(p, x) / np.polyval([1] + q, x) def rational3_3(x, p0, p1, p2, q1, q2): return rational(x, [p0, p1, p2], [q1, q2]) x = np.linspace(0, 10, 100) y = rational(x, [-0.2, 0.3, 0.5], [-1.0, 2.0]) ynoise = y * (1.0 + np.random.normal(scale=0.1, size=x.shape)) popt, pcov = curve_fit(rational3_3, x, ynoise, p0=(0.2, 0.3, 0.5, -1.0, 2.0)) print popt plt.plot(x, y, label='original') plt.plot(x, ynoise, '.', label='data') plt.plot(x, rational3_3(x, *popt), label='fit')