1

This might be some simple mistake, but I am running out of ideas. I am trying to fit an ODE system to some data. This error keeps appearing

NDSolve::nlnum: "The function value {0.299812 - 0.000145006 a, 0.001 -0.000145006 a} is not a list of numbers with dimensions {2} at {t, sa[t], st[t]} = {0.000483352, 0.000145006, 1.}."

The code is adapted to How do I find the best parameter to fit my data if the model is a interpolating function? :

data = {
 {0.`, 34.84229229652888`}, {3.7448333333333332`, 29.004820470134124`}, 
 {7.4896666666666665`, 22.762395022844483`}, {11.2345`, 16.82225654209896`}
 }

Clear[kss, kdm, at, gof];

kss = 0.001; kdm = 0; at = 0.3;

eqs[kas_, k2s_, at_] := {
  st'[t] == kss - k2s sa[t] - kdm st[t],
  sa'[t] == kas (at - sa[t]) (st[t] - sa[t]) - k2s sa[t],
  sa[0] == 0, st[0] == 1
  }

gof[kas_, k2s_, at_] := 
(
  soltry = st /. NDSolve[eqs[kas, k2s, at], {st, sa}, {t, 0, 100}] // First;
  (soltry /@ data[[All, 1]]) - data[[All, 2]]/data[[1, 2]] // #.# &
)

NMinimize[{gof[1, a, 0.3], 0 < a < 5}, a]
malumno
  • 349
  • 1
  • 11
  • The title of the question is far away from the real problem in the code. The real problem is that in NMinimize[f[x],...], the evaluation of f[x] should (in the present context) be delayed by using the pattern _?NumberQ in the definition of f (f[x_?NumberQ]:=... shall be used instead of f[x_]:=...). This problem is already explain somewhere on stack exchange. – andre 29 mins ago – andre314 Mar 21 '13 at 12:00
  • Possible duplicate of this question – m_goldberg Mar 21 '13 at 13:24

1 Answers1

2
data = {{0.`, 34.84229229652888`}, {3.7448333333333332`, 
    29.004820470134124`}, {7.4896666666666665`, 
    22.762395022844483`}, {11.2345`, 16.82225654209896`}};
Clear[kss, kdm, at, gof];
kss = 0.001; kdm = 0; at = 0.3;
eqs[kas_, k2s_, at_] = {st'[t] == kss - k2s sa[t] - kdm st[t], 
   sa'[t] == kas (at - sa[t]) (st[t] - sa[t]) - k2s sa[t], sa[0] == 0,
    st[0] == 1};
gof[kas_?NumberQ, k2s_?NumberQ, 
   at_?NumberQ] := (soltry = 
    st /. NDSolve[Evaluate@eqs[kas, k2s, at], {st, sa}, {t, 0, 100}] //
      First;
   (soltry /@ data[[All, 1]]) - data[[All, 2]]/data[[1, 2]] // #.# &);
NMinimize[{gof[1, a, 0.3], 0 < a < 5}, a]

gives

{0.0001129348, {a -> 0.2471454}}
Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76