Ask Your Question
2

find_fit piecewise defined functions

asked 2012-01-14 07:08:18 +0100

Egroj gravatar image

Hi all! I'm working with the function find_fit and now I need to fit a piecewise function, but I have tried it returns errors. How could I fit a picewise function?

Thanks

edit retag flag offensive close merge delete

Comments

2

Yes. It turns out that piecewise does not have a member function variables. So len(variables) retruns a error. Is there a ticket about this? I could not find any. If the function happens to be piecewise we should call length and not len(variables)

Shashank gravatar imageShashank ( 2012-01-14 18:19:28 +0100 )edit

Sounds like you should open a ticket for this :)

niles gravatar imageniles ( 2012-01-16 08:55:38 +0100 )edit

A ticket has been created (http://trac.sagemath.org/sage_trac/ticket/12316). I will try to fix it soon.

Shashank gravatar imageShashank ( 2012-01-16 15:48:03 +0100 )edit

Ok, thank you.

Egroj gravatar imageEgroj ( 2012-01-16 16:07:24 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2012-01-17 07:03:18 +0100

pang gravatar image

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)
edit flag offensive delete link more

Comments

yes that is true, but i think conditional statement is slower.

Shashank gravatar imageShashank ( 2012-01-17 20:49:43 +0100 )edit

If it is slower than Piecewise, I'd say that's only because of fast_callable. But you can also compile the function f with cython

pang gravatar imagepang ( 2012-01-18 04:48:02 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-01-14 07:08:18 +0100

Seen: 896 times

Last updated: Jan 17 '12