Problem description: I'm studying a chemical reaction that can be modeled by a system of coupled differential equations, and I want to use measured data to determine the parameters that appear in the equations.
(1) I come up with the following code:
data = {{0, 0.269036323, 0, 0},
{1.855, 0.26559627, 0.001414574, 0.000317798},
{2.715, 0.265004681, 0.002081772, 0.000435464},
{4.004, 0.26092304, 0.003181524, 0.000689863}}\[Transpose];
ti = data[[1, All]]; (* independent variable *)
ci = data[[2 ;; 4, All]]; (* three dependent variables *)
pfun = ParametricNDSolveValue[{
c1'[t] == -k1/(1 + k3*c1[t]) - 2*k2*k3*c1[t]/(1 + k3*c1[t]),
c2'[t] == k2*k3*c1[t]/(1 + k3*c1[t]),
c3'[t] == k1/(1 + k3*c1[t]),
c1[ti[[1]]] == ci[[1, 1]],
c2[ti[[1]]] == ci[[2, 1]],
c3[ti[[1]]] == ci[[3, 1]]},
{c1, c2, c3}, {t, 0, 20}, {k1, k2, k3}];
FindMinimum[Sum[Total[(ci[[i, All]] - Map[pfun[k1, k2, k3][[i]], ti])^2], {i, 1, 3}],
{{k1, 3.*^-4}, {k2, 1.74*^-3}, {k3, 3.81}}]
and the result is:
FindMinimum[\!\( \*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(3\)]\(Total[ \*SuperscriptBox[\((ci[[i, All]] - \(pfun[k1, k2, k3]\)[[i]] /@ ti)\), \(2\)]]\)\), {{k1, 0.0003}, {k2, 0.00174}, {k3, 3.81}}]
It merely rephrased what I entered without evaluating it.
(2) I tried also setting up the equations this way:
objfun[k_] := Module[{},
fun = NDSolveValue[{
c1'[t] == -k[[1]]/(1 + k[[3]]*c1[t]) - 2*k[[2]]*k[[3]]*c1[t]/(1 + k[[3]]*c1[t]),
c2'[t] == k[[2]]*k[[3]]*c1[t]/(1 + k[[3]]*c1[t]),
c3'[t] == k[[1]]/(1 + k[[3]]*c1[t]),
c1[ti[[1]]] == ci[[1, 1]],
c2[ti[[1]]] == ci[[2, 1]],
c3[ti[[1]]] == ci[[3, 1]]},
{c1, c2, c3}, {t, 0, 20}];
Sum[Total[(ci[[i, All]] - Map[fun[[i]], ti])^2], {i, 1, 3}]
];
FindMinimum[objfun[{k1, k2, k3}], {k1, k2, k3}]
Still it didn't evaluate:
FindMinimum[objfun[{k1, k2, k3}], {k1, k2, k3}]
What's the correct way to perform this fitting? Any suggestion would be great!