I am trying to fit some data to an equation in python and I am having some difficulty. I have the equation:
y(t)=yo+a(t-ti)^b+kt
where a
, ti
, b
and k
are fitting parameters, and t
and disp
are array varaibles representing time and displacement respectively. The equation will fit in gnuplot fine with some iteration, but fitting it in python throws up an error of:-
ValueError: array must not contain infs or NaNs
The complete stack trace is:
creep_test.py:246: RuntimeWarning: invalid value encountered in power
fitfunc = lambda p, t: disp_list[0]+(p[0]*(t-p[1])**p[2])+p[3]*t # Target function
Traceback (most recent call last):
File "creep_test.py", line 374, in <module>
main()
File "creep_test.py", line 368, in main
python_fit(filename)
File "creep_test.py", line 256, in python_fit
out = optimize.leastsq(errfunc, p0[:], args=(t, disp,err), full_output=1)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 338, in leastsq
cov_x = inv(dot(transpose(R),R))
File "/usr/lib/python2.7/dist-packages/scipy/linalg/basic.py", line 285, in inv
a1 = asarray_chkfinite(a)
File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 590, in asarray_chkfinite
"array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs
I have found with playing around that its the term ti
that is causing the issues in that the fitting works if you have ti
fixed at around 35.5
. I have used a spreadsheet and for any values of t
under the ti
, the equation throws up an #VALUE
(probably because its imaginary)
Basically is there a way to get python to fit the curve like gnuplot (which I assume ignores the non valid results)? I the code I have used for the fittiong part of my programme is below:
fitfunc = lambda p, t: disp_list[0]+(p[0]*(t-p[1])**p[2])+p[3]*t # Target function
errfunc = lambda p, t, y, err: (fitfunc(p, t) - y)/(err) # Distance to the target function
err=0.01
p0 = [ 50, 35.5,0.005, 0.001] # Initial guess for the parameters
out = optimize.leastsq(errfunc, p0[:], args=(t, disp,err), full_output=1)
print out[0]
print out[1]
Thankyou!!