From WeBWorK_wiki
Revision as of 20:05, 22 April 2010 by Pearson (talk | contribs)
Jump to navigation Jump to search

Graphing Direction and Vector Fields: PG Code Snippet

This code snippet shows the essential PG code to graph direction and vector fields in a WeBWorK problem. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working. Also note that in the first example we consider the slope or direction field for a differential equation given an explicit equation for the slope. To graph a phase plane where the x'(t) and y'(t) are given for an autonomous system, see the example below this.

You may also be interested in [VectorFields|Vector Fields]

Problem Techniques Index

PG problem file Explanation



We need make no changes to the tagging and description section of the PG file. In the initialization section, we load the file.


$gr = init_graph(-4,-4,4,4,axes=>[0,0],size=>[400,400]);
$dy = sub { my ($x,$y) = @_; return $x + $y; };
$fn = new VectorField( $dy, $gr );

# comment out the following line to get a small
#    dot at the end of each slope tick in the
#    direction/slope field

(Not really related to adding the graph to the problem, for the problem snippet that we're considering here we need to add the variable y to the Context.)

In the problem set-up section of the file, we define a graph object as we would for a dynamically generated graph. Then we define a reference to a subroutine that gives the slope at any (x,y) point. Here this is given by $dy, and we are plotting the slope (direction) field for the differential equation y'=x+y. To add the slope field to the graph, we create a new VectorField object, which also installs the vector field in the graph object's plot queue.

There are a number of options that may be of specific interest when rendering this graph. Here, we've set the size of the dot at the end of the direction field ticks to zero. We might also want to reset the number of direction field ticks shown in the graph (the default is 10 in each of the x and y directions). This can be done with the following sequence of calls:

$fn->rf_arrow_length( sub{
  return(0) if sqrt($dx*$dx + $dy*$dy)==0;
  0.25*1/sqrt($dx*$dx + $dy*$dy);
} );

This doubles the number of direction field ticks that will be shown in each direction, and then redefines the function that is multiplied times (dx, dy) to determine the length of the direction field tick at that point. This is done to avoid overlapping ticks.

Consider the differential equations
\( y' = x + y \),
\( y' = x - y \),
\( y' = -x - y\).
The slope field below is that for one
of these.
Which differential equation matches the
direction field?
\( y' = \) \{ ans_rule(15) \}
\{ image(insertGraph($gr),width=>400,height=>400,tex_size=>500) \}

In the text section of the file, we can then use the graph object as we do for other dynamically generated graphs.

ANS( Compute("x+y")->cmp() );


No additional changes are needed for the answer and solution section of the file.

To graph the phase plane or vector field for a system given the x and y derivatives of an autonomous system, we proceed in a similar manner.

PG problem file Explanation


$gr = init_graph(-4,-4,4,4,axes=>[0,0]);
$dx = sub { my ($x,$y) = @_; return $y; };
$dy = sub { my ($x,$y) = @_; return -1*$x; };
$fn = new VectorField( $dx, $dy, $gr );

# comment out the following line to get a small
#    dot at the end of each slope tick in the
#    direction/slope field

We proceed as before, but instead of specifying the right-hand side of the differential equation y'=f(x,y), we specify the expressions for x' and y'. Here, we plot the phase plane for the system x'=y, y'=-x (an undamped spring).

The available options for the plot are as before.

\{ image(insertGraph($gr),tex_size=>350) \}

And so forth.

More info is available from the pod documentation:

Problem Techniques Index