# 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?

Thanks

edit retag close merge delete

Hmm, 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.

Does it mean that I have to switch to Maxima in order to solve such ode?

I 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.

Sort by » oldest newest most voted

For 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)

more

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.