Note Added in proof
Oleksander's answer provides a better fit to the data than my solution below and circumvents the reiteration-and-solving-individually problem I describe below.
This is not an answer to your question specifically, but rather is one method to use ParametricNDSolve
to fit experimental data.
Defining the problem
$\require{mhchem}$
Differential equations come in to play in chemical kinetics, most notably in determining the rates of reactions. Consider the following reaction $\ce{A -> B + C}$. To my knowledge, the set of equations that describe the differential rate laws for the three components of this system cannot be solved analytically. Let's say that we have collected a set of concentration vs. time data for each of the three species A, B, and C and would like to fit the data to this chemical equation. First, I will make some data, noting that if the rate constant for the reverse reaction is k then the rate of the forward reaction can be given by K x k where K is the equilibrium constant for the chemical equation.
tdata = NDSolve[{a'[t] == -b'[t] == -c'[t] == -k K a[t] + k b[t] c[t],
a[0] == a0, b[0] == b0, c[0] == c0} /. {K -> 3, k -> 1,
a0 -> 0.5, b0 -> 0.4, c0 -> 0.1}, {a[t], b[t], c[t]}, {t, 0, 1}]
edata = Flatten[
Table[{t, RandomReal[{0.98, 1.02}] a[t],
RandomReal[{0.98, 1.02}] b[t],
RandomReal[{0.98, 1.02}] c[t]} /. tdata, {t, 0, 1, 0.05}], 1];
Here, I've chosen the answers to be k = 1, K = 3, and the initial concentrations of A, B, and C to be 0.5, 0.4, and 0.1, respectively.
Solving the problem
We start by using ParametricNDSolve
to generate the interpolation functions, and generate a plot just to make sure we are on the right track.
sol = ParametricNDSolve[{a'[t] == -b'[t] == -c'[t] == -k K a[t] +
k b[t] c[t], a[0] == a0, b[0] == b0, c[0] == c0}, {a, b, c}, {t,
0, 1}, {k, K, a0, b0, c0}];
a1 = a[1, 3, 0.5, 0.4, 0.1] /. sol;
b1 = b[1, 3, 0.5, 0.4, 0.1] /. sol;
c1 = c[1, 3, 0.5, 0.4, 0.1] /. sol;
Plot[Evaluate@{a1[t], b1[t], c1[t]}, {t, 0, 1}]
Using NonlinearModelFit
with ParametricNDsolve
solutions
The solutions from ParametricNDSolve
can be used directly in the NonlinearModelFit function, providing results that seem to provide a decent fit. Note that fitting the concentration data for B and C threw errors (more on that below).
nlma = NonlinearModelFit[edata[[All, {1, 2}]],
a[k, K, a0, b0, c0][t] /. sol, {k, K, a0, b0, c0}, t]
nlmb = NonlinearModelFit[edata[[All, {1, 3}]],
b[k, K, a0, b0, c0][t] /. sol, {k, K, a0, b0, c0}, t]
nlmc = NonlinearModelFit[edata[[All, {1, 4}]],
c[k, K, a0, b0, c0][t] /. sol, {k, K, a0, b0, c0}, t]
The results look pretty good, however the parameters are actually meaningless:
Adding constraints
This problem can be improved by adding constraints to the NonlinearModelFit
nlma2 = NonlinearModelFit[
edata[[All, {1, 2}]], {a[k, K, a0, b0, c0][t] /. sol2, k > 0, K > 0,
0 < a0 < 1, 0 < b0 < 1, 0 < c0 < 1}, {k, K, a0, b0, c0}, t]
nlmb2 = NonlinearModelFit[
edata[[All, {1, 3}]], {b[k, K, a0, b0, c0][t] /. sol2, k > 0, K > 0,
0 < a0 < 1, 0 < b0 < 1, 0 < c0 < 1}, {k, K, a0, b0, c0}, t]
nlmc2 = NonlinearModelFit[
edata[[All, {1, 4}]], {c[k, K, a0, b0, c0][t] /. sol2, k > 0, K > 0,
0 < a0 < 1, 0 < b0 < 1, 0 < c0 < 1}, {k, K, a0, b0, c0}, t]
The fit is better, but still not great, and it is important to note that the data sets for each individual concentration give varying optimal parameters. I'm a fan of the brute force and ignorance approach, and one way to address the problem with the parameters is to take the Mean
and StandardDeviation
of the "optimal" parameters and use these as new constraints.
(* New stuff *)
mean = Mean[{k, K, a0, b0, c0} /. #["BestFitParameters"] & /@ {nlma2,
nlmb2, nlmc2}]
sd = StandardDeviation[{k, K, a0, b0, c0} /. #[
"BestFitParameters"] & /@ {nlma2, nlmb2, nlmc2}]
const = MapThread[{#1 - #2 < #3 < #1 + #2} &, {mean,
sd, {k, K, a0, b0, c0}}]
(* old stuff *)
nlma3 = NonlinearModelFit[
edata[[All, {1, 2}]], {a[k, K, a0, b0, c0][t] /. sol2, const}, {k,
K, a0, b0, c0}, t]
nlmb3 = NonlinearModelFit[
edata[[All, {1, 3}]], {b[k, K, a0, b0, c0][t] /. sol2, const}, {k,
K, a0, b0, c0}, t]
nlmc3 = NonlinearModelFit[
edata[[All, {1, 4}]], {c[k, K, a0, b0, c0][t] /. sol2, const}, {k,
K, a0, b0, c0}, t]
TableForm[{k, K, a0, b0, c0} /. #["BestFitParameters"] & /@ {nlma3,
nlmb3, nlmc3},
TableHeadings -> {{"a", "b", "c"}, {"k(1)", "K(3)", "a0(0.5)",
"b0(0.4)", "c0(0.1)"}}]
We are now starting to get agreement between the three sets of data. After repeating this loop approximately a dozen times, the results start to settle:
Conclusion
We can see that some of the best fit parameters are reasonably close to the actual values (concentrations of A and B) what might be considered the important values (k and K) have appreciable, but possibly acceptable errors (on the order of 20 to 30%). I presume, although have not tested, that part of the problem lies in redefining the constraints for concentration of C, which from the first iteration did not contain the true value. The conclusion to draw from this exercise is that a complex model (and this is not a terribly complex model) will give you a fit of the data, but you need to know something about the reality of the parameters before you can actually trust the fit. Maybe Mathematica version 10 will provide that insight, but as of now, we must use our own brains to determine the value of a fit.
pfun[{k1_,k2_…,k10_}]:= *definition*
or something? I'm afraid I have to agree with the other comments that your task is almost impossible with the paucity of data and the number of parameters. (This would probably never survive a sensitivity analysis) But, I am interested in the general problem of fitting differential equation parameters to data sets... $\endgroup$