I'm trying to perform fittings of a model defined through a system of ODEs to data consisting of time courses of a measured quantity. As described in a previous question, the system of ODEs is as follows:
X'[t]:= m[t].X[t]
X[t_] := {h[t], r[t], rh1[t], rh2[t], rh3[t]}
m[t_] := {{-k1*r[t], 0, ki1, 0, 0}, {0, -k1*h[t], ki1, 0, 0}, {0, k1*h[t], -(ki1 + k2), ki2, 0}, {0, 0, k2, -(ki2 + k3), ki3}
and the actual quantity I need to evaluate is:
F:= aF.X[t]
aF:={0,aR,aRH1,aRH2,aRH3}
After the incorporation of some suggestions given in this forum, I end up with the following procedure to fit the model:
Model to be fitted:
modelo[k1, ki1, k2, ki2, k3, ki3, aR, aRH1, aRH2, aRH3, Ht, Rt] =
Module[{X, m, aF, sol},
X[t_] := {h[t], r[t], rh1[t], rh2[t], rh3[t]};
m[t_] := {{-k1*r[t], 0, ki1, 0, 0}, {0, -k1*h[t], ki1, 0, 0}, {0, k1*h[t], -(ki1 + k2), ki2, 0}, {0, 0, k2, -(ki2 + k3), ki3}, {0, 0, 0, k3, -ki3}};
aF = {0, aR, aRH1, aRH2, aRH3};
sol = NDSolve[{X'[t] == m[t].X[t], X[0] == {Ht, Rt, 0, 0, 0}}, X[t], {t, 0, 500}];
Function[{tu}, Evaluate[aF.Flatten[X[t] /. sol] - aR*Rt] /. t :> tu]])
Data:
data = Table[{t, .22 (1 - E^(-7.2 t)) + 0.10 (1 - E^(-0.084 t)) + 0.15 (1 - E^(-0.027 t))}, {t, 0, 500, 0.1}]
Fitting routine (leaving some parameters fixed):
k1 = 0.0012;
k2 = 0.43;
ki2 = 0.0055;
k3 = 0.01;
ki3 = 0.0096;
aR = 0.0034;
Ht = 800;
Rt = 62; (*The last three quantities are not fitting parameters but fixed ones, whose values are known. *)
fit = NonlinearModelFit[data, {modelo[k1, ki1, k2, ki2, k3, ki3, aR, aRH1, aRH2, aRH3, Ht, Rt][t], {2 <= ki1 <= 20, 0.001 <= aRH1 <= 0.1, 0.001 <= aRH2 <= 0.1, 0.001 <= aRH3 <= 0.1}}, {{ki1, 5}, {aRH1, .032}, {aRH2, .01}, {aRH3, .012}}, t, Method -> {NMinimize, Method -> "NelderMead"}]]
Here is the problem. The model works and the fitting code too, but it is too slow. For instance, it takes about 110 s (in Linux Mint MATE 1.8.1 on a dual core AMD A4 machine, with 3 GiB RAM) for a 2 iterations run and a data set of 5000 points. Given that I pretend to use the program to fit series of ~ 8 curves of ~ 14000 points, this performance is obviously too slow (it is much faster in other programs such us COPASI). Please, let me know if you have any idea on how to speed up the code.
Edit 1:
To asses the quality of the fitting I add here a new data set obtained from the model with a given set of parameter values:
newdata = Table[{t, Evaluate[With[{k1 = 0.0012, ki1 = 5, k2 = 0.43, ki2 = 0.0055, k3 = 0.01, ki3 = 0.0096, aR = 0.0034, aRH1 = .032, aRH2 = .01, aRH3 = .012},modelo[k1, ki1, k2, ki2, k3, ki3, aR, aRH1, aRH2, aRH3, Ht, Rt]]][t]}, {t, 0, 500}]