jcchin / boring

TTT Battery Pack Research
1 stars 8 forks source link

cp_func.py needs a smoothing function #51

Closed jcchin closed 3 years ago

jcchin commented 3 years ago

currently just using an if statement, needs to be differentiable

jcchin commented 3 years ago
def cp_sigmoid(x, start=0, stop=1, low=0, high=1, a = 10.0):
    y = (high-low)*(0.25 - 0.25 * np.tanh(a*(-stop + x)))*(np.tanh(a*(-start + x)) + 1)+low
    return y

def cp_sigmoid_deriv(x, start=0, stop=1, low=0, high=1, a = 10.0):
    dy = (high-low)*0.25*a*((np.tanh(a*(start - x)) - 1)/np.cosh(a*(stop - x))**2 + (np.tanh(a*(stop - x)) + 1)/np.cosh(a*(start - x))**2)
    return dy

if __name__ == '__main__':

    T1 = 0 # start of filter impulse
    T2 = 3 # stop of filter impulse
    sigma = 3 # smoothness constant (helps keep derivatives tractable)
    high = 5
    low = 2
    # make example
    x = np.linspace(T1 - 1, T2 + 1, 100)
    y = cp_sigmoid(x, T1, T2, low, high, sigma)
    dy = cp_sigmoid_deriv(x, T1, T2, low, high, sigma)
    plt.figure()
    plt.plot(x,y, label='y')
    plt.plot(x,dy, label='dy')
    plt.legend()

    plt.show()