My curvefitting code shows up an error while using Scipy.optimize.curvefit function.I feel this issue has started after I have inserted bounds for my initial guess.
please suggest what has to be done.Attached the code and error trace.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
def ssexp_if(x, a, b, c):
arg = (b * x) ** c
g = a * np.exp(-2 * arg)
g[~np.isfinite(g)] = 0.0
return g
datafile = pd.read_excel(r'C:\Users\Trial12.xls', header=None)
x = datafile.iloc[ : , 0]
y = datafile.iloc[ : , 1]
first = 6 #change the index after inserting a heading to the excel
last = 90
index = 10
xdatam = x[first:last]
ydatam = y[first:last]
listfirst = 6
a = np.mean(ydatam[first+listfirst:first+10+listfirst])
b = (np.log(ydatam[first+listfirst]/ydatam[index+first+listfirst]))/(2*(xdatam[index+first+listfirst]-xdatam[first+listfirst]))
c = 1
print("a", a, "b", b, "c", c)
lb = [0, 0, 0]
ub = [100, 1e5, 1]
yfit = ssexp_if(x, a, b, c)
params, pcov = curve_fit(ssexp_if, x, yfit, bounds=(lb, ub))
print(params)
plt.semilogx(x, y, 'ko', label="Original Noised Data")
plt.semilogx(x, ssexp_if(x, *params), 'r-', label="Fitted Curve")
plt.legend()
plt.show()
Error:
C:/Users/CurveFitting_Python_UsingFun.py:8: RuntimeWarning: overflow encountered in exp
g = a * np.exp(-2 * arg)
C:\Users\\PycharmProjects\CurveFitting_Python\venv\lib\site-packages\scipy\optimize\_lsq\trf.py:355: RuntimeWarning: invalid value encountered in double_scalars
actual_reduction = cost - cost_new
Traceback (most recent call last):
File "C:/Users/PycharmProjects/CurveFitting_Python/CurveFitting_Python_UsingFun.py", line 30, in <module>
params, pcov = curve_fit(ssexp_if, x, yfit, bounds=[lb, ub])
File "C:\Users\PycharmProjects\CurveFitting_Python\venv\lib\site-packages\scipy\optimize\minpack.py", line 765, in curve_fit
raise RuntimeError("Optimal parameters not found: " + res.message)
RuntimeError: Optimal parameters not found: The maximum number of function evaluations is exceeded.
Process finished with exit code 1