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.Fri, 09 Jun 2017 03:22:51 -0500ode and piecewise functionhttp://ask.sagemath.org/question/37847/ode-and-piecewise-function/ Hi all,
I am trying to solve, using Sage, an ode which includes a piecewise. For that I wrote the following piece of code which raises an error :
f = piecewise([(RealSet.unbounded_below_open(0),0), (RealSet.unbounded_above_closed(0),10)])
u = function('u')(x)
eqn = 2*diff(u,x) + u == f(x)
u = desolve(eqn, u, ivar=x)
show(expand(u))
Since the error vanishses when I replace the function `f` by either of the functions `exp` or `log`, I guess the problem is coming from the piecewise function. Could anyone help me solve this issue and explain what is wrong here?
ThanksThu, 08 Jun 2017 09:38:47 -0500http://ask.sagemath.org/question/37847/ode-and-piecewise-function/Comment by kcrisman for <p>Hi all,</p>
<p>I am trying to solve, using Sage, an ode which includes a piecewise. For that I wrote the following piece of code which raises an error : </p>
<pre><code>f = piecewise([(RealSet.unbounded_below_open(0),0), (RealSet.unbounded_above_closed(0),10)])
u = function('u')(x)
eqn = 2*diff(u,x) + u == f(x)
u = desolve(eqn, u, ivar=x)
show(expand(u))
</code></pre>
<p>Since the error vanishses when I replace the function <code>f</code> by either of the functions <code>exp</code> or <code>log</code>, I guess the problem is coming from the piecewise function. Could anyone help me solve this issue and explain what is wrong here?</p>
<p>Thanks</p>
http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37858#post-id-37858I don't know; I'm just suggesting that could provide a solution. Of course, for numerical solutions there are good options. Also, I believe Sage does support Dirac and Heaviside functions now so they may work fine with this and `desolve`.Thu, 08 Jun 2017 12:06:46 -0500http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37858#post-id-37858Comment by soking for <p>Hi all,</p>
<p>I am trying to solve, using Sage, an ode which includes a piecewise. For that I wrote the following piece of code which raises an error : </p>
<pre><code>f = piecewise([(RealSet.unbounded_below_open(0),0), (RealSet.unbounded_above_closed(0),10)])
u = function('u')(x)
eqn = 2*diff(u,x) + u == f(x)
u = desolve(eqn, u, ivar=x)
show(expand(u))
</code></pre>
<p>Since the error vanishses when I replace the function <code>f</code> by either of the functions <code>exp</code> or <code>log</code>, I guess the problem is coming from the piecewise function. Could anyone help me solve this issue and explain what is wrong here?</p>
<p>Thanks</p>
http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37849#post-id-37849Does it mean that I have to switch to Maxima in order to solve such ode?Thu, 08 Jun 2017 09:48:43 -0500http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37849#post-id-37849Comment by kcrisman for <p>Hi all,</p>
<p>I am trying to solve, using Sage, an ode which includes a piecewise. For that I wrote the following piece of code which raises an error : </p>
<pre><code>f = piecewise([(RealSet.unbounded_below_open(0),0), (RealSet.unbounded_above_closed(0),10)])
u = function('u')(x)
eqn = 2*diff(u,x) + u == f(x)
u = desolve(eqn, u, ivar=x)
show(expand(u))
</code></pre>
<p>Since the error vanishses when I replace the function <code>f</code> by either of the functions <code>exp</code> or <code>log</code>, I guess the problem is coming from the piecewise function. Could anyone help me solve this issue and explain what is wrong here?</p>
<p>Thanks</p>
http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37848#post-id-37848Hmm, I'm not sure whether our `piecewise` functions are supported with Maxima's ode solvers. Note that Maxima has its own piecewise implementation which may allow this directly within Maxima.Thu, 08 Jun 2017 09:45:30 -0500http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37848#post-id-37848Answer by eric_g for <p>Hi all,</p>
<p>I am trying to solve, using Sage, an ode which includes a piecewise. For that I wrote the following piece of code which raises an error : </p>
<pre><code>f = piecewise([(RealSet.unbounded_below_open(0),0), (RealSet.unbounded_above_closed(0),10)])
u = function('u')(x)
eqn = 2*diff(u,x) + u == f(x)
u = desolve(eqn, u, ivar=x)
show(expand(u))
</code></pre>
<p>Since the error vanishses when I replace the function <code>f</code> by either of the functions <code>exp</code> or <code>log</code>, I guess the problem is coming from the piecewise function. Could anyone help me solve this issue and explain what is wrong here?</p>
<p>Thanks</p>
http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?answer=37871#post-id-37871For you concrete example, you can use `unit_step` instead of `piecewise`:
sage: f(x) = 10*unit_step(x)
sage: u = function('u')(x)
sage: eqn = 2*diff(u,x) + u == f(x)
sage: u = desolve(eqn, u, ivar=x)
sage: u
(5*(e^(1/2*x) - 1)*sgn(x) + _C + 5*e^(1/2*x) - 5)*e^(-1/2*x)
Thu, 08 Jun 2017 14:09:18 -0500http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?answer=37871#post-id-37871Comment by soking for <p>For you concrete example, you can use <code>unit_step</code> instead of <code>piecewise</code>:</p>
<pre><code>sage: f(x) = 10*unit_step(x)
sage: u = function('u')(x)
sage: eqn = 2*diff(u,x) + u == f(x)
sage: u = desolve(eqn, u, ivar=x)
sage: u
(5*(e^(1/2*x) - 1)*sgn(x) + _C + 5*e^(1/2*x) - 5)*e^(-1/2*x)
</code></pre>
http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37886#post-id-37886Thank you, for your answer, but could I have a more general solution because I can't apply your answer when for instance
f = piecewise([((-1,0),0), ((0,1),x), ((1,2),2-x), (RealSet.unbounded_above_closed(2),0)])
I thought of telling sage to solve the ode on each interval, but I don't see how to do it with Sage.Fri, 09 Jun 2017 03:22:51 -0500http://ask.sagemath.org/question/37847/ode-and-piecewise-function/?comment=37886#post-id-37886