Re: NDSolve and NumericQ

*To*: mathgroup at smc.vnet.net*Subject*: [mg116371] Re: NDSolve and NumericQ*From*: Oliver Ruebenkoenig <ruebenko at wolfram.com>*Date*: Sat, 12 Feb 2011 05:19:06 -0500 (EST)*References*: <201102101024.FAA09748@smc.vnet.net>

On Thu, 10 Feb 2011, Simon Pearce wrote: > Hi Mathgroup, > > I have a differential equation in terms of y[S] which contains several complicated piecewise functions inside the ODE. I currently then use NDSolve in order to find a solution for y. This works, but takes about 10 seconds to solve. > I'd like to speed this up, and I'm wondering if NDSolve is trying to differentiate the piecewise function explicitly, as has been mentioned before in the group (http://forums.wolfram.com/mathgroup/archive/2008/Sep/msg00458.html). The solution given there is to use ?NumericQ to prevent evaluation without supplying the value, common enough (and an entirely separate issue on the group - can't we have an option in FindRoot and related functions so we can turn this off?). But I'm having trouble in doing this and using NDSolve, the method I'm trying to use currently . > > So a test problem: > > AA[y_, S_?NumericQ] := Sin[y[S]] + y'[S]/S > NDSolve[{AA[y, S] == 0, y[0] == 1}, y, {S, 0, 1}] > > Giving the error "NDSolve::dvnoarg: The function y appears with no arguments", as it tries to evaluate for a general S. It works if I take the ?NumericQ out of the definition, but that isn't what I'm trying to do. > > As an example of a function which doesn't work if we take the ?NumericQ out of the definition: > > BB[y_, S_?NumericQ] := Sin[y[S]] + y'[S]/S + NIntegrate[(Sqrt[t] + Sin[t]), {t, 0, S}] > NDSolve[{BB[y, S] == 0, y[0] == 1},y, {S, 0, 1}] > Same error again. > > CC[y_, S_] := Sin[y[S]] + y'[S]/S + NIntegrate[(Sqrt[t] + Sin[t]), {t, 0, S}] > NDSolve[{CC[y, S] == 0, y[0] == 1},y, {S, 0, 1}] > This gives errors on the NIntegrate which suggest to me that NDSolve is doing something to the equation before putting values in for S. The result is correct, but isn't immediate. > > Anyone have any idea how to do this? > > Thanks, > Simon Pearce > University of Nottingham > > This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation. > > Simon, CC[yp_, y_, S_?NumberQ] := Sin[y] + yp/S + NIntegrate[(Sqrt[t] + Sin[t]), {t, 0, S}] Block[{eps = 10^-8}, NDSolve[{CC[y'[S], y[S], S] == 0, y[eps] == 1, y'[eps] == -eps Sin[1]}, y, {S, eps, 1}, SolveDelayed -> True]] better: is = Integrate[(Sqrt[t] + Sin[t]), {t, 0, S}] DD[yp_, y_, S_?NumberQ] := Evaluate[Sin[y] + yp/S + is] Block[{eps = 10^-8}, NDSolve[{DD[y'[S], y[S], S] == 0, y[eps] == 1, y'[eps] == -eps Sin[1]}, y, {S, eps, 1}, SolveDelayed -> True]] You could also have a look at tutorial/NDSolveStateData hth, Oliver

**References**:**NDSolve and NumericQ***From:*Simon Pearce <Simon.Pearce@nottingham.ac.uk>