MrPibbXIX
MrPibbXIX

Reputation: 15

Using scipy.optimize's curve_fit

----ANSWERED----

I apologize if this question is too simply answered. I'm very new to Python coding and am currently taking on a difficult project. Any help is appreciated; I'd be happy to clarify confusion for those willing to assist.

I was able to successfully fit my data with this function, but the binding model was inaccurate (thermodynamic values resulting were incorrect):

#defining function of the binding model 
def binding_model(molar_ratio,Ka,n,dH):
    return (dH*molar_ratio**n)/(molar_ratio**n+Ka**n)

#fitting molar enthalpy vs. molar ratio data with binding model
initial_paramaters=array([1,0,0])   
parameters,cov=curve_fit(binding_model,molar_ratio,peak_areas,initial_paramaters)
n=parameters[0]
Ka=parameters[1]
dH=parameters[2]
Ffit=binding_model(molar_ratio,n,Ka,dH)

I corrected the binding model (to a single-site ligand binding model), but I cannot get the curve_fit function to work anymore:

#defining function of the binding model 
def binding_model(Mt,Xt,Vcell,Ka,n,dH):
    return (n*Mt*dH*Vcell/2)*(1+(Xt/(Mt*n))+(1/(n*Ka*Mt)))-((1+(Xt/(n*Mt))+(1/(n*Ka*Mt)))**2-(4*Xt/(Mt*n))**.5)
#fitting molar enthalpy vs. molar ratio data with binding model
initial_paramaters=array([1,0,0])   
parameters=curve_fit(binding_model,Mt,Xt,Vcell,peak_areas,initial_paramaters)
n=parameters[0]
Ka=parameters[1]
dH=parameters[2]
Ffit=binding_model(Mt,Xt,Ka,n,dH,Vcell)

It now returns a "curve_fit() takes at most 5 arguments (6 given)" error.

molar_ratio, Mt, and Xt are three arrays, each consist of 41 values. Vcell is an integer extracted from the data. Thank you for taking a look!

Upvotes: 1

Views: 2026

Answers (1)

will
will

Reputation: 10650

You're just not using it properly;

read how it works here

say i wanted to fit a polynomial to some data;

i could have something like this:

def quad(xdata, a,b,c):
    return a * xdata**2 + b * xdata + c

then i could have some data;

xdata = np.array([range(10)])
ydata = np.array([1 for _ in xdata])

and then i could call curve_fit;

initial_guess = [0,0,0]
popt, pcov = scipy.optimize.curve_fit(quad, xdata, ydata, p0=initial_guess)

print popt # [0,0,1]

The issue you were having is that you weren't putting you initial guess into an array for the p0 argument (which is optional anyway).

For your function, try this (i have not tested it, i don't have python on this machine):

def binding_model(xdata,Vcell,Ka,n,dH):
  Mt = xdata[0]
  Xt = xdata[1]
  return (n*Mt*dH*Vcell/2)*(1+(Xt/(Mt*n))+(1/(n*Ka*Mt)))-((1+(Xt/(n*Mt))+(1/(n*Ka*Mt)))**2-(4*Xt/(Mt*n))**.5)

#fitting molar enthalpy vs. molar ratio data with binding model

initial_Vcell = 0
initial_Ka = 0
initial_n = 0
initial_dH = 0

initial_paramaters=array([initial_Vcell, initial_Ka, initial_n, initial_dH])   
xdata = zip(Mt, Xt) 
parameters, cov = curve_fit(binding_model, xdata, ydata, p0=initial_paramaters)


Vcell=parameters[0]
Ka=parameters[1]
n=parameters[2]
dH=parameters[3]

Ffit=binding_model(xdata, Vcell, Ka, n, dH)

Upvotes: 2

Related Questions