ContourPlots

From WeBWorK_wiki
Revision as of 16:19, 14 May 2010 by Aubreyja (talk | contribs) (1 revision: import all default namespace pages from old wiki)
Jump to navigation Jump to search

Contour Plots


This PG code shows how to construct contour plots.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

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

TEXT(beginproblem());

$refreshCachedImages=1;

Initialization: Use unionTables.pl to display the graphs nicely.

Context("Numeric")->variables->are(x=>"Real",y=>"Real");

#
#  Create some graph canvases
#
foreach my $i (0..1) {
  $gr[$i] = init_graph(-5,-5,5,5,grid=>[10,10],axes=>[0,0],pixels=>[400,400]);
  $gr[$i]->lb('reset');
  $gr[$i]->lb( new Label(4.7,0.2,'x','black','center','middle'));
  $gr[$i]->lb( new Label(0.2,4.7,'y','black','center','middle'));
  $gr[$i]->new_color("nicegreen",0,110,0); # verdant green
  $gr[$i]->new_color("darkred", 159, 64, 16); # red-brown
}

#
#  Circular contours as parametrized curves
#
foreach $k (5,10,15,20,25,30,35,40,45) {
  my $a = sqrt($k);
  $fn = new Fun( 
     sub { my $t=shift(); return $a * cos($t); }, 
     sub { my $t=shift(); return $a * sin($t);}, 
     $gr[0] 
  );
  $fn->domain(0,6.3);
  $fn->color("nicegreen");
}

#
#  Hyperbolas as parametrized curves
#
foreach my $k (0,5,10,15,20,25) {
  # left and right branches
  $fn = new Fun( 
     sub { my $t=shift(); return sqrt($k+$t**2); }, 
     sub { my $t=shift(); return $t;}, 
     $gr[1] 
  );
  $fn->domain(-5,5);
  $fn->color("darkred");
  $fn = new Fun( 
     sub { my $t=shift(); return -sqrt($k+$t**2); }, 
     sub { my $t=shift(); return $t;}, 
     $gr[1] 
  );
  $fn->domain(-5,5);
  $fn->color("darkred");

  # top and bottom branches
  add_functions($gr[1],
  " sqrt(x^2+$k)  for x in <-5,5> using color:darkred and weight:2",
  "-sqrt(x^2+$k)  for x in <-5,5> using color:darkred and weight:2",
  );
}

foreach my $i (0..1) {
  $fig[$i] = image(insertGraph($gr[$i]),width=>300,height=>300,tex_size=>450);
}

Setup: We create the contour plots as a parametrized family of (parametric) curves.

BEGIN_TEXT
$BCENTER
\{
BeginTable().
AlignedRow([$fig[0],$fig[1]]).
TableSpace(5,0).
AlignedRow(["A","B"]).
EndTable();
\}
$BR
(Click on a graph to enlarge it.)
$ECENTER
END_TEXT

Main Text: We display the contour plots nicely using tables provided by unionTables.pl.

$showPartialCorrectAnswers = 1;

ENDDOCUMENT();

Answer Evaluation: We did not ask any questions, so there's nothing interesting here.

Problem Techniques Index