python - curve_fit: calculations with x before fit -


i'm using curve_fit function of scipy package fit experimental data. wrote objective function that:

def binding21(g0, eps1, eps2, k1, k2):      = k1 * k2     b = k1 * (2 * k2 * h0 - k2 * g0 + 1)     c = k1 * (h0 - g0) + 1     d = g0      roots = np.roots([a, b, c, -d])     roots = [value value in roots if np.isreal(value)]     g = max(roots)      y = (eps1 * h0 * k1 * g + eps2 * h0 * k1 * k2 * g**2) / \         (1 + k1 * g + k1 * k2 * g**2)      return y 

g0 array of x values. however, model use in objective function not defined y=f(g0), instead y=f(g=f(g0)).

i need find roots of g=f(g0) before can calculate y: have choose smallest positive value of roots returned, , assign g.

for now, previous code returns:

traceback (most recent call last):   file "/home/djipey/working/data_boss/21.py", line 80, in <module>     result = mod.fit(y, g0=x)   file "/usr/lib/python3.5/site-packages/lmfit/model.py", line 506, in fit     output.fit(data=data, weights=weights)   file "/usr/lib/python3.5/site-packages/lmfit/model.py", line 710, in fit     self.init_fit    = self.model.eval(params=self.params, **self.userkws)   file "/usr/lib/python3.5/site-packages/lmfit/model.py", line 372, in eval     result = self.func(**self.make_funcargs(params, kwargs))   file "/home/djipey/working/data_boss/21.py", line 38, in binding21     roots = np.roots([a, b, c, -d])   file "/usr/lib/python3.5/site-packages/numpy/lib/polynomial.py", line 207, in roots     p = atleast_1d(p)   file "/usr/lib/python3.5/site-packages/numpy/core/shape_base.py", line 50, in atleast_1d     ary = asanyarray(ary)   file "/usr/lib/python3.5/site-packages/numpy/core/numeric.py", line 525, in asanyarray     return array(a, dtype, copy=false, order=order, subok=true) valueerror: setting array element sequence. 

because i'm trying find roots using g0, not single value array.

could me solve problem ?

edit:

i'm trying fit equation: equation http://imagizer.imageshack.com/img911/2966/9lthns.png

with:

explanation http://imagizer.imageshack.com/img911/9868/zyfp1b.png

epsilon_hg, epsilon_hg2, k1 , k2 parameters. h0 known constant. g variable in model. , g depends on g0, "x" values give model.

i have several experimental data points y=f(g0). need fit y=f(g). g can obtained solving cubic equation above.

i found solution, in fact simple:

g = []   def binding21(g0, eps1, eps2, k1, k2):      global g      g = []      value in g0:         = k1 * k2         b = k1 * (2 * k2 * h0 - k2 * value + 1)         c = k1 * (h0 - value) + 1         d = value          roots = np.roots([a, b, c, -d])          roots = [float(value) value in roots if np.isreal(value)]          g.append(max(roots))      g = np.array(g)      y = (eps1 * h0 * k1 * g + eps2 * h0 * k1 * k2 * g**2) / \         (1 + k1 * g + k1 * k2 * g**2)      return y 

i re-calculate g @ each iteration of curve_fit. have whole list, can't use 1 particular value. so, entire list :)

and need final value of g, had use global variable. know it's evil, couldn't find better solution in case.

for chemists out there, snippet used fit titrations binding experiments (model 1:2).


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -