2017-04-04 105 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()