Ask Your Question

Revision history [back]

According to the documentation of find_fit, the "model" can be a plain python function:

model – Either a symbolic expression, symbolic function, or a Python function. model has to be a function of the variables (x1;x2;:::;xk) and free parameters (a1;a2;:::;al).

and nothing stops you from using conditionals inside f:

def f(t,a) :
    if t<1/2:
        return t*a
    return (1-t)*a

data = [(i,i*(1-i)) for i in xsrange(0, 1, 0.1)]

var('a, x')
d  = find_fit(data,f, parameters=[a], variables=[x], solution_dict=True)
a0 = d[a]
print a0
point(data) + plot(lambda t:f(t,a0), 0,1)