擬合指數(shù)衰減

我正在嘗試解決以下線性化方程:

ln?{1−y/y}=ln?(c)−b(x)

使用python scipy curvefit或其他類似的方法,請(qǐng)你告訴我怎么做?

樣本數(shù)據(jù):

x = [15, 16, 17, 18, 19, 20]

y = [0.78, 0.67, 0.56, 0.41, 0.31, 0.20]

我到目前為止嘗試的代碼:

import numpy as np
import scipy as sp
import pylab
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import math
import warnings   

def sigmoid(x,c,b):
    y = np.log(c)-b*x
    return y

def sigmoid_solve(y, c, b):
    x = (np.log(c)+np.log((1-y)/y))/b
    return x

y_new = []

x_data = [15, 16, 17, 18, 19, 20]
y_data = [0.78, 0.67, 0.56, 0.41, 0.31, 0.20]


for data in y_data:
    y_new.append(np.log((1-data)/data))


popt, pcov = curve_fit(sigmoid, x_data, y_new)
ce50 = sigmoid_solve(0.5,popt[0],popt[1])

x = np.linspace(10,40,10)
y = 1/(1+np.exp(sigmoid(x, *popt)))
plt.plot(x, y, 'r',label='logistic exp curve fit')
plt.plot(x_data, y_data,'o',label='data plot')
plt.ylim(0, 1)
plt.xlim(10, 50)
plt.legend(loc='best')
plt.savefig('test.png')
plt.close("all")