Message Boards Message Boards

Why I get obscure error messages in NDSolve?

Posted 2 months ago

Hello, after my failures to force NDSolve to solve any of my PDEs, I decided to try out a supposedly simpler solution of two-point BVPs in 2nd order ODEs by NDSolve. Here again NDSolve fails to solve my BVP directly, but I devised my own shooting procedure that uses NDSolve only to obtain a solution of an initial value problem for a second order ODE. A single call to NDSolve produces quickly and accurately a desired result (without error messages), but if I combine NDSolve with FindRoot to automate the iterations of the shooting algorithm, I get obscure error messages (although the computed numerical result seems correct). Can anybody tell me how to get rid of these messages and why they occur? My code is as follows:

SS0[wm_,g0_,wp_]:=NDSolve[{psi''[v]+2*v*psi'[v]==0,psi[0]==0,psi'[0]==g0},psi,{v,0,wm},WorkingPrecision->wp ];
(* Expected gradient g0 of psi[w] at w=0:  *)
N[2/Sqrt[π],20]
1.1283791670955125739
(* Hence, assumed interval of g0 for the Brent method:  1.0 < g0 < 1.2  *)
g0min=1;
g0max=12/10;
(* Assumed maximum w value: *)
wmax = 10;
(* Requested working precision *)
wprec = 40;
(* Numerical solution for psi[w] at w=wmax by the shooting method *)
psiwmax[g_]:=Module[{},sol=SS0[wmax,g,wprec];Evaluate[psi[wmax]/.sol]];
(* Determination of the gradient at w=0 that makes psiwmax = 1 *)
g0acc=FindRoot[psiwmax[g0]==1,{g0,g0min,g0max},Method->"Brent",WorkingPrecision->wprec]

This produces error messages, plus a correct result for g0:

 NDSolve::ndinnt: Initial condition g0 is not a number or a rectangular array of numbers.
    ReplaceAll::reps: {NDSolve[{2 v (psi^′)[v]+(psi^′′)[v]==0,psi[0]==0,(psi^′)[0]==g0},psi,{v,0,10},WorkingPrecision->40]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.
    NDSolve::ndinnt: Initial condition g0 is not a number or a rectangular array of numbers.

    {g0->1.128379167095512573947307418293139931938}
POSTED BY: Leslaw Bieniasz
2 Replies

It's because FindRoot evaluates psiwmax[g0] first with symbolic g0 and analyzes the result. This causes the error message. Then FindRoot evaluates psiwmax[g0] with numeric values substituted for g0, and everything works fine after that.

The standard way to prevent the error message is to define numerical functions using NumericQ to prevent evaluation on nonnumeric arguments. Wolfram has a Knowledgebase article on this.

Example coding paradigm:

ClearAll[psiwmax];
psiwmax[g0_?NumericQ] := ....
POSTED BY: Michael Rogers

This is also why Compile supports RuntimeOptions->{"EvaluateSymbolically"->False}. That way compiled numerical functions don't run when presented with symbolic arguments, avoiding errors.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract