next up previous
Next: About this document Previous: Maple and differential equations

Plotting direction fields and solution curves


In this section we describe Maple commands for plotting direction fields and/or solution curves for a single first order differential equation of the form


or a two-dimensional autonomous system of the form


or a two-dimensional non-automonous system of the form


There are four Maple commands we will describe here. All are in the DEtools package. We deal here with only the most basic examples for these commands. To learn about more advanced features, consult the help pages for the individual commands. Here is a list of the four commands we will consider, along with brief descriptions. More complete descriptions and examples follow.

This command will plot the direction field for either a single differential equation or a two-dimensional autonomous system.
Plots the direction field for a single differential equation. Can also be given an list of initial conditions for which to plot solution curves.
Plots the direction field for a two-dimensional autonomous system. Can also be given a list of initial conditions for which to plot solution curves. Will also plot the direction field and solution trajectories for a two-dimensional nonautonomous system.
Plots solution trajectories for a set of initial conditions. A useful command. Can also include the direction field or use the scene parameter to plot the individual components versus t.

These four commands do similar things. In fact, DEplot1 and DEplot2 are the fundamental commands and dfieldplot and phaseportrait are convenience functions with simplified syntax that call the lower level routines DEplot1 and DEplot2. We will usually use the higher level routines, so they will be described before DEplot1 and DEplot2. There is also a DEplot command in the DEtools package. It is similar to the DEplot1 and DEplot2 commands, but will work with systems of differential equations of dimension 3 or higher. We won't need it, so it won't be described here.


The examples in the rest of this handout will deal with the first order differential equation


the two dimensional autonomous system


and the two dimensional non-automonous system


Plotting direction fields: dfieldplot

The dfieldplot routine plots direction fields for a single first order differential equation or a two-dimensional system of differential equations, but cannot be used with a two-dimensional non-autonomous system. The phaseportrait command, which is described in the next section, can be used to plot solution trajectories for all of the three classes of differential equations being considered here.

The DEtools package only appeared with Release 3 of Maple. Before that, it was very difficult to plot solution trajectories for nonlinear differential equations. While I'm not exactly thrilled with the DEtools package, it was a big improvement on what was available in Release 2.

Direction fields for a single differential equation

An example of using dfieldplot with a single first order equation appears below. Note that the first command loads the DEtools package. It is only necessary to do this once in each session.

  > with(DEtools);


  > de1 := sin(x^2);


  > dfieldplot(de1,[t,x],t=0..5,x=-2..2);

The first argument to dfieldplot is always the differential equation (or system of differential equations, as we'll see below). The second argument lists the independent variable t and the dependent variable x. This argument must be present, and t must appear first. The third argument specifies a range for the independent variable, and must be present. The fourth argument, specifying a range for the dependent variable, is optional but usually needed so that the command plots the x range you are interested in. There are several other optional arguments, see the help page for more details.

Note that we used vector field notation to specify the differential equation. That is, all we did was specify the function f(x,t) from the right-hand side of equation 1. The help page implies that you can also use a differential equation as the first argument,as in the following example.

  > dfieldplot(diff(x(t),t)=sin(x(t)^2),[t,x],t=0..5,x=-2..2);

However, this has the following consequences.

  1. On a DEC Alpha machine, this command crashes the session immediately.
  2. On other platforms I have tried, it doesn't crash Maple, but it slows the command down tremendously.
My recommendation is to always use the vector field notation with Maple V Release 3. If you have Release 4, however, then the behavior is different and you must use differential equation notation.

Direction fields for two-dimensional autonomous systems

Next, we present an example of dfieldplot usage for a two-dimensional autonomous system. The main difference from our first example is that two differential equations must be specified. Again, vector field notation should be used.

  > sys1 := [y,-4*x];


  > dfieldplot(sys1,[x,y],t=0..1,x=-2..2,y=-2..2);

As in our previous example, only the first three arguments are required. However, I can't really think of a reason for not specifying ranges for x and y.

Optional arguments to dfieldplot

There are several optional arguments to dfieldplot. One of the most useful is the arrows option, which can be used to change the appearance of the arrows. Here is one example.

  > dfieldplot(sys1,[x,y],0..1,x=-2..2,y=-2..2,arrows=SLIM);

Other possible values for the arrows option are THICK (big arrows), LINE (no arrowheads), SLIM (like thin, but with better arrowheads), and NONE (no direction field - don't use this option with dfieldplot).

Plotting solution curves: phaseportrait

The phaseportrait command is even more useful. What it does, essentially, is to plot solution trajectories for a set of initial conditions that you specify. Optionally, phaseportrait can plot the trajectories and the direction field for a single differential equation or a two-dimensional system of autonomous differential equations.

By default, the phaseportrait command plots the solution of an autonomous system as a parametric curve in the xy plane. However, it is possible to plot individual components by using the optional scene parameter. In the case of an non-autonomous system, the solution curve is plotted in three dimensions by default, but the scene parameter can also be used to select any two variables for plotting. Examples are given below.

Single, first order differential equations

Using the phaseportrait command to plot trajectories for a single first order differential equation is straightforward. See the following example.

  > phaseportrait(de1,[t,x],t=0..2,{[0,1],[0,2]});

This command will plot solutions of the differential equation for the two initial conditions x(0)=1 and x(0)=2 over the range tex2html_wrap_inline910 . The syntax is similar to that of dfieldplot, but the fourth argument, which provides initial conditions, is required. Each of the two lists in the fourth argument provides a value of t and a value of x to be used as initial conditions. The t value must come first. The arrows option, as described previously, can be used to plot the direction field along with the trajectories.

Two-dimensional autonomous systems

Here is an example using phaseportrait to plot two trajectories for our two dimensional system, equation 5.

  > phaseportrait(sys1,[x,y],t=0..1,{[0,0,1],[0,1,0]},x=-2..2,y=-2..2);

The first four arguments to phaseportrait are required. First comes the system of differential equations. The second argument is the list of dependent variables. The third argument specifies a range for t. In the example, we specified the range t=0 to t=1, but you might have to use a larger or smaller range. The fourth argument is a set of lists. Each list in the set represents an initial condition, giving the values of t, x and y. For example the list [0,0,1] represents the initial condition x(0) = 0 and y(0)=1. The first item in the list is the value of t. More initial conditions could have been specified by including more lists in the set.

The phaseportrait command can also plot direction fields if you specify the arrows option. Here is an example.

  > phaseportrait(sys1,[x,y],t=0..1,{[0,0,1],[0,1,0],[0,1/2,-1/2]},

The arrows argument can only have the values given above for the dfieldplot command. The value NONE is the default if you don't use this optional argument.

In the previous examples we have used a relatively short time interval. Many times you need to use a longer time interval. This can result in a very jagged plot, like from the following command.

  > phaseportrait(sys1,[x,y],t=0..5,{[0,0,1],[0,1,0]},x=-2..2,y=-2..2);

The reason for the jagged appearance is that the numerical integration is being done with a stepsize that is too large. The fix is to set the step size with the stepsize command as in the following command.

  > phaseportrait(sys1,[x,y],t=0..5,{[0,0,1],[0,1,0]},

If the plot is still jagged after you decrease the step size, try decreasing it again. However, the smaller the step size, the longer the integration takes, so be cautious in reducing the step size.

Finally, the phaseportrait command can be used to plot individual components versus t by using the scene optional argument. Here is an example of plotting x versus t for our two-dimensional autonomous system.

  > phaseportrait(sys1,[x,y],t=0..1,{[0,0,1],[0,1,0]},scene=[t,x]);

You might wonder why you would want to do this. One common reason to plot a single component is to determine the approximate period when a solution is periodic.

Two-dimensional non-autonomous systems

To use the phaseportrait command with our non-autonomous example, equation 6, we first define the vector field with the following command.

  > sys2 := [y,-y-4*x+sin(t)];


Then we can plot a trajectory in the three dimensional txy space with the command

  > phaseportrait(sys2,[t,x,y],t=0..30,{[0,1,1]},stepsize=0.1);

This plot can be manipulated with the mouse to view it from various directions. However, it is often more useful to plot various projections of the solution curve by using the scene parameter. The two examples below plot x(t) versus t

  > phaseportrait(sys2,[t,x,y],t=0..30,{[0,1,1]},scene=[t,x],stepsize=0.1);

and the parametric curve (x(t),y(t))

  > phaseportrait(sys2,[t,x,y],t=0..30,{[0,1,1]},scene=[x,y],stepsize=0.1);

Lower level routines: DEplot1 and DEplot2

These two commands do the same things as dfieldplot and phaseportrait, but are more general. In fact, dfieldplot and phaseportrait just call DEplot1 or DEplot2 with appropriate arguments. However, they are more specific in the following sense: DEplot1 is only for a single first order differential equation and DEplot2 is only for a system of two first order differential equations. We will consider only autonomous systems here. For further information, look at the help pages. Optional arguments that we have described earlier, like arrows, stepsize, and scene can also be used with these commands.

The DEplot1 command has two forms. The first is used to plot the direction field and a set of trajectories and the second form plots only the direction field. However, it is also possible to plot solution trajectories without the direction field. We give examples below for each behavior for our model, equation (4).

To plot a set of trajectories as well as the direction field, use a command like the following.

  > DEplot1(de1,[t,x],t=0..2,{[0,1/2],[0,-1/2],[0,2]});

You can also set the range for the dependent variable, but it isn't required. Instead, Maple computes the trajectories and then sets an appropriate range for x.

To plot only the direction field, you omit the initial conditions and set a range for the dependent variable, like the following.

  > DEplot1(de1,[t,x],t=0..2,x=-2..2);

To plot only trajectories without the direction field, use the optional argument arrows=NONE. The default value of the arrows parameter is THIN, but you can set it to one of the other values given above for the phaseportrait command. Here is an example using the arrows=NONE option.

  > DEplot1(de1,[t,x],t=0..2,{[0,1/2],[0,-1/2],[0,2]},arrows=NONE);

The syntax for the DEplot2 command is very similar to that for DEplot1. Again there are two forms, one for plotting trajectories and the direction field and one for plotting only the direction field. Just as for DEplot1, it is possible to just plot trajectories.

To plot a set of trajectories as well as the direction field, use a command like the following.

  > DEplot2(sys1,[x,y],t=0..1,{[0,0,1],[0,1,0]});

If you don't like the ranges that Maple chooses for the dependent variables, you can specify them yourself.

To plot just the direction field, leave out the initial conditions and specify ranges for the two independent variables.

  > DEplot2(sys1,[x,y],t=0..1,x=-2..2,y=-2..2);

To omit the direction field and just plot the trajectories, use the optional argument arrows=NONE. This speeds up the plotting tremendously.

  > DEplot2(sys1,[x,y],t=0..1,{[0,0,1],[0,1,0]},arrows=NONE);

next up previous
Next: About this document Previous: Maple and differential equations

William W. Farr
Mon Apr 28 12:04:52 EDT 1997