2017-04-04 37 views
2

我坚持要满足一个双极S形曲线 - 我想有以下曲线:飞度双极乙状结肠蟒蛇

enter image description here

,但我需要它转移和延伸。我有以下输入:

x[0] = 8, x[48] = 2 

所以超过48周期我需要使用双极S形函数来近似平滑度很高送货从8下降到2。任何想法如何我可以推导出适合这些参数的曲线?

这里是我到目前为止,但我需要改变双曲线函数:

import math 

def sigmoid(x): 
    return 1/(1 + math.exp(-x)) 


plt.plot([sigmoid(float(z)) for z in range(1,48)]) 
+0

'atan'和'atanh'是流行的sigmoid函数 – f5r5e5d

回答

1

你可以重新定义双曲线函数像这样

def sigmoid(x, a, b, c, d): 
    """ General sigmoid function 
    a adjusts amplitude 
    b adjusts y offset 
    c adjusts x offset 
    d adjusts slope """ 
    y = ((a-b)/(1 + np.exp(x-(c/2))**d)) + b 
    return y 

x = np.arange(49) 
y = sigmoid(x, 8, 2, 48, 0.3) 

plt.plot(x, y) 

塞韦林的答案很可能较为强劲,但是这应该是罚款,如果你想要的是一个快速而肮脏的解决方

In [2]: y[0] 
Out[2]: 7.9955238269969806 

In [3]: y[48] 
Out[3]: 2.0044761730030203 

enter image description here

1

另外,您也可以使用curve_fit这可能会派上用场,如果你有比只有两个数据点多。输出看起来是这样的:

enter image description here

正如你可以看到,图中包含了所需的数据点。我使用了@ lanery的功能来配合;你当然可以选择你喜欢的任何功能。这是一些内嵌评论的代码:

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit 

def sigmoid(x, a, b, c, d): 
    return ((a - b)/(1. + np.exp(x - (c/2)) ** d)) + b 

# one needs at least as many data points as parameters, so I just duplicate the data 
xdata = [0., 48.] * 2 
ydata = [8., 2.] * 2 

# plot data 
plt.plot(xdata, ydata, 'bo', label='data') 

# fit the data 
popt, pcov = curve_fit(sigmoid, xdata, ydata, p0=[1., 1., 50., 0.5]) 

# plot the result 
xdata_new = np.linspace(0, 50, 100) 
plt.plot(xdata_new, sigmoid(xdata_new, *popt), 'r-', label='fit') 
plt.legend(loc='best') 
plt.show()