2
$\begingroup$

I am currently trying to numerically solve a system of differential equations. Here is my code:

NDSolve[{x'[t] == 0.5*y[t] - 0.1*x[t]*y[t],y'[t] == 0.1*x[t]*y[t] + 0.5*y[t] - 0.25*y[t], 
z'[t] == 0.25*y[t],x[0] == 99, y[0] == 1, z[0] == 0}, {x[t], y[t], z[t]}, {t, 0, 1}] 

And here is the error message I am getting back:

NDSolve::deqn: Equation or list of equations expected instead of True in the first argument {True,True,True,x[0]==99,y[0]==1,z[0]==0}. >>

I am relatively new to using Mathematica, so it is most likely some sort of amateur error that I am making. Any help with this is greatly appreciated!

$\endgroup$
  • 1
    $\begingroup$ This actually works on my system. My guess is that you previously evaluated the equation definition statements (like x'[t] == 0.5*y[t] - 0.1*x[t]*y[t]) which told Mathematica that they are identities (hence, True). Try either clearing the variable definitions (Clear[x] etc.) or just restarting the kernel. $\endgroup$ – Virgil Apr 17 '15 at 20:28
  • $\begingroup$ @Virgil I tried using Clear too. To be sure, I put it in a new notebook and obtained the same result as before. $\endgroup$ – Weston Apr 17 '15 at 20:46
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Apr 17 '15 at 20:52
  • $\begingroup$ Runs fine for me to on Mathematica 10.1 under Windows 8.1. Close Mathematica, reopen it, and copy your code only from the question into a new notebook and see what happens. $\endgroup$ – bbgodfrey Apr 17 '15 at 20:54
  • 1
    $\begingroup$ @bbgodfrey Thanks! I just tried that and it is now working! $\endgroup$ – Weston Apr 17 '15 at 20:58
6
$\begingroup$

The comments on your question already give a working solution, but I thought it would be useful to explain how and why it works for the benefit of future readers. I'd also like to show you how you can understand and diagnose these problems for yourself in the future.

Your NDSolve code is actually correct, and runs fine on my system. Based on the comments so far, here is what I think happened: prior to evaluating your call to NDSolve, you must have accidentally evaluated something containing the expressions

x'[t] = 0.5*y[t] - 0.1*x[t]*y[t]
y'[t] = 0.1*x[t]*y[t] + 0.5*y[t] - 0.25*y[t]
z'[t] = 0.25*y[t]

with single = instead of double ==. These two operators have very different meanings.

  • = is the Set operator; an expression of the form x = y tells Mathematica to replace x with y wherever it appears.
  • == is the Equal operator; an expression of the form x == y is a logical assertion that x and y are numerically equal.

Crucially, it is the latter form that you want to use with functions like Solve, DSolve, RSolve, and Reduce. When you evaluated x'[t] = 0.5*y[t] - 0.1*x[t]*y[t], you effectively told Mathematica "whenever you see x'[t], replace it with 0.5*y[t] - 0.1*x[t]*y[t]." This caused problems when you entered your system of equations:

{x'[t] == 0.5*y[t] - 0.1*x[t]*y[t], ...}

Here, Mathematica did as it was told and replaced x'[t] with 0.5*y[t] - 0.1*x[t]*y[t]:

{0.5*y[t] - 0.1*x[t]*y[t] == 0.5*y[t] - 0.1*x[t]*y[t], ...}

and since these two expressions are identical, Mathematica told you that they are indeed equal:

{True, ...}

Similar things happened for your other two equations, causing the system to become

{True, True, True, x[0]==99, y[0]==1, z[0]==0}

which NDSolve obviously didn't know what to do with.

Now here's the tricky part: why didn't Clear[x, y, z] solve the problem? Here, you have to understand that many of Mathematica's syntactic structures are actually shorthands for longer forms. In particular, x'[t] is actually a shorthand for Derivative[1][x][t]. You can check this by asking Mathematica for its FullForm:

FullForm[x'[t]]
(* => Derivative[1][x][t] *)

This means that whenever you evaluate an expression of the form x'[t] = ..., the RHS is not associated with the symbol x, but the symbol Derivative! You can confirm this by asking Mathematica for all the definitions associated with x:

??x (* shorthand for Information[x] *)
(* => Global`x *)

This shows that Mathematica doesn't know anything about x other than that it exists in the Global environment. However, if you evaluate

ClearAttributes[Derivative, ReadProtected];
??Derivative

you should see your definitions for x'[t], y'[t], and z'[t].

With this in mind, solving your problem is simple. All you need to do is remove the values associated with x'[t], y'[t], and z'[t]. One way to do this is with Unset:

x'[t] =.
y'[t] =.
z'[t] =.

Alternatively, if you have no other definitions associated with Derivative you wish to keep, you can simply Clear[Derivative]. (This won't mess up any of Mathematica's differentiation capabilities--it'll still know that Sin' == Cos.)

$\endgroup$

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