0

I would like to fit a trapezoid function to my data. I define the function as trap and tried to use the scipy.cur_fit function. But I get the error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). For the line of " if (np.less(x, start)): return upper".

Does somebody know how I can lift this problem or if there is somewhere a predefined trapezoid function I could feed to curve_fit?

Thank you for your help!

def trap(x, upper, start, end, slope, width):
    x = np.array(x)
    if (np.less(x, start)): return upper
    if (np.greater_equal(x,start)) & (np.less(x,end)): return upper - slope*(x-start)
    if (np.greater_equal(x,end)) & (np.less(x,end+width)): return upper - slope*(end-start)
    if (np.greater_equal(x,end+width)) & (np.less_equal(x,2*end - start)): return upper - slope*(end-start) + slope*(x- (end + width))
    else: return upper

def main():
    dir = "Daten/2022-11-28_WASP-12b.csv"
    df = import_transit(dir)
    plt.errorbar(x=df['time'], y=df['str'], yerr= df['err'], fmt='x')
    x = np.linspace(0.5131, 0.6842, 1000 )
    ### Trapez
    p0 = [1.311, 0.5298, 0.5568, 1.04, 0.07]
    par, cov = curve_fit(trap, df['time'], df['str'], sigma=df['err'], p0=p0)
    upper, start, end, slope, width = par
    y = trap(df['str'], upper, start, end, slope, width)
    np.plot(x,y)

    plt.show()

I tried defining my own trapezoid function but curve_fit is having problems with the piece-wise definition of the model function.

daniel
  • 11
  • 2
  • This can be solved by methods presented [here](https://de.scribd.com/document/380941024/Regression-par-morceaux-Piecewise-Regression-pdf) – mikuszefski Dec 07 '22 at 08:01
  • Alternatively one can play with [this](https://stackoverflow.com/a/70713496/803359) idea – mikuszefski Dec 07 '22 at 08:05

0 Answers0