I am trying to find the optimal parameter values based on 2 dimensional data.
I searched for other Q&A threads and one of the questions looked similar to mine and the answer seemed to be duable link for me to replicate but I get the following error:
TypeError: only size-1 arrays can be converted to Python scalars
I rarely changed the codes but customized a bit only but seems like my equation does not accept Numpy arrays. Is there a way to address this issue and derive the parameter values?
Below is the code:
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
t_data = np.array([0,5,10,15,20,25,27])
y_data = np.array([1771,8109,22571,30008,40862,56684,59101])
def func_nl_lsq(t, *args):
K, A, L, b = args
return math.exp(L*x)/((K+A*(b**x)))
popt, pcov = curve_fit(func_nl_lsq, t_data, y_data, p0=[1, 1, 1, 1])
plt.plot(t_data, y_data, 'o')
plt.plot(t_data, func_nl_lsq(t_data, *popt), '-')
plt.show()
print(popt[0], popt[1], popt[2], popt[3])
For more clarifications, I also attach a screenshot of the (1) equation that I want to run NLS fitting and the data observations from the journal paper that I've read.
year(x_data) | value(y_data) |
---|---|
1960 | 1771 |
1965 | 8109 |
1970 | 22571 |
1975 | 30008 |
1980 | 40862 |
1985 | 56684 |
1987 | 59101 |