简体   繁体   中英

Scipy curve_fit fails on measured data fit

I have coded a program which is performing parameter identification for measured data. The formula is

f = k0*x+c1*(x-x1)^e1+c2*(x-x2)^e2.

(It's presented this way, because I am not yet allowed to put pictures here)

I have to find the correct parameters for the formula and the parameters are k0, x,1, e1, c2, x2, e2. The linear part is easy to find. So I get k0 and x1.My first questions is: Is this code correct for the formula

x = [0.4,0.5,0.513,1.02,1.5,2,2.25,2.75,3,3.3,3.51,3.75,4,4.3,4.5,4.7]
y = [65,115,135,150,170,300,400,600,700,800,1064,1401,1935,2616,3697,4693]

x_np = np.array(x)
y_np = np.array(y)

p0 =(0.1,10)

def advance(x,c2,e2):
    k0 = 166.801522505
    c1 =0.195545880867
    x1 = 0.3
    x2 = 4.7
    print c1
    return k0*x+c1*np.power((x-x1),e1)+c2*np.power((x-x2),e2)


standard_fitting = scipy.optimize.curve_fit(advance, x_np, y_np, p0)

The second is that, my code is failing to do curve_fitting for this curve. If I print variables during the fitting, the Python interpeter prints only nan .

x = [0.4,0.5,0.513,1.02,1.5,2,2.25,2.75,3,3.3,3.51,3.75,4,4.3,4.5,4.7]
y = [65,115,135,150,170,300,400,600,700,800,1064,1401,1935,2616,3697,4693]

x_np = np.array(x)
y_np = np.array(y)

p0 =(0.1,10)

def advance(x,c1,e1):
    k0 = 166.801522505
    x1 = 0.3
    print c1
    return k0*x+c1*np.power((x-x1),e1)#+c2*np.power((x-x2),e2)


standard_fitting = scipy.optimize.curve_fit(advance, x_np, y_np, p0)

This converges, and runs. Your example code still doesn't run. Also I don't recommend fitting to more than two parameters with that data, the two parameter fit looks pretty good, so you probably don't have enough information to meaningfully fit any more parameters.

edit: Ohh I see there used to be a linear part. Maybe then it would be worth more parameters, though I doubt a two power law model will help.

If you want to use more parameters, you need good guesses. So for example if you want k0 to be a parameter, use the mean or median or min of y as the guess. Do the two parameter fit first, then use the output of that as guesses for the input of the next fit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM