简体   繁体   中英

Combination (piecewise) function of two pre-defined functions

I'm currently making a custom function that I will eventually feed into scipy.optimize.curve_fit() for a custom curve fit. My curve fit will be shaped like a bump. Gaussian rise and exponential fall, pieced together at the highest point of the gaussian. I have defined a Gaussian and an exponential function and currently trying to define a combo() function. Here's what I have so far:

    def exp(x, a, b, c):
          return a * np.exp((-b * x) + c)
    def gauss(x,d,e,f):
          return d * np.exp(-((x-e)**2)/(2*(f**2)))
    def combo(x,a,b,c,d,e,f):
          ex = exp(x,a,b,c)
          ga = gauss(x,d,e,f)
    num = np.arange(0,1000,1)
    test =combo(num,1,2,3,10,4,3)

I've tried to use if statements in my combo function (if x<d: return ga) but I get the error message: "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". Maybe this is the solution but I'm not sure how to employ it.

def combo(x,a,b,c,d,e,f, dtype=np.float64):
    def gauss(x,d,e,f):
        return d * np.exp(-((x-e)**2)/(2*(f**2)))
    def exp(x, a, b, c):
        return a * np.exp((-b * x) + c)
    result = np.piecewise(
        x,
        [x <= e,x > e],
        [lambda x: gauss(x,d,e,f), lambda x: exp(x,a,b,c)],
    )
    return result

I think the best way to do this using numpy is to use array slicing. First, create the test array as a Gaussian, then find the index where it reaches its max value, and then replace the array from that point on with the value calculated using the exponential function:

def exp(x, a, b, c):
      return a * np.exp(-c * (x-b))
def gauss(x, a, b, d):
      return a * np.exp(-((x-b)**2)/(2*(d**2)))
def combo(x, a, b, c, d):
    y = gauss(x, a, b, d)
    g_max_ind = y.argmax()
    y[g_max_ind+1:] = exp(x[g_max_ind+1:], a, b, c)
    return y
num = np.arange(-50, 50, 0.5)
test = combo(num, 10, 4, 3, 3)

I assume that you want this function to be continuous, so I changed your parameters so that the values input into exp and gauss are consistent with each other, and I changed the arange parameters so the plot is more meaningful. Please let me know if I misunderstood and I can correct.

Output:

在此处输入图像描述

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