Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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] == 0}, u[r], {r, 0, Infinity}]

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

share|improve this question
1  
Use Piecewise to represent V[r]. Understand, though, that DSolve cannot solve all ODEs. – bbgodfrey Mar 3 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. – anthony2005 Mar 3 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] == 0}, u[r], r] – J. M. Mar 3 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. – bbgodfrey Mar 3 at 1:29
2  
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. – tsuresuregusa Mar 3 at 2:03
up vote 8 down vote accepted

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] == 0}, u1[r], 
    r, Assumptions -> k^2 + v0 > 0][[1, 1]] /. C[1] -> -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.

share|improve this answer
    
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)$ ... – anthony2005 Mar 3 at 1:40
    
@AntonioCox Here is the corrected answer. – bbgodfrey Mar 3 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! – anthony2005 Mar 3 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. – bbgodfrey Mar 3 at 3:26

Well, here's an indirect way, using the value Zeta[3] 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'[0].

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

Simplified:

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

Mathematica graphics

Check:

u''[r] + k^2 u[r] - Piecewise[{{-v0, 0 <= r <= R}}] u[r] /. 
   First[sol] // PiecewiseExpand // FullSimplify
(*  0  *)
share|improve this answer
    
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. – bbgodfrey Mar 3 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[3]*)}, DSolve[{u''[r] == Piecewise[{{-(k^2*u[r]), (R ∈ Reals && r > R) || (R ∈ Reals && r < 0)}}, -u[r] - k^2*u[r]], u[0] == u0, Derivative[1][u][0] == up}, u[r], r, Assumptions -> R > 0]] -- (1/0 error, unless R is numeric) – Michael E2 Mar 3 at 12:46
    
Thanks Michael for your suggestion. Imposing also the u'[0] == up condition (and avoiding symbolic expressions) did the job , compared to u[0] == 0 alone. It's a bit sad that one has to use tricks like Zeta[3] to "cheat" Mathematica, but as long as it works it's ok. For sure in future versions it will be more flexible. – anthony2005 Mar 3 at 23:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.