Difference between revisions of "ParametricPlots"

From WeBWorK_wiki
Jump to navigation Jump to search
m
Line 67: Line 67:
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
</p>
 
</p>
  +
  +
[[Category:Problem Techniques]]

Revision as of 22:48, 14 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