ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 26 Mar 2014 17:34:01 -0500desolve_rk4 cannot handle a piecewise functionhttp://ask.sagemath.org/question/11062/desolve_rk4-cannot-handle-a-piecewise-function/I am trying to get a numeric solution to a differential equation involving a function that is piecewise (actually defined using an if/else statement). I successfully generated a slopefield using the lambda function, but desolve_rk4 seemingly cannot cope with this syntax. It seems to me that a numeric solver should be able to handle a function defined this way, but perhaps I am just doing something wrong?
Code:
var('t')
def f(t):
if t<5:
return 3
else:
return 0
V=function('V',t)
desolve_rk4(lambda t,V: ((f)-V)/(2.3*1.2),V, ics=(0,0),ivar=t)Thu, 20 Feb 2014 11:26:00 -0600http://ask.sagemath.org/question/11062/desolve_rk4-cannot-handle-a-piecewise-function/Comment by kcrisman for <p>I am trying to get a numeric solution to a differential equation involving a function that is piecewise (actually defined using an if/else statement). I successfully generated a slopefield using the lambda function, but desolve_rk4 seemingly cannot cope with this syntax. It seems to me that a numeric solver should be able to handle a function defined this way, but perhaps I am just doing something wrong?</p>
<p>Code:</p>
<pre><code>var('t')
def f(t):
if t<5:
return 3
else:
return 0
V=function('V',t)
desolve_rk4(lambda t,V: ((f)-V)/(2.3*1.2),V, ics=(0,0),ivar=t)
</code></pre>
http://ask.sagemath.org/question/11062/desolve_rk4-cannot-handle-a-piecewise-function/?comment=16222#post-id-16222Short answer is that Maxima doesn't know what to do with such a Python lambda function, and this uses a Maxima functionality.Thu, 20 Feb 2014 13:31:21 -0600http://ask.sagemath.org/question/11062/desolve_rk4-cannot-handle-a-piecewise-function/?comment=16222#post-id-16222Answer by calc314 for <p>I am trying to get a numeric solution to a differential equation involving a function that is piecewise (actually defined using an if/else statement). I successfully generated a slopefield using the lambda function, but desolve_rk4 seemingly cannot cope with this syntax. It seems to me that a numeric solver should be able to handle a function defined this way, but perhaps I am just doing something wrong?</p>
<p>Code:</p>
<pre><code>var('t')
def f(t):
if t<5:
return 3
else:
return 0
V=function('V',t)
desolve_rk4(lambda t,V: ((f)-V)/(2.3*1.2),V, ics=(0,0),ivar=t)
</code></pre>
http://ask.sagemath.org/question/11062/desolve_rk4-cannot-handle-a-piecewise-function/?answer=16104#post-id-16104You can do this using `desolve_odeint`. I'm not sure why it's not working in `desolve_rk4`. Try this:
var('t s V')
def f(t):
if t<5:
return 3
else:
return 0
def rhs(V,s,t):
return ((f(t)-V)/(2.3*1.2))
F=[SR(1),rhs]
tlist=srange(0,10,0.1)
ans=desolve_odeint(F,[0,10],tlist,[s,V],ivar=t)
line(ans)Wed, 26 Mar 2014 17:34:01 -0500http://ask.sagemath.org/question/11062/desolve_rk4-cannot-handle-a-piecewise-function/?answer=16104#post-id-16104