Difference between revisions of "ParametricPlots"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Plotting Parametric Functions: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to graph...)
 
m
Line 47: Line 47:
 
<p>
 
<p>
 
Note that this does not set up any default axis labels, so you may need to do that by [[DynamicImages2|adding labels]].
 
Note that this does not set up any default axis labels, so you may need to do that by [[DynamicImages2|adding labels]].
  +
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>

Revision as of 16:18, 1 February 2008

Plotting Parametric Functions: PG Code Snippet

This code snippet shows the essential PG code to graph parametrically defined functions. 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.

Problem Techniques Index

PG problem file Explanation
  loadMacros("PGgraphmacros.pl");

As with any dynamically generated graph, we need to include PGgraphmacros.pl in the loaded macro files in the initialization portion of the problem.

  $gr = init_graph(-1.5,-1.5,1.5,1.5,axes=>[0,0]);

  $xfunc = sub { my $t = shift();
    return cos($t); };
  $yfunc = sub { my $t = shift();
    return sin($t); };
  $fn = new Fun( $xfunc, $yfunc, $gr );
  $fn->domain(0,6.2832);

To set up the parametric graph, we first define a graph object as for any dynamically generated graph. Then, we specify rules to generate the x and y positions of the parametric plot, and use these in a Fun call to define the parametric function. By including the graph object as the third argument of this call we put the parametric plot in the queue to be graphed in the graph object.

Finally, we can set the t domain of the parametrically plotted function with the domain method of the Fun object.

Note that this does not set up any default axis labels, so you may need to do that by adding labels.

  BEGIN_TEXT
  \{ image( insertGraph($gr) ) \}
  END_TEXT

The graph is then inserted into the problem in the usual manner.

Problem Techniques Index