# ParametricNDSolveValue or NDSolve + fitting

I have been trying to find the value for the parameter kestim that yields the best fit of a model to some data points. datac has 25 data points, but I illustrate with just a few. I tried to do the fitting after using ParametricNDSolveValue:

datac={{0,73},{300,605},{600,1244},{900,1874},{1200,2000},{1500,2900},{1800,3300}};

pfun = ParametricNDSolveValue[{
[MODEL]
} /. parameters,
{G,T,H,Y,U},
{t, 0, 7200}, {kestim}]

(*because variable Y is the one I am interested, I specify it in the next command*)
f[x_]:=pfun[x][[4]]

(*I evaluate f[1.1][600] I get the correct value of variable Y. I then fit it to the data.*)
fit = NonlinearModelFit[datac, f[kestim][t], {kestim}, {t}]


Yet, I get the error Part::partw: "Part 4 of \!\(\*TagBox[RowBox[{\"ParametricFunction\" [etc etc] does not exist.>> and the output FittedModel[InterpolatingFunction[{{0.,7200.}},<>][t]]. From the latter I am able to extract the value of kestim, although the value obtained for this parameter is not significant.

I know I am not providing enough information, but I really cannot show my model here. Yet, do you think you could help me find what I am doing wrong? Thank you so much!

Edit: What was going wrong was probably the ReplaceAll inside pfun. Now it is working. I provide a draft version of the model below:

pfun = ParametricNDSolveValue[{
G'[t] == 50 - 6*^3 T[t] G[t], G[0] == 0,
T'[t] == 73 H[t] - 11 T[t], T[0] == 25000,
H'[t] == ke (1 - T[t] - H[t]) - 73 H[t], H[0] == 0
},
{G,T,H},
{t, 0, 7200}, {ke}];

(*as per suggestion of @ruebenko*)
f[ke_?NumericQ] := pfun[ke][[3]]

f[1.1][600]
(*Out=0.0135135*)

-
Probably best to post complete code. As it stands there are too many ways something can go wrong for people to sensibly speculate (unless they are much more familiar with this kind of problem than I am). – Daniel Lichtblau Jan 18 '13 at 17:42
Could you try f[x_?NumericQ]:=.... and see what happens. Also, you could use {Y} - no need to give the entirelist of dependent variables if you just need one. If NDSolve chokes you could specify DependentVariables->{G,T,H,Y,U} – user21 Jan 20 '13 at 8:43
@Sosi, did you solve this problem already? – Cendo Aug 7 '13 at 15:42
@Cendo I had followed ruebenko's suggestions, but it didn't solve the problem. Indeed, DanielLichtblau was right and the problem was pinned with the model. I provided a full example of the solution in the thread – Sosi Aug 8 '13 at 10:56
Just to note that the link @Ghersic mentions is to a notebook with basically the same content as my answer here. I had written this for my own purposes before I had reason to use it in an answer. – Oleksandr R. Aug 23 '13 at 13:43