'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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marystellah |
