4
$\begingroup$

I am trying to solve a chemical equilibrium ODE with NDSolve where one function is the argument to another.

I.E. My equations look like:

A'[t] == B[t] + f[A[t]] C[t],
B'[t] == ...,
C'[t] == ...

This is the actual code:

sol = NDSolve[{
  T'[t] == -((uCO[T[t]] nCO'[t] + uO[T[t]] nO'[t] + uO2[T[t]] nO2'[t] + uCO2[T[t]] nCO2'[t])/(CvCO[T[t]] nCO[t] + CvO[T[t]] nO[t] + CvO2[T[t]] nO2[t] + CvCO2[T[t]] nCO2[t])),
  nCO'[t] == -nCO[t] nO[t] m[t] r1[T[t]] - nCO[t] nO2[t] r2[T[t]] + nCO2[t] m[t] r1b[T[t]] + nCO2[t] nO[t] r2b[T[t]],
  nO'[t] == -nCO[t] nO[t] m[t] r1[T[t]] + nCO[t] nO2[t] r2[T[t]] - 2 nO[t] nO[t] m[t] r3[T[t]] + nCO2[t] m[t] r1b[T[t]] - nCO2[t] nO[t] r2b[T[t]] + 2 nO2[t] m[t] r3b[T[t]],
  nO2'[t] == -nCO[t] nO2[t] r2[T[t]] + nO[t] nO[t] m[t] r3[T[t]] + nCO2[t] nO[t] r2b[T[t]] - nO2[t] m[t] r3b[T[t]],
  nCO2'[t] == nCO[t] nO[t] m[t] r1[T[t]] + nCO[t] nO2[t] r2[T[t]] - nCO2[t] m[t] r1b[T[t]] - nCO2[t] nO[t] r2b[T[t]],
  m'[t] == nCO'[t] + nO'[t] + nO2'[t] + nCO2'[t],
  T[0] == 1500,
  nCO[0] == nCOinit,
  nO[0] == 0.,
  nO2[0] == nO2init,
  nCO2[0] == 0.,
  m[0] == nCOinit + nO2init
  },
 {T, nCO, nO, nO2, nCO2, m},
 {t, 0.00, 1.00},
 MaxSteps -> 300000
 ]

The problem is that my functions:

CvCO2, CvCO, CvO, CvO2, uCO2, uCO, uO2, uO, r1, r2, r3, r1b, r2b, r3b

can only take a numeric value argument.

Mma spits our many errors due to that...

Any idea how to solve this problem?

$\endgroup$

1 Answer 1

8
$\begingroup$

This is a very frequently asked question, but I agree it might not be so easy to find out what is happening. The solution is defining the functions as

f[x_ ? NumericQ] := ...

to prevent them from evaluating for non-numerical arguments

$\endgroup$
5
  • $\begingroup$ Thanks Szabolcs, but I have declared my functions like that yes and it doesn't help. In fact, I haven't notice any changes. $\endgroup$ Commented Mar 9, 2012 at 19:19
  • $\begingroup$ @JonathanRoy You need to clear them first, otherwise both definitions persist (check it using ?f). So evaluate Clear[f]. But since you have so many, just start with a fresh kernel (Evaluation -> Quit Kernel) to test this. $\endgroup$
    – Szabolcs
    Commented Mar 9, 2012 at 19:22
  • $\begingroup$ For some reason, I had to close Mma and re-open my notebook and now it works! Is there any way to 'reset' the notebook without having to restart Mma? $\endgroup$ Commented Mar 9, 2012 at 19:26
  • $\begingroup$ Haha you answered my question while I was commenting. Thanks so much!! $\endgroup$ Commented Mar 9, 2012 at 19:27
  • 6
    $\begingroup$ @Jonathan Whenever I have a function that has a not-totally-general pattern, such as f[x_?NumericQ], I precede the definition with Clear[f]. This way I make sure that when I change the definition (and possibly change the patter as well), I won't end up with several coexisting definitions that I don't want. It's useful to make this a habit. $\endgroup$
    – Szabolcs
    Commented Mar 9, 2012 at 19:36

Your Answer

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

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