Difference between revisions of "SlopeFields"

From WeBWorK_wiki
Jump to navigation Jump to search
(added historical tag and gave updated problem link)
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<h2>Graphing Direction and Vector Fields: PG Code Snippet</h2>
 
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/VectorCalc/DirectionField.html a newer version of this problem]</p>
  +
<h2>Graphing Slope Fields</h2>
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>This code snippet shows the essential PG code to graph direction and vector fields in a WeBWorK problem. Note that these are <b>insertions</b>, 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.</em>
+
<em>This code snippet shows the essential PG code to graph direction and vector fields in a WeBWorK problem. Note that these are <b>insertions</b>, 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.
  +
<br />
  +
<br />
  +
You may also be interested in [[VectorFields2D|Vector Fields]]
  +
</em>
 
</p>
 
</p>
 
<p style="text-align:center;">
 
<p style="text-align:center;">
Line 16: Line 19:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("PGgraphmacros.pl");
 
  +
DOCUMENT();
  +
  +
loadMacros(
  +
"PGstandard.pl",
  +
"MathObjects.pl",
  +
"PGgraphmacros.pl"
  +
);
  +
  +
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 28: Line 39:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context()-&gt;variables-&gt;add(y=&gt;"Real");
+
Context()-&gt;variables-&gt;add(y=&gt;"Real");
   
$gr = init_graph(-4,-4,4,4,axes=&gt;[0,0]);
+
$gr = init_graph(-4,-4,4,4,axes=&gt;[0,0],size=&gt;[400,400]);
$dy = sub { my ($x,$y) = @_; return $x + $y; };
+
$dy = sub { my ($x,$y) = @_; return $x + $y; };
$fn = new VectorField( $dy, $gr );
+
$fn = new VectorField( $dy, $gr );
   
# comment out the following line to get a small
+
# comment out the following line to get a small
# dot at the end of each slope tick in the
+
# dot at the end of each slope tick in the
# direction/slope field
+
# direction/slope field
$fn-&gt;dot_radius(0);
+
$fn-&gt;dot_radius(1);
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 51: Line 62:
 
</p>
 
</p>
 
<pre>
 
<pre>
$fn-&gt;x_steps(20);
+
$fn-&gt;x_steps(20);
$fn-&gt;y_steps(20);
+
$fn-&gt;y_steps(20);
$fn-&gt;rf_arrow_length( sub{
+
$fn-&gt;rf_arrow_length( sub{
my($dx,$dy)=@_;
+
my($dx,$dy)=@_;
return(0) if sqrt($dx*$dx + $dy*$dy)==0;
+
return(0) if sqrt($dx*$dx + $dy*$dy)==0;
0.25*1/sqrt($dx*$dx + $dy*$dy);
+
0.25*1/sqrt($dx*$dx + $dy*$dy);
} );
+
} );
 
</pre>
 
</pre>
 
<p>
 
<p>
Line 67: Line 78:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
BEGIN_TEXT
+
BEGIN_TEXT
Consider the differential equations
+
Consider the differential equations
\( y' = x + y \),
+
\( y' = x + y \),
\( y' = x - y \),
+
\( y' = x - y \),
\( y' = -x - y\).
+
\( y' = -x - y\).
The slope field below is that for one
+
The slope field below is that for one
of these.
+
of these.
$PAR
+
Which differential equation matches the
$BCENTER
+
direction field?
\{ image(insertGraph($gr),tex_size=>350) \}
+
$BR
$ECENTER
+
\( y' = \) \{ ans_rule(15) \}
$PAR
+
$PAR
Which differential equation matches the
+
$BCENTER
direction field?
+
\{ image(insertGraph($gr),width=&gt;400,height=&gt;400,tex_size=>500) \}
$BR
+
$ECENTER
\( y' = \) \{ ans_rule(15) \}
+
END_TEXT
END_TEXT
 
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
Line 93: Line 104:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( Compute("x+y")->cmp() );
+
ANS( Compute("x+y")->cmp() );
  +
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
Line 115: Line 126:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("PGgraphmacros.pl");
+
loadMacros("PGgraphmacros.pl");
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 127: Line 138:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
$gr = init_graph(-4,-4,4,4,axes=&gt;[0,0]);
+
$gr = init_graph(-4,-4,4,4,axes=&gt;[0,0]);
$dx = sub { my ($x,$y) = @_; return $y; };
+
$dx = sub { my ($x,$y) = @_; return $y; };
$dy = sub { my ($x,$y) = @_; return -1*$x; };
+
$dy = sub { my ($x,$y) = @_; return -1*$x; };
$fn = new VectorField( $dx, $dy, $gr );
+
$fn = new VectorField( $dx, $dy, $gr );
   
# comment out the following line to get a small
+
# comment out the following line to get a small
# dot at the end of each slope tick in the
+
# dot at the end of each slope tick in the
# direction/slope field
+
# direction/slope field
$fn-&gt;dot_radius(0);
+
$fn-&gt;dot_radius(0);
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 150: Line 161:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
BEGIN_TEXT
+
BEGIN_TEXT
$BCENTER
+
$BCENTER
\{ image(insertGraph($gr),tex_size=>350) \}
+
\{ image(insertGraph($gr),tex_size=>350) \}
$ECENTER
+
$ECENTER
END_TEXT
+
END_TEXT
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
Line 162: Line 173:
 
</td>
 
</td>
 
</tr>
 
</tr>
<tr><td colspan=2>
 
More info is available from the pod documentation: http://devel.webwork.rochester.edu/doc/cvs/pg_HEAD/lib/VectorField.html
 
</td></tr>
 
 
</table>
 
</table>
   
Line 172: Line 180:
   
 
[[Category:Problem Techniques]]
 
[[Category:Problem Techniques]]
  +
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/pod/pg/lib/VectorField.html VectorField.html]</li>
  +
<li>PG macro: http://webwork.maa.org/viewvc/system/trunk/pg/lib/VectorField.pm?view=log</li>
  +
</ul>

Latest revision as of 11:29, 28 June 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Graphing Slope Fields

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 Vector Fields

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGgraphmacros.pl"
);

TEXT(beginproblem());

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

Context()->variables->add(y=>"Real");

$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
$fn->dot_radius(1);

(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->x_steps(20);
$fn->y_steps(20);
$fn->rf_arrow_length( sub{
  my($dx,$dy)=@_;
  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.

BEGIN_TEXT
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?
$BR
\( y' = \) \{ ans_rule(15) \}
$PAR
$BCENTER
\{ image(insertGraph($gr),width=>400,height=>400,tex_size=>500) \}
$ECENTER
END_TEXT

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() );

ENDDOCUMENT();

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
loadMacros("PGgraphmacros.pl");

Load PGgraphmacros.pl.

$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
$fn->dot_radius(0);

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.

BEGIN_TEXT
$BCENTER
\{ image(insertGraph($gr),tex_size=>350) \}
$ECENTER
END_TEXT

And so forth.

Problem Techniques Index