Re: Problem with spline function object in Mathematica 6
- To: mathgroup at smc.vnet.net
- Subject: [mg95684] Re: Problem with spline function object in Mathematica 6
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 25 Jan 2009 06:48:35 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <gletej$4mm$1@smc.vnet.net>
In article <gletej$4mm$1 at smc.vnet.net>, borisov at sas.upenn.edu wrote:
[snip]
> Here is my current code:
> SplineTable={{0,0},{1,1},{2,8},{3,27},{4,64}};
> Spl=SplineFit[SplineTable,Cubic];
> derivSpline=ReplacePart[Spl,Map[Append[Rest[#]*{1,2,3},0]&,Spl[[4]],{2}],4];
> dMdr[x_]:=Last[derivSpline[x]]/First[derivSpline[x]];
> drdt[x_] := First[derivSpline[x]];
> sol3 = NDSolve[{param[0]==0,(param'[x]-(1/drdt[param[x]]))==0},param,{x,0,4}]
>
> So again the problem I have is that drdt[param[x]] evaluates to param[x]
> automatically (and immediately gives a division by 0 error in NDSolve) instead
> of attempting to evaluate param[x] to a number first.
> The same happens with dMdr[x]. My goal in the end will be to evaluate
> dMdr[param[x]] correctly when supplied with x.
Add a test _?NumericQ to the parameters of the functions so they are
called only when the arguments are numeric.
dMdr[x_?NumericQ] := Last[derivSpline[x]]/First[derivSpline[x]];
drdt[x_?NumericQ] := First[derivSpline[x]];
For instance,
In[1]:= Needs["Splines`"]
SplineTable = {{0, 0}, {1, 1}, {2, 8}, {3, 27}, {4, 64}};
Spl = SplineFit[SplineTable, Cubic];
derivSpline =
ReplacePart[Spl, Map[Append[Rest[#]*{1, 2, 3}, 0] &, Spl[[4]], {2}],
4];
dMdr[x_?NumericQ] := Last[derivSpline[x]]/First[derivSpline[x]];
drdt[x_?NumericQ] := First[derivSpline[x]];
sol3 = NDSolve[{param[0] == 0, (param'[x] - (1/drdt[param[x]])) == 0},
param, {x, 0, 4}]
Out[7]= {{param->InterpolatingFunction[{{0.,4.}},<>]}}
Regards,
--Jean-Marc