# Finding the parameters of a function via curve fit

I'm trying to estimate the parameters (v, n, k) defined in fit_func. I tried the default least squares fit but I couldn't find the parameters successfully.

def fit_func(x, v, n, k):
return v * x ** n / (k ** n + x ** n)

x = [2.5,         2.71317829,  4.08,        4.18604651,  5.19379845,  6.92,
7.98449612,  8.94,        9.92248062,  9.94,       12.36,       13.48837209]
y = [0.16054661, 0.14643943, 0.11639118, 0.11796543, 0.15609638, 0.29527088,
0.40774818, 0.51331307, 0.6163489,  0.61807529, 0.78372639, 0.78643515]
popt, pcov = curve_fit(fit_func, x, y)
print(popt)
plt.plot(x, y, '*')
plt.plot(x, fit_func(x, *popt), 'r')
plt.show()


I get the following error:

    raise RuntimeError("Optimal parameters not found: " + errmsg)
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800.


I'm not sure if I have selected the right method. Suggestions on alternate methods that I could use to estimate the parameters will be really helpful.

Cross-posted: here

If you transform your formula and data to the reciprocals, you get $$\frac1y=\frac1v\left(\frac{k^n}{x^n}+1\right)$$ or $$y^{-1}=Ax^{-n}+B$$ The graph of this should be an $$n$$th power parabola elevated by some constant.

Now plotting the data in this fashion gives

which in the first 3 data points does not fit the expected behavior. Leaving these 3 points out

popt, pcov = curve_fit(fit_func, x[3:], y[3:], p0 = [1,3,20])


results in a fit with parameters

1.030581640553569 3.1928649383817267 8.923440277503085


and plot

• Wonderful explanation! I love it:) Could you please confirm the last value in popt? I get [1.03058199 3.19286183 8.92344026] . Thanks so much Oct 1, 2022 at 15:44
• Yes, that is correct, I used the wrong code variant to modify. I removed the $n$th power from $k$ to reduce complexity, without any change in the success or failure of the fit. // The initial guess used is rather fortunate, especially in the value of the power $n$ large deviations lead to negative exponents in the search, and a useless result. Oct 1, 2022 at 15:55