1
$\begingroup$

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]
$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – andre314
    Mar 21, 2013 at 12:00
  • $\begingroup$ Possible duplicate of this question $\endgroup$
    – m_goldberg
    Mar 21, 2013 at 13:24

1 Answer 1

2
$\begingroup$
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}}
$\endgroup$
1
  • 2
    $\begingroup$ The key point is ?NumberQ in k2s_?NumberQ. $\endgroup$
    – andre314
    Mar 21, 2013 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.