Difference between revisions of "VectorFields"
Jump to navigation
Jump to search
m (1 revision: import all default namespace pages from old wiki) |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | <h2>Graphing Direction and Vector Fields: PG Code Snippet</h2> |
||
+ | #REDIRECT [[SlopeFields]] |
||
− | |||
− | <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> |
||
− | </p> |
||
− | <p style="text-align:center;"> |
||
− | [[IndexOfProblemTechniques|Problem Techniques Index]] |
||
− | </p> |
||
− | |||
− | <table cellspacing="0" cellpadding="2" border="0"> |
||
− | <tr valign="top"> |
||
− | <th> PG problem file </th> |
||
− | <th> Explanation </th> |
||
− | </tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#ddffdd;border:black 1px dashed;"> |
||
− | <pre> |
||
− | DOCUMENT(); |
||
− | |||
− | loadMacros( |
||
− | "PGstandard.pl", |
||
− | "MathObjects.pl", |
||
− | "PGgraphmacros.pl" |
||
− | ); |
||
− | |||
− | TEXT(beginproblem()); |
||
− | </pre> |
||
− | </td> |
||
− | <td style="background-color:#ccffcc;padding:7px;"> |
||
− | <p> |
||
− | We need make no changes to the tagging and description section of the PG file. In the initialization section, we load the <code>PGgraphmacros.pl</code> file. |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#ffffdd;border:black 1px dashed;"> |
||
− | <pre> |
||
− | 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); |
||
− | </pre> |
||
− | </td> |
||
− | <td style="background-color:#ffffcc;padding:7px;"> |
||
− | <p> |
||
− | (Not really related to adding the graph to the problem, for the problem snippet that we're considering here we need to [[VariablesOtherThanX|add the variable y]] to the Context.) |
||
− | </p> |
||
− | <p> |
||
− | In the problem set-up section of the file, we define a graph object as we would for a [[DynamicImages|dynamically generated graph]]. Then we define a reference to a subroutine that gives the slope at any (<i>x</i>,<i>y</i>) point. Here this is given by <code>$dy</code>, and we are plotting the slope (direction) field for the differential equation <i>y'</i>=<i>x</i>+<i>y</i>. 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. |
||
− | </p> |
||
− | <p> |
||
− | 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: |
||
− | </p> |
||
− | <pre> |
||
− | $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); |
||
− | } ); |
||
− | </pre> |
||
− | <p> |
||
− | 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. |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#ffdddd;border:black 1px dashed;"> |
||
− | <pre> |
||
− | 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. |
||
− | $PAR |
||
− | $BCENTER |
||
− | \{ image(insertGraph($gr),width=>400,height=>400,tex_size=>500) \} |
||
− | $ECENTER |
||
− | $PAR |
||
− | Which differential equation matches the |
||
− | direction field? |
||
− | $BR |
||
− | \( y' = \) \{ ans_rule(15) \} |
||
− | END_TEXT |
||
− | </pre> |
||
− | <td style="background-color:#ffcccc;padding:7px;"> |
||
− | <p> |
||
− | In the text section of the file, we can then use the graph object as we do for [[DynamicImages|other dynamically generated graphs]]. |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#eeddff;border:black 1px dashed;"> |
||
− | <pre> |
||
− | ANS( Compute("x+y")->cmp() ); |
||
− | |||
− | ENDDOCUMENT(); |
||
− | </pre> |
||
− | <td style="background-color:#eeccff;padding:7px;"> |
||
− | <p> |
||
− | No additional changes are needed for the answer and solution section of the file. |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
− | </table> |
||
− | |||
− | <p> |
||
− | To graph the phase plane or vector field for a system given the <i>x</i> and <i>y</i> derivatives of an autonomous system, we proceed in a similar manner. |
||
− | </p> |
||
− | |||
− | <table cellspacing="0" cellpadding="2" border="0"> |
||
− | <tr valign="top"> |
||
− | <th> PG problem file </th> |
||
− | <th> Explanation </th> |
||
− | </tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#ddffdd;border:black 1px dashed;"> |
||
− | <pre> |
||
− | loadMacros("PGgraphmacros.pl"); |
||
− | </pre> |
||
− | </td> |
||
− | <td style="background-color:#ccffcc;padding:7px;"> |
||
− | <p> |
||
− | Load <code>PGgraphmacros.pl</code>. |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#ffffdd;border:black 1px dashed;"> |
||
− | <pre> |
||
− | $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); |
||
− | </pre> |
||
− | </td> |
||
− | <td style="background-color:#ffffcc;padding:7px;"> |
||
− | <p> |
||
− | We proceed as before, but instead of specifying the right-hand side of the differential equation <i>y'</i>=<i>f</i>(<i>x</i>,<i>y</i>), we specify the expressions for <i>x'</i> and <i>y'</i>. Here, we plot the phase plane for the system <i>x'</i>=<i>y</i>, <i>y'</i>=<i>-x</i> (an undamped spring). |
||
− | </p> |
||
− | <p> |
||
− | The available options for the plot are as before. |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#ffdddd;border:black 1px dashed;"> |
||
− | <pre> |
||
− | BEGIN_TEXT |
||
− | $BCENTER |
||
− | \{ image(insertGraph($gr),tex_size=>350) \} |
||
− | $ECENTER |
||
− | END_TEXT |
||
− | </pre> |
||
− | <td style="background-color:#ffcccc;padding:7px;"> |
||
− | <p> |
||
− | And so forth. |
||
− | </p> |
||
− | </td> |
||
− | </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> |
||
− | |||
− | <p style="text-align:center;"> |
||
− | [[IndexOfProblemTechniques|Problem Techniques Index]] |
||
− | </p> |
||
− | |||
− | [[Category:Problem Techniques]] |
Latest revision as of 15:19, 14 May 2010
Redirect to: