title: NDSolve inside NDSolve in Mathematica
body: <p>I need to use NDSolve which in turn uses the solution from another ODE as function in terms of output from another NDSolve.</p>
  
  <p>If I use the exact solution from the first differential equation inside the NDSolve, it's OK. But when I use the same solution in the form of function (which uses InterpolatingFunction) it does not work.</p>
  
  <p>I believe, it's got to do with the structure of NDSolve output. Could anyone please enlighten me on this. Will be of great help!</p>
  
  <p>The code is:</p>
  
  <hr>
  
  <p><code>feq = 2 V alpha fip F''[fi] - (V^2 - (V^2 + sigma - 2 fi) (F'[fi])^2 + (F'[fi])^4</code></p>
  
  <p><code>Frange[lo_, hi_] := 
   Module[{fii, sol},
     sol = NDSolve[{(feq == 0 /.fi -&gt; fii), F[0] == 0}, F, {fii, lo, hi}]]</code></p>
  
  <p><code>eqpois = fi''[x] == ne[x] - F[fi[x]]/.sol</code></p>
  
  <p><code>NDSolve[{eqpois, fi'[0] == 0, fi[0] == 0}, fi, {x,0,1}]</code></p>
  
  <p>Here in order to find F[phi], I need to solve the 1st diff eq that is feq, which is solved by NDSolve inside the function Frange[lo,hi]. The solution is then used inside the second equation eqpois, which has to be solved using NDSolve again. The problem comes up in the second NDSolve, which does not produce the result. If I use the analytical solution of F[phi] in eqopis, then there is no problem.</p>
  
  <hr>
  
  <p><strong>Example Problem</strong></p>
  
  <p>I have done a little experiment with this. Let's take an example of coupled ODEs</p>
  
  <p><code>1st eqn : dg/dx = 2f(g)</code> with initial condition <code>g(0) = 1</code></p>
  
  <p>The function <code>f(y)</code> is a solution from another ODE, say,</p>
  
  <p><code>2nd eqn : df/dy = 2y</code> with IC <code>f(0) = 0</code></p>
  
  <p>The solution of the 2nd ODE is <code>f(y) = y^2</code> which when put into the the 1st ODE becomes</p>
  
  <p><code>dg/dx = 2 g^2</code> and the final solution is <code>g(x) = 1/(1-2x)</code></p>
  
  <p><strong>The issue:</strong></p>
  
  <p>When I use DSolve, it finds the answer correctly</p>
  
  <p><code>In[39]:= s = DSolve[{f'[y] == 2 y, f[0] == 0}, f, y]</code></p>
  
  <p><code>Out[39]= {{f -&gt; Function[{y}, y^2]}}</code></p>
  
  <p><code>In[40]:= ss = DSolve[{g'[x] == 2 (f[g[x]]/.First@s), g[0] == 1}, g, x]</code></p>
  
  <p><code>Out[40]= {{g -&gt; Function[{y}, 1/(1 - 2 x)]}}</code></p>
  
  <p>The problem comes when I use NDSolve</p>
  
  <p><code>In[41]:= s = NDSolve[{f'[y] == 2 y, f[0] == 0}, f, {y, 1, 5}]</code></p>
  
  <p><code>Out[41]= {{f -&gt; InterpolatingFunction[{{1., 5.}}, &lt;&gt;]}}</code></p>
  
  <p><code>In[42]:= ss1 = NDSolve[{g'[x] == 2 (Evaluate[f[g[x]]/.First@s1]), g[0] == 1}, g, {x, 1, 2}]</code></p>
  
  <p><code>Out[42]= {}</code></p>
  
  <p>The erros are:</p>
  
  <p><em>During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.01726} lies outside the range of data in the interpolating function. Extrapolation will be used. >></em></p>
  
  <p><em>During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.01726} lies outside the range of data in the interpolating function. Extrapolation will be used. >></em></p>
  
  <p><em>During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.04914} lies outside the range of data in the interpolating function. Extrapolation will be used. >></em></p>
  
  <p><em>During evaluation of In[41]:= General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation. >></em></p>
  
  <p><em>During evaluation of In[41]:= NDSolve::ndsz: At y == 0.16666654771477857, step size is effectively zero; singularity or stiff system suspected. >></em></p>
  
  <p><em>During evaluation of In[41]:= NDSolve::ndsz: At y == 0.16666654771477857, step size is effectively zero; singularity or stiff system suspected. >></em></p>
  
  <p>Any help in this regard will be highly appreciated!</p>
  
  <p>--- Madhurjya</p>