lemmyBarnet
lemmyBarnet

Reputation: 79

Scipy.optimize.curvefit log function

I'm trying to generate equation parameters for this equation:

y = -log((a+bx)/(c+x))

I've got a sample set of data for x, y and a, b and c.

When I do the following:

from scipy.optimize import curve_fit
from scipy import log as log
from scipy import exp as exp
import numpy as np

#Should generate: a = 2.22726000005 , b = 0.1073, c = 2.68937000008

a=1
b=1e-6
c=1

yarr = np.array([0.31776,0.30324,0.28148,0.2651,0.24328,0.22144,0.19799,0.17431,0.14685,0.11521])
xarr = np.array([0.250,0.225,0.200,0.175,0.150,0.125,0.100,0.075,0.050,0.025])

def func(x, a, b, c):

    return (log(c+x)-log(a+(b*x)))

popt, pcov = curve_fit(func,  xarr, yarr, (a,b,c))

print "a = %s , b = %s, c = %s" % (popt[0], popt[1], popt[2])

This should give me:

a = 2.22726000005 , b = 0.1073, c = 2.68937000008

but what I get is:

a = 0.37366276487 , b = 0.415297976794, c = 0.406353416622

which gives a nice curve, but it's nowhere near the correct values.

I've read several similar issues here, but none of the solutions have worked for me.

Any tips?

Thanks Lemmy

Upvotes: 1

Views: 1866

Answers (1)

xnx
xnx

Reputation: 25518

I can't reproduce the supplied data nearly so well with the values you supply as with the values curve_fit produces, so perhaps you need to supply more information about the problem:

In [48]: pylab.plot(xarr, yarr, label='data')
In [49]: pylab.plot(xarr,func(xarr, *popt), label='curve_fit')
In [50]: ap, bp, cp = 2.22726000005, 0.1073, 2.68937000008
In [51]: pylab.plot(xarr,func(xarr, ap,bp,cp), label='supplied a,b,c')
In [52]: pylab.legend()
In [53]: pylab.show()

enter image description here

Upvotes: 2

Related Questions