RE: NDSolve and Lane-Emden
- To: mathgroup at smc.vnet.net
- Subject: [mg42548] RE: [mg42512] NDSolve and Lane-Emden
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Mon, 14 Jul 2003 05:42:18 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: fedgingt at fandm.edu [mailto:fedgingt at fandm.edu] To: mathgroup at smc.vnet.net >Sent: Saturday, July 12, 2003 11:19 AM >To: mathgroup at smc.vnet.net >Subject: [mg42548] [mg42512] NDSolve and Lane-Emden > > >Hi. I have not worked extensively with Mathematica in the >past. I am working >with the Lane-Emden equation for a polytropic gas: >-y[x]^N=1/x^2*D[x^2D[y[x],x],x]. N is the polytropic index. >I have been using >NDSolve to find a solution to this equation when N=3.3, 3.35 >and 3.4 for x >starting a 1. However, I am have trouble with the >InterpolatingFunction >Mathematica gives me. It does not give numerical answers (for >example, when I >type y[2]/.% the output Mathematica gives is {y[2]}.) At the >same time, I am >able to plot this function. Is there some way to get a result for the >Lane-Emden that will produce numerical answers? Thanks, Fred > You'r not telling quite enough (what was % ??, how did you manage to plot?), but this works: In[13]:= res = NDSolve[{-y[x]^3.3 == (2*x*Derivative[1][y][x] + x^2*Derivative[2][y][x])/x^2, y[1] == 1, Derivative[1][y][1] == 0}, y[x], {x, 1, 5}] Out[13]= {{y[x] -> InterpolatingFunction[{{1., 5.}}, "<>"][x]}} In[15]:= Plot[Evaluate[y[x] /. res[[1]]], {x, 1, 5}] This gives a plot... ...and this a value for x == 2 In[17]:= y[x] /. res[[1]] /. x -> 2 Out[17]= 0.731295 You may define your solution as In[18]:= f[x_] = y[x] /. res[[1]] Out[18]= InterpolatingFunction[{{1., 5.}}, "<>"][x] In[19]:= f[2] Out[19]= 0.731295 (Instead of (the letter) f you might have used y, but I'd not recommend doing that, because that mixes up a formal expression (for a solution function, something like a variable in an algebraic equation) with a concrete solution (something like a solution value e.g. a real number, in an algebraic equation).) However if you do In[25]:= res2 = NDSolve[{-y[x]^3.3 == (2*x*Derivative[1][y][x] + x^2*Derivative[2][y][x])/x^2, y[1] == 1, Derivative[1][y][1] == 0}, y, {x, 1, 5}] Out[25]= {{y -> InterpolatingFunction[{{1., 5.}}, "<>"]}} looking for the function y, not the expression y[x], then more easily In[26]:= (y /. res2[[1]])[2] Out[26]= 0.731295 -- Hartmut Wolf