ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 17 Jan 2012 21:48:02 -0600find_fit piecewise defined functionshttps://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/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?
ThanksSat, 14 Jan 2012 00:08:18 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/Comment by Egroj for <p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20532#post-id-20532Ok, thank you.Mon, 16 Jan 2012 09:07:24 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20532#post-id-20532Comment by niles for <p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20538#post-id-20538Sounds like you should open a ticket for this :)Mon, 16 Jan 2012 01:55:38 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20538#post-id-20538Comment by Shashank for <p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20539#post-id-20539Yes. 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)Sat, 14 Jan 2012 11:19:28 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20539#post-id-20539Comment by Shashank for <p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20533#post-id-20533A ticket has been created (http://trac.sagemath.org/sage_trac/ticket/12316). I will try to fix it soon.Mon, 16 Jan 2012 08:48:03 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20533#post-id-20533Answer by pang for <p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?answer=13129#post-id-13129According 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)
Tue, 17 Jan 2012 00:03:18 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?answer=13129#post-id-13129Comment by pang for <p>According to the documentation of find_fit, the "model" can be a plain python function:</p>
<blockquote>
<p>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).</p>
</blockquote>
<p>and nothing stops you from using conditionals inside f:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20510#post-id-20510If it is slower than Piecewise, I'd say that's only because of fast_callable. But you can also compile the function f with cythonTue, 17 Jan 2012 21:48:02 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20510#post-id-20510Comment by Shashank for <p>According to the documentation of find_fit, the "model" can be a plain python function:</p>
<blockquote>
<p>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).</p>
</blockquote>
<p>and nothing stops you from using conditionals inside f:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20512#post-id-20512yes that is true, but i think conditional statement is slower. Tue, 17 Jan 2012 13:49:43 -0600https://ask.sagemath.org/question/8626/find_fit-piecewise-defined-functions/?comment=20512#post-id-20512