Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I've been trying to solve a system of nonlinear differential equation, but the conditions are a bit weird.

Two of the differentials equate to the same equation, but have different boundary conditions. The third differential equate to something else.

For the background on the problem I am trying to solve, it is related to concentration as a function of time for a reaction.

Here is the code I have put, which was more or less straightforward, but I keep getting 'true.' I've tried using DSolve and NDsolve, but I am unable to get a graph for my three concentration profiles (I do not need an analytical solution, just a graphical one).

NDSolve[{
A'[t] == -kf A[t] *B[t] + kb * c[t], 
B'[t] == -kf A[t] *B[t] + kb * c[t], 
c'[t] == kb * c[t] + kf A[t] B[t], 
A[0] == 1, B[0] == 2, c[0] == 0}, 
{A, B, c}, {t, 100}] 

kf and kb are constants I've already equated into my code

But I keep resulting with:

NDSolve[{True, True, True, A[0] == 1, B[0] == 2, c[0] == 0}, {A, B, c}, {t, 100}]

I'm thinking it's because I'm multiplying two functions together, I haven't solved ODEs this way. I'd appreciate any input, or hints/guides/direction. Thanks!

If there is something unclear, or if I am lacking proper question asking etiquette please let me know!

share|improve this question
    
Then you must have once mixed up = and ==. You need Clear[Derivative]. Values stored in Derivative can't be cleared by Clear["Global`*"] because it actually means clearing all the variables under context "Global`" while Context[Derivative] gives "System`". –  xzczd Apr 16 at 7:15
    
@xzczd New users might fall in to this Context trap. I haven't found another post to mark this as a duplicate. Perhaps your comment is worthy of an answer just so it is in the books? –  bobthechemist Apr 16 at 13:12
    
@bobthechemist OK, let me elaborate it into an answer. –  xzczd Apr 16 at 13:42

1 Answer 1

Short Answer

Clear[Derivative] first.

Long Answer

OK, it's surprising that there seems to be no regular answer to this common problem for beginners, let me elaborate my comment into an answer. If you restart your Mathematica and run your code again then you'll find your problem no longer exists anymore! Then, why? Because Mathematica is unstable?

Of course not. Just recall what you've done before meeting the error, you may find the following scene in the corner of memory:

enter image description here

The value of kf and kb is chosen at random.

It's another common mistake of Mathematica newbies, that is, mix up = and ==.

Your code was in a mess now, but you didn't feel worried because you've already learned from some material that you can use

Clear["Global`*"]

to fix this. You cheerfully placed this line at the beginning of your code and ran it again, only to find:

enter image description here

What happened? Why does the magic of Clear["Global`*"] lose its power?

To answer this I'd like to first explain why you need to clear the variables after you mistyped == to = since I presumingly guess you may still be vague about this. Try the following code:

A[0] = 0
A[0]
A[0] == 0
Clear[A]
A[0] == 0
0
0
True
A[0] == 0

BTW, though I've execute A[0] for several times to show the variation, you can judge whether A owns a value just by observing its color: it's black when it does, otherwise it'll be blue.

As you see, Set (=) will give value to its left hand side. (Of course there exist cases that the left hand side is Protected, for example a + b = 4 won't give the left side a value, but it's another story and I'd like not to talk about it here. ) If you don't Clear it, it'll be always there and break your equations. When introducing this issue, many materials will tell readers that you can simply use Clear["Global`*"] to "clear all the variables" at once, but it doesn't work for your case, yeah, you already know that, but do you know what's the exact meaning of Clear["Global`*"]?

If you check "Details" of document of Clear, you'll find the following description:

Clear["context`*"] clears all symbols in a particular context.

What's "context"? "context" is something that every symbol in Mathematica owns. You can check it by function Context, for example:

Context[a]
Context[NDSolve]
"Global`"
"System`"

So Clear["Global`*"] is clearing the values of symbols under context Global`, which is the stronghold for most symbols that'll have values (at least for beginners), but, what you can Clear is only "most", not "all". Try the following code:

A'[0] = 0
A'[0]
Clear["Global`*"]
A'[0]
Clear[A]
A'[0]
HoldForm[FullForm[A'[0]]]
0
0
0
0
Derivative[1][A][0]    

Aha, A'[0] is one of the exceptions, the value of A'[0] isn't stored in A, in fact it's stored as SubValues (this is another story that I'd like to omit here, you can search it in this site or have a look at Leonid Shifrin's excellent book ) of Derivative, then what's the context of Derivative?:

Context[Derivative]
"System`"

So values in it can't be cleared by Clear["Global`*] (BTW 2, for most cases it can be shortened as Clear["`*"], meaning clear all the values of symbols under the current context, which is usually Global`), what you need to clear it is

Clear[Derivative]

Though some warnings generated, Clear["System`*"] can be used too if you like.

BTW 3, another symbol that is likely to trig this problem is Subscript.

share|improve this answer
    
@xzcxd: I must say this an excellent answer, one of the most helpful I have read. –  David 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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