Home > Back-end >  Getting error TypeError: unsupported operand type(s) for 'list' and 'Float' when
Getting error TypeError: unsupported operand type(s) for 'list' and 'Float' when

Time:09-27

I am a newbie and please help me. Thanks in advance. I am trying to find full width at half maxima of a derivative curve and I am getting errors.

def cauchy(x, l, k, x1):
    return l / (1 np.exp(-k*(x-x1)))

def fwhm(x, y, k=10):
    half_max = amax(y)/2
    s = splrep(x, y - half_max, k=k)
    roots = sproot(s)
    return abs(roots[1] - roots[0])

distance = [1000*0.001, 2000*0.001, 3000*0.001, 4000*0.001,5000*0.001,6000*0.001,7000*0.001,8000*0.001,
           9000*0.001, 11000*0.001, 12000*0.001, 13000*0.001, 14000*0.001, 15000*0.001, 16000*0.001,
           17000*0.001, 18000*0.001, 19000*0.001, 21000*0.001, 22000*0.001, 23000*0.001, 24000*0.001, 25000*0.001, 26000*0.001,
           27000*0.001, 28000*0.001, 29000*0.001, 30000*0.001, 31000*0.001, 32000*0.001, 33000*0.001,
           34000*0.001, 35000*0.001]
amplitude= [26, 31, 29, 26, 27, 24, 24, 28, 24, 24, 28, 31, 24, 26, 55, 30, 73, 101, 168, 219, 448, 833, 1280, 1397, 1181, 1311,
            1715, 1975, 2003, 2034, 2178, 2180, 2182]
plt.plot(distance,amplitude, 'o')
popt, pcov = curve_fit(cauchy, distance, amplitude,maxfev=100, bounds=((-10, -10, -10), (3000, 3000, 3000)),p0=[2500, 1, 30])
plt.plot(distance, cauchy(distance, *popt), 'r', label='cauchy fit')
l,k,x,x1 = sp.symbols('l k x x1')
expr_diff1 = Derivative(l / (1 sp.exp(-k*(x-x1))), x)
print("Derivative of the function : {}".format(expr_diff1.doit()))
var1=[]
for i in distance:
    xnew = ((4.06252582e-01*2.22741362e 03*exp(-4.06252582e-01*(i - 2.57502110e 01))/(1   exp(-4.06252582e-01*(i - 2.57502110e 01)))**2))*10
    var1.append(xnew)
print(var1)
plt.plot(distance, var1)
fwhm(distance, var1)

CodePudding user response:

In the cauchy() method you expect an attribute x which supports the subtraction operation, but you pass the list of floats (which does not support that).

I suppose you wanted to use numpy arrays instead of regular lists.

distance = numpy.array([.... your array data...])
ampliture = numpy.array([... your array data...])

Also further in the call of fwhm you should pass numpy array:

fwhm(distance, np.array(var1))
  • Related