# python fitting curve with integral function

## Question:

I would like to fit data with integral function(truncated gamma distribution).
I tried following code, but errors occur. I am appreciate if you would kind help me. Thank you very much in advance.

``````%matplotlib inline
import numpy as np
from scipy import integrate
import scipy.optimize
import matplotlib.pyplot as plt

xlist=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
ylist=[1.0, 0.7028985507246377, 0.4782608695652174, 0.36231884057971014,
0.2536231884057971, 0.1811594202898551, 0.12318840579710147,
0.08695652173913046, 0.057971014492753645, 0.04347826086956524,
0.02173913043478263, 0.007246376811594223]

xdata=np.array(xlist)
ydata=np.array(ylist)

parameter_initial=np.array([0.0,0.0,0.0])#a,b,c

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

parameter_optimal,cov=scipy.optimize.curve_fit(func,xdata,ydata,p0=parameter_initial)
print "paramater =", paramater_optimal
y = func(xdata,paramater_optimal[0],paramater_optimal[1],paramater_optimal[2])
plt.plot(xdata, ydata, 'o')
plt.plot(xdata, y, '-')
plt.show()
``````

Following errors occur.

``````ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
``````

Your code has the following errors:

• The initial values are inadequate since being zeroes, and in the functions divided between that parameter causing problems because the division between 0 is undefined.

• The `quad()` function receives as a second and third parameter a numeric data, not a list, nor a `np.ndarray()` to some iterable, but in your case the parameter x in your function `fun()` is an `np.ndarray()`, What you do is iterate over x and pass that parameter to `quad()`.

• `quad()` returns 2 parameters, the first is the value of the integral and the second is the error, so only the first parameter should be used.

• You must use `**` instead of `^`.

Considering the above, I propose the following code:

``````import numpy as np
from scipy import integrate
import scipy.optimize
import matplotlib.pyplot as plt

xlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
ylist = [1.0, 0.7028985507246377, 0.4782608695652174, 0.36231884057971014,
0.2536231884057971, 0.1811594202898551, 0.12318840579710147,
0.08695652173913046, 0.057971014492753645, 0.04347826086956524,
0.02173913043478263, 0.007246376811594223]

xdata = np.array(xlist)
ydata = np.array(ylist)

parameter_initial = np.array([2.5,2.5,2.5]) # a, b, c

def func(x,a,b,c):
fn = lambda t : t**(a-1)*np.exp(-t)
num = np.asarray([integrate.quad(fn, _x/c, b/c)[0] for _x in x])
return num/den

parameter_optimal, cov = scipy.optimize.curve_fit(func, xdata, ydata,p0=parameter_initial)
print("paramater =", parameter_optimal)
y = func(xdata, *parameter_optimal)
plt.plot(xdata, ydata, 'o')
plt.plot(xdata, y, '-')
plt.show()
``````

Categories: questions
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.