Plotting and Dynamical Systems How-to
W. Garrett Mitchener
In[1]:=
Out[1]=
The Graphics package contains a function called :
In[2]:=
In[3]:=
Out[3]=
Unfortunately, Mathematica doesn't include much in the line of nice point-and-click interfaces for drawing solutions to the dynamical system defined by a vector field. Here's how to do it through function calls.
Let's say your dynamical system is:
Let's define a rule table that encodes this system:
In[4]:=
Out[4]=
Note that I've defined rules that replace where the is a pattern. That means we can apply this rule and it will work on , not just . By using a rule table of this form, we can do lots of interesting things. For example, let's say we want to plot the vector field:
In[5]:=
Out[5]=
In[6]:=
Out[6]=
If having expressions like and such in your vector field bothers you, you can also get rid of them with this rule:
In[7]:=
Out[7]=
Yes, you can make the name of the function a pattern to. Just so you can see what that is doing, consider:
In[8]:=
Out[8]=
So I can now give this command, which is pretty much the same as my earlier command but it doesn't look quite so odd, and this time I'll save the result to be used in later pictures:
In[9]:=
Out[9]=
Back to our dynamical system. Now suppose you want to plot solutions on top of the vector field. To do that, we should use Mathematica's built-in numerical solving functions. Here's how to turn our dynamical system rule table into a system of equations that can handle:
In[10]:=
Out[10]=
We also have to add in initial conditions. Those must be added to the ODE, so we use the function:
In[11]:=
Out[11]=
In[12]:=
Out[12]=
Here's how to use :
In[13]:=
Out[13]=
The return value is a list of rule tables that encode numerical solutions. You can use them pretty much like you'd use any regular function. It returns a list of rule tables in case it happens to find multiple solutions, which is rare. Since we usually just want the first solution, it's best to use the command
In[14]:=
Out[14]=
Or you can use the function:
In[15]:=
Out[15]=
In[16]:=
Out[16]=
Note: Notice that I've used double equal signs in the definition of ode, because that represents an equation in Mathematica, as in something that might be true or false, or something to be solved. That's different from the single equal or the colon equal that are used to make definitions. For various reasons, you can use single equal inside an expression, because that makes a definition and returns the value of the definition. That leads to bugs where you use instead of by mistake and can't know that anything is wrong until you get a goofy error message. Here's what can go wrong if you use by mistake:
In[17]:=
Out[17]=
In[18]:=
Out[18]=
And what this has done is to define stuff about and rather than create equations. You can see from the output that the actual command has turned out all wrong. Here's how to view the definitions we incorrectly created:
In[19]:=
Global`u
|
In[20]:=
Global`v
|
Back to our vector field problem. Now suppose you wanted to overlay solutions on the vector field. Since solution curves are defined parametrically, we have to use the function:
In[21]:=
Out[21]=
In[22]:=
Out[22]=
Hmm, let's make it square:
In[23]:=
Out[23]=
Now if you wanted to plot a lot of solution curves, the procedure I just described would get tedious. So, we should automate it. First, let's define a function that creates a numerical solution starting at a given initial condition:
In[24]:=
I used rather than to define because I don't want Mathematica to attempt to evaluate the NDSolve command right now. I want it to wait until I've acutally specified the initial conditions. Here's what happens if you forget and use instead:
In[25]:=
Out[25]=
In[26]:=
Out[26]=
Which is all wrong. So be sure to use to define it.
Now is a good time to introduce the function. It takes a function and a list, and applies the function to each item in the list, returning the result. (The name "map" is traditional for this kind of operation, even though something like "apply to each" would be more descriptive.)
In[27]:=
Out[27]=
In[28]:=
Out[28]=
So this makes a bunch of solution curves:
In[29]:=
Again, I use to tell Mathematica to wait and not evaluate the right hand side right away. Here's how all this works:
In[30]:=
Out[30]=
In[31]:=
Out[31]=
So now I can make a list of rule tables, each representing a different numerical solution. And I can plot them all at once like this:
In[32]:=
Out[32]=
In[33]:=
Out[33]=
Ooh, that was unpleasant. This often happens when you try to do fancy plotting commands. The problem is that plotting functions often "evaluate their arguments in a non-standard way," which means they do confusing things like that. You can usually get it to work by using the Evaluate function to force Mathematica to create the solutions before attempting to plot anything:
In[34]:=
Out[34]=
And you can show the solutions on top of the vector field as follows:
In[35]:=
Out[35]=
Created by Mathematica (February 23, 2005)