This is my first post, and I will preface my question by stating that I am relatively new to Mathematica.
In any case, I would like to fit the solution of chemical kinetics equations to data and get the resulting "MeanPredictionBands". Using the answer to a previous question as a starting point, I am able to fit a very simple set of chemical kinetics equations to two data sets self-consistently (see example code below).
tmax = 100.; (*region of time in which we are interested to have a \
solution*)
(*Define a simple chemical kinetics coupled differential equation*)
sol = ParametricNDSolveValue[
{D[s1[t], t] == k1 s1[t] s2[t],
D[s2[t], t] == -k1 s1[t] s2[t],
s1[0] == 0.1,
s2[0] == 10.},
{s1, s2},
{t, 0, tmax},
{k1}
];
(*temp variables that help us avoid solving the coupled differential \
equations over and over if "model" is called without changing k1temp*)
k1temp = 0.1;
soltemp = sol[k1temp];
(*the function definition that will return the solution of the \
coupled differential equations for a given k1, species and time*)
model[k1_?NumericQ][species_?NumericQ, t_?NumericQ] :=
Module[{returnval, intspecies},
If[k1 != k1temp,
soltemp = sol[k1];
k1temp = k1;
];
intspecies = Round[species];
soltemp[[intspecies]][t]
];
(*Make some fake data using model*)
data = {};
Do[
Do[
noise = RandomReal[{-0.5, 0.5}];
AppendTo[data, {i, t, model[0.2][i, t] + noise}];
, {t, 0., tmax/10.}]
, {i, 1, 2}];
(*fit the fake data*)
fit = NonlinearModelFit[data, model[k1f][i, t], {{k1f, 0.3}}, {i, t}];
fit["ParameterTable"]
Using the FittedModel from NonlinearModelFit, I expected it to be straight forward to get the "MeanPredictionBands" via the following function definition.
meanpredictionbandsfunc[i_?NumericQ, t_?NumericQ] =
fit["MeanPredictionBands"]
This function definition, however, is giving me a number of warnings/errors and when evaluated it does not give me the "MeanPredictionBands" as I would expect. For example, the evaluation
meanpredictionbandsfunc[1, 10]
is giving me the following errors
StringForm::sfr: Item 2 requested in "`1` cannot be interpreted. The operator `2` requires a subscript with a variable specification." out of range; 1 items available.
Internal`LocalizedBlock::novar: 1 cannot be interpreted. The operator `2` requires a subscript with a variable specification.
StringForm::sfr: Item 2 requested in "`1` cannot be interpreted. The operator `2` requires a subscript with a variable specification." out of range; 1 items available.
Internal`LocalizedBlock::novar: 1 cannot be interpreted. The operator `2` requires a subscript with a variable specification.
StringForm::sfr: Item 2 requested in "`1` cannot be interpreted. The operator `2` requires a subscript with a variable specification." out of range; 1 items available.
Internal`LocalizedBlock::novar: 1 cannot be interpreted. The operator `2` requires a subscript with a variable specification.
How should I define a function to provide me with the "MeanPredictionBands"?
I would really appreciate your help/suggestions!
