# Piecewise differential equation

Say I have a very trivial function

$$V\left(r\right)=\begin{cases} -V_{0} & 0\leq r\leq R\\ 0 & r>R \end{cases}$$ with $V_{0}>0$ and $R>0$. I would like to let Mathematica solve the differential equation

$$u''\left(r\right)+k^{2}u\left(r\right)=V\left(r\right)u\left(r\right)$$

in the $r>0$ region with boundary conditions $u(0)=0$ and such that the solution and it's derivative match at $r=R$. Is there a simple way for this? A way generalizable to other piecewise functions? Using $k=1$ and $V_{0}=1$ I have attempted

DSolve[{u''[r] + u[r] (1 + UnitStep[1 - r]) == 0, u == 0}, u[r], {r, 0, Infinity}]


I get no result, Mathematica just shows back my input.

• Use Piecewise to represent V[r]. Understand, though, that DSolve cannot solve all ODEs. Mar 3, 2016 at 1:12
• Using Piecewise I get exactly the same problem. Since it's a very trivial equation, which has a simple analytic (piecewise) solution, I am hoping to get it with DSolve. Mar 3, 2016 at 1:25
• Try using just the variable instead of a range in the third argument: DSolve[{u''[r] + u[r] (1 + UnitStep[1 - r]) == 0, u == 0}, u[r], r] Mar 3, 2016 at 1:27
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Mar 3, 2016 at 1:29
• what about NDSolve? I bet you are not really looking into solving a step potential but something more complex that most probably will not be analytically solvable. Mar 3, 2016 at 2:03

This particular ODE can be integrated by the somewhat cumbersome means,

s1 = Simplify@ExpToTrig@DSolve[{u1''[r] + k^2 u1[r] + v0 u1[r] == 0, u1 == 0}, u1[r],
r, Assumptions -> k^2 + v0 > 0][[1, 1]] /. C -> -I c/2
s2 = First@FullSimplify@First@DSolve[{u2''[r] + k^2 u2[r] == 0,
u2[r0] == u1[r] /. s1 /. r -> r0, u2'[r0] == D[u1[r] /. s1, r] /. r -> r0},
u2[r], r]
s = Piecewise[{{u1[r] /. s1, 0 < r < r0}}, u2[r] /. s2]
(* Piecewise[{{c*Sin[r*Sqrt[k^2 + v0]], 0 < r < r0}},
(c*Sqrt[k^2 + v0]*Cos[r0*Sqrt[k^2 + v0]]*Sin[k*(r - r0)])/k +
c*Cos[k*(r - r0)]*Sin[r0*Sqrt[k^2 + v0]]] *)


In general, if DSolve can integrate each region of the ODE, then the parts can be matched together as shown here. The more fundamental question is whether DSolve can integrate ODEs with more complicated expressions for V. In general, DSolve can solve only those ODEs that have known solutions. Otherwise, NDSolve must be used, and it can handle discontinuous expressions for V.

• What you wrote was the solution to $u''\left(r\right)+k^{2}u\left(r\right)=V\left(r\right)$ but I need $u''\left(r\right)+k^{2}u\left(r\right)=V\left(r\right)u\left(r\right)$ ... Mar 3, 2016 at 1:40
• @AntonioCox Here is the corrected answer. Mar 3, 2016 at 2:24
• Ok, so in general I can extract each region of a piecewise function, solve for the region (assuming the integration is possible), impose the continuity conditions as you did, and then stick each piece back together. I can attempt to write a code for that. At the end, from a general solvable input piecewise function I will get a nice piecewise output, with continuity conditions automatically imposed. Thanks! Mar 3, 2016 at 3:10
• @AntonioCox You are correct. It is unfortunate that DSolve cannot handle such cases without assistance. This is not the first time that I have seen DSolve struggle with seemingly straightforward ODEs. Mar 3, 2016 at 3:26

Well, here's an indirect way, using the value Zeta as a proxy for a symbolic $R$, which can be replaced by R after DSolve returns. I also put in an explicit (symbolic) initial value up for u'.

sol = DSolve[{u''[r] + k^2 u[r] == Piecewise[{{-v0, 0 <= r <= Zeta}}] u[r],
u == 0, u' == up}, u, r] /. Zeta -> R
(*  somewhat long solution  *)


Simplified:

u[r] /. First[sol] // FullSimplify Check:

u''[r] + k^2 u[r] - Piecewise[{{-v0, 0 <= r <= R}}] u[r] /.
First[sol] // PiecewiseExpand // FullSimplify
(*  0  *)

• Incredible. Anything that Mathematica recognizes as a real number appears to work in place of R, like Pi^3 or Gamma[Pi]. This suggests that Assumptions -> r > 0 && R ∈ Reals in DSolve should work, but it does not. What do you make of this? In any case, +1. Mar 3, 2016 at 5:10
• @bbgodfrey Solving for u''[t], generally the first step of (N)DSolve, yields two ConditionalExpression solutions, which should be a single Piecewise solution, imo. I think that splits the solving process into two branches (similar to your solution) that DSolve struggles with. But constructing the Piecewise solution by hand doesn't work either: Block[{R(*=Zeta*)}, DSolve[{u''[r] == Piecewise[{{-(k^2*u[r]), (R ∈ Reals && r > R) || (R ∈ Reals && r < 0)}}, -u[r] - k^2*u[r]], u == u0, Derivative[u] == up}, u[r], r, Assumptions -> R > 0]] -- (1/0 error, unless R is numeric) Mar 3, 2016 at 12:46
• Thanks Michael for your suggestion. Imposing also the u' == up condition (and avoiding symbolic expressions) did the job , compared to u == 0 alone. It's a bit sad that one has to use tricks like Zeta to "cheat" Mathematica, but as long as it works it's ok. For sure in future versions it will be more flexible. Mar 3, 2016 at 23:38