I am trying to numerically solve a non-linear ODE with variable initial conditions, specifically it is :

$$ \partial_{t}R (r,t) ^2 + 2 R(r,t) \partial_{t} ^2 R(r,t) +r^2 k(r) = 0 $$ and my initial conditions are $$ R(r,1) = r (1-\frac{r}{10})^{2/3} \ \ \ \partial_{t} R(r,1) = \frac{2}{3} \frac{r}{(1-\frac{r}{10})^{1/3}}.$$

Here $t >1 $ and $0\leq r \leq 1$ and $k(r) = \frac{(1-(r^2-1-10^{-4})^{20}}{r^2}. $

I solve this equation two ways :

1) By treating $R$ as a function of $t$ alone, and defining a solution function using NDSolve:

sol[r_]:= NDSolve[{R'[t]^2+ 2 R[t] R''[t]==-r^2 k[r], R[1]== r(1-r/10)^2/3, 
    R'[1]==(2/3) 4/(1-r/10)^(1/3)},R,{t,1,3}, WorkingPrecision-> 50,
    AccuracyGoal->40, PrecisionGoal->40, InterpolationOrder->All]

Then I can just compute $sol[r][t]$ which is really $R[r,t]$ for any $r$. When I plot the left hand side of the differential equation for any $r$, by setting the WorkingPrecision to around 20 I exactly get zero, as I should. The disadvantage here is that I cannot treat $sol[r][t]$ as a function of $r$. I cannot differentiate it with respect to $r$, or at least I don't know how.

2) I solve it using

sol= NDSolve[{D[R[r,t],t]^2 + 2 R[r,t] D[R[r,t],{t,2}]==-r^2 k[r], R[r,1]== r(1-r/10)^2/3,
    Derivative[0,1][R][r,1]==(2/3) 4/(1-r/10)^(1/3)},R,{t,1,3}, {r,9/10,1}, 
    WorkingPrecision-> 50, AccuracyGoal->40, PrecisionGoal->40, InterpolationOrder->All]

Then I am in trouble because when I evaluate the left hand side using the numerical solution I do not get zero for all values of $r$.

I think something gets screwed up as it multiplies interpolating functions together, but I have no idea how to fix it.

  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Jul 11, 2015 at 0:40
  • 1
    $\begingroup$ Try using ParametricNDSolve,with r as the parameter. According to the documentation, it is possible to differentiate with respect to the parameter. $\endgroup$
    – bbgodfrey
    Jul 11, 2015 at 0:51

1 Answer 1



k[r] = (1 - (r^2 - 1 - 10^-4)^20)/r^2


sol = ParametricNDSolveValue[{R'[t]^2 + 2 R[t] R''[t] == -r^2 k[r], 
   R[1] == r (1 - r/10)^2/3, R'[1] == (2/3) 4/(1 - r/10)^(1/3)}, 
   R, {t, 1, 3}, {r}];
dsol = Derivative[1][sol]

and then plot, for example, R[1][t], R'[1][t], and (to verify that this really is the derivative with respect to r), (R[1.1]-R[1])/.1.

Plot[{sol[1][t], dsol[1][t], (sol[1.1][t] - sol[1.0][t])/.1}, {t, 1, 3}]

enter image description here

A 3D plot shows that the function is smooth except as r approaches 0.

Plot3D[sol[r][t], {t, 1, 3}, {r, 0.006, 1}, AxesLabel -> {t, r, R}, 
 LabelStyle -> Directive[Black, Bold, 12]]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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