python - fit multiple parametric curves with scipy -
python - fit multiple parametric curves with scipy -
i have set (at to the lowest degree 3) of curves (xy-data). each curve parameters e , t constant different. i'm searching coefficients a,n , m best fit on curves.
y= x/e + (a/n+1)*t^(n+1)*x^m
i tried curve_fit, have no thought how parameters e , t function f (see curve_fit documetation). furthermore i'm not sure if understand xdata correctly. doc says: m-length sequence or (k,m)-shaped array functions k predictors. what's predictor? ydata has 1 dimension can't feed multiple curves routine.
so curve_fit might wrong approach don't know magic words search right one. can't first 1 dealing problem.
any help appreciated. j.
one way utilize scipy.optimize.leastsq
instead (curve_fit
convenience wrapper around leastsq
).
stack x
info in 1 dimension; ditto y
data. lengths of 3 individual datasets don't matter; let's phone call them n1
, n2
, n3
, new x
, y
have shape (n1+n2+n3,)
.
inside function optimize, can split info @ convenience. not nicest function, work:
def function(x, e, t, a, n, m): homecoming x/e + (a/n+1)*t^(n+1)*x^m def leastsq_function(params, *args): = params[0] n = params[1] m = params[2] x = args[0] y = args[1] e = args[2] t = args[3] n1, n2 = args[2] yfit = np.empty(x.shape) yfit[:n1] = function(x[:n1], e[0], t[0], a, n, m) yfit[n1:n2] = function(x[n1:n2], e[1], t[1], a, n, m) yfit[n2:] = function(x[n2:], e[2], t[2], a, n, m) homecoming y - yfit params0 = [a0, n0, m0] args = (x, y, (e0, e1, e2), (t0, t1, t2), (n1, n1+n2)) result = scipy.optimize.leastsq(leastsq_function, params0, args=args)
i have not tested this, principle. you're splitting info 3 different calls inside function optimized.
note scipy.optimize.leastsq
requires function returns whatever value you'd minized, in case difference between actual y
info , fitted function data. actual of import variables in leastsq
parameters want fit for, not x
, y
data. latter passed arguments, sizes of 3 separate datasets (i'm not using n3, , i've done juggling n1+n2
convenience; maintain in mind n1
, n2
within leastsq_function
local variables, not original ones).
since awkward function fit (it won't have smooth derivative, example), quite essential
provide starting values (params0
, ...0
values).
don't have info or parameters span orders of magnitude. closer around 1 (a few orders of magnitude ok), better.
python curve-fitting
Comments
Post a Comment