RE: Immediate or Delayed Definitions in NDSolve?
- To: mathgroup at smc.vnet.net
- Subject: [mg45023] RE: [mg44993] Immediate or Delayed Definitions in NDSolve?
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 13 Dec 2003 06:06:13 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Well, you... >-----Original Message----- >From: extrabyte [mailto:extrabyte22 at libero.it] To: mathgroup at smc.vnet.net >Sent: Friday, December 12, 2003 10:42 AM >To: mathgroup at smc.vnet.net >Subject: [mg45023] [mg44993] Immediate or Delayed Definitions in NDSolve? > [...] >I have a serious problem with NDSolve. > but it seems not result in that question for NDSolve > >Immediate or Delayed Definitions in NDSolve?? > Instead, I'll try to comment on what you have done. How to do it right, then should become obvious. >Method 1 (Delayed Definition) >^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >In[1]: eq[a_] := y''[x] + a^2*y[x] == 0; > >In[2]: sol[a_] := NDSolve[{eq[a], y[0] == 1, y'[0] == -1}, y, >{x, 0, 10}] > Of course "sol" is a completely misleading name for such an object. This sol[a] may be understood as a command to do NDSolve with eq[a] and the initial conditions, and range as specified, **provided** a numerical argument is supplied for a. e.g.: In[3]:= aSolutionSet = sol[1.] Out[3]= {{y -> InterpolatingFunction[{{0., 10.}}, "<>"]}} In[4]:= s1 = y /. aSolutionSet[[1]] Out[4]= InterpolatingFunction[{{0., 10.}}, "<>"] s1 now is a solution function, which may be plotted within it's domain {0., 10.}: In[5]:= Plot[s1[x], {x, 0., 10.}] >In[3]: sol[a] > > NDSolve::"ndnum": "Encountered non-numerical value for a >derivative at \ >\!\(x\) == \!\(5.840380984046049`*^-301\)." > >Out[3]:{{y -> InterpolatingFunction[{{0., 0.}}, "<>"]}} > Of course this is humbug! (look back at the expression, your In[2], where a has become a, a Symbol, and NDSolve can't do anything reasonable with eq[a]; a better name for sol (solution ??)would have been doNDSolveForeqWithNumericValueForSymbola) In[7]:= whatthehellisthis = y /. sol[a][[1]] >From In[7]:= NDSolve::"ndnum": "Encountered non-numerical value for a derivative at \ \!\(x\) == \!\(2.3997329129377998`*^175\)." Out[7]= InterpolatingFunction[{{0., 0.}}, "<>"] What's this? Well, a spurious result, an InterpolatingFunction, defined for zero range {0., 0.}. Just don't think about it! In[8]:= whatthehellisthis // InputForm Out[8]//InputForm= InterpolatingFunction[{{0., 0.}}, {1, 1, False, Real, {3}, {0}}, {{0.}}, {{0, 2}, {1., -1.}}] In[9]:= whatthehellisthis[0.] Out[9]= 1. That works, of course. But then that's all: In[10]:= whatthehellisthis /@ Range[0,10] >From In[10]:= InterpolatingFunction::"dmval": "Input value \!\({1}\) lies outside the range \ of data in the interpolating function. Extrapolation will be used." >From In[10]:= InterpolatingFunction::"dmval": "Input value \!\({2}\) lies outside the range \ of data in the interpolating function. Extrapolation will be used." >From In[10]:= InterpolatingFunction::"dmval": "Input value \!\({3}\) lies outside the range \ of data in the interpolating function. Extrapolation will be used." >From In[10]:= General::"stop": "Further output of \!\(InterpolatingFunction :: \"dmval\"\) \ will be suppressed during this calculation." Out[10]= {1., 0., -1., -2., -3., -4., -5., -6., -7., -8., -9.} >In[4]: f[x_, a_] := y[x] /. sol[a][[1]] > Not so much a lucky definition, as for any x, NDSolve must be executed (in the Plot below, over and over again! >In[5]: f[1, 1] > This is well defined. >In[6]:Plot[f[x, 2], {x, 0, 10}] > >[PLOT HERE] >Out[6] - Graphics - > Inefficient, but it works. >In[7] f[x, a] > >NDSolve::"ndnum": "Encountered non-numerical value for a >derivative at \ >\!\(x\) == \!\(5.840380984046049`*^-301\)." > >Out[7] InterpolatingFunction[{{0., 0.}}, "<>"][x] > Same nonsense as sol[a] above >In[8] Table[{x, f[x, 1]}, {x, 0, 10}] > >General::"ivar": "\!\(0\) is not a valid variable." >NDSolve::"dsvar": "\!\(0\) cannot be used as a variable." > >[SNIPPED] Here you must understand how Table works (have a look at the book!). Effectively this is the same as for In[14]:= Block[{x = 0}, {x, f[x, 1]}] >From In[14]:= General::"ivar": "\!\(0\) is not a valid variable." >From In[14]:= NDSolve::"dsvar": "\!\(0\) cannot be used as a variable." >From In[14]:= ReplaceAll::"reps" : {{y[0] + y"[0] == 0, y[0] == 1, y'[0] == -1}} is neither a list of replacement rules nor a valid dispatch table, and so \ cannot be used for replacing. Out[14]= {0, y[0] /. {y[0] +y"[0] == 0, y[0] == 1, y'[0] == -1}} If you look at this, and compare it with somehing you typed in, perhaps you'll get the idea. As now NDSolve is called (at Table), first value x = 0, now creeps in as value for (global) x inside eq[a], destroying your differential equation; rest is nonsense of course. >^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >Example 2 (Immediate Definition) > >In[1]: eq[a_] := y''[x] + a^2*y[x] == 0; > >In[2]: sol[a_] := NDSolve[{eq[a], y[0] == 1, y'[0] == -1}, y, >{x, 0, 10}] > >In[3]: sol[a] > > NDSolve::"ndnum": "Encountered non-numerical value for a >derivative at \ >\!\(x\) == \!\(5.840380984046049`*^-301\)." > >Out[3]:{{y -> InterpolatingFunction[{{0., 0.}}, "<>"]}} > This is the same as above, and you'r already wrecked here. >In[4]: f[x_, a_] = y[x] /. sol[a][[1]] > >NDSolve::"ndnum": "Encountered non-numerical value for a >derivative at \ >\!\(x\) == \!\(5.840380984046049`*^-301\)." > >Out[5]: InterpolatingFunction[{{0., 0.}}, "<>"][x] > This (as In[3]) calls NDSolve with symbolic a, which prevents for NDSolve to do the numerical integrations, and results in the same spurious InterpolatingFunction as above with domain {0., 0.} (look at your results!) >In[6]: f[1, 1] > >InterpolatingFunction::"dmval": "Input value \!\({1}\) lies outside the >range \ >of data in the interpolating function. Extrapolation will be used." > >Out[6]: 0 Should be clear now. > >In[7]: Table[{x, f[x, 1]}, {x, 0, 10}] >InterpolatingFunction::"dmval": "Input value \!\({1}\) lies outside the >range \ >of data in the interpolating function. Extrapolation will be used." >InterpolatingFunction::"dmval": "Input value \!\({2}\) lies outside the >range \ >of data in the interpolating function. Extrapolation will be used." >[snip] > >Out[7]: {{0, 1.}, {1, 0.}, {2, -1.}, {3, -2.}, {4, -3.}, > {5, -4.}, {6, -5.}, {7, -6.}, {8, -7.}, {9, -8.}, > {10, -9.}} > Again you've got the same whatthehellisthis as (I did) above! >^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > >Immediate or Delayed Definitions in NDSolve?? > As you will have seen, that was not the question! A way to get at a working solution (function) has been shown with s1 above. -- Hartmut Wolf