Difference between revisions of "GraphTool"

From WeBWorK_wiki
Jump to navigation Jump to search
(initial checkin of this page. Not ready)
 
(Correct typo)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{UnderConstruction}}
 
 
 
<h2>Graph Tool</h2>
 
<h2>Graph Tool</h2>
   
Line 48: Line 46:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
$h = non_zero_random(-5, 5);
 
  +
## this is the answer checker for the graph tool
$k = non_zero_random(-5, 5);
 
$r = random(1, 4);
 
   
Context()->variables->add("y" => "Real");
 
  +
# This grader allows the student to graph the correct circle multiple
$circle_eq_lhs = Formula("(x-$h)^2 + (y-$k)^2")->reduce;
 
  +
# times. The idea is that the graph is graded based on appearance.
  +
# No matter how many times the student graphs the correct circle,
  +
# the resulting graph appears the same.
   
# This grader allows the student to graph the correct circle multiple times. The idea is that
 
  +
$gt_checker = sub {
# the graph is graded based on appearance. No matter how many times the student graphs the
 
  +
my ($correct, $student, $ans, $value) = @_;
# correct circle, the resulting graph appears the same.
 
  +
return 0 if $ans->{isPreview};
$gt = GraphTool("{circle, solid, ($h, $k), ($h + $r, $k)}")->with(
 
bBox => [-11, 11, 11, -11],
 
cmpOptions => {
 
list_checker => sub
 
{
 
my ($correct, $student, $ans, $value) = @_;
 
return 0 if $ans->{isPreview};
 
   
my $score = 0;
+
my $score = 0;
my @errors;
+
my @errors;
my $count = 1;
+
my $count = 1;
   
# Get the center and point that define the correct circle and compute the square of
+
# Get the center and point that define the correct circle and
# the radius.
+
# compute the square of the radius.
my ($cx, $cy) = $correct->[0]->extract(3)->value;
 
my ($px, $py) = $correct->[0]->extract(4)->value;
 
my $r_squared = ($cx - $px) ** 2 + ($cy - $py) ** 2;
 
   
my $pointOnCircle = sub
 
  +
my ($cx, $cy) = $correct->[0]->extract(3)->value;
{
 
  +
my ($px, $py) = $correct->[0]->extract(4)->value;
my $point = shift;
 
  +
my $r_squared = ($cx - $px) ** 2 + ($cy - $py) ** 2;
my ($x, $y) = $point->value;
 
return ($x - $cx) ** 2 + ($y - $cy) ** 2 == $r_squared;
 
};
 
   
for (@$student)
 
  +
my $pointOnCircle = sub {
{
 
  +
my $point = shift;
my $nth = Value::List->NameForNumber($count++);
 
  +
my ($x, $y) = $point->value;
  +
return ($x - $cx) ** 2 + ($y - $cy) ** 2 == $r_squared;
  +
};
   
$score += 1, next
 
  +
# Iterate through the objects the student graphed and check to
if ($_->extract(1) eq $correct->[0]->extract(1) &&
 
  +
# see if each is the correct circle.
$_->extract(2) eq $correct->[0]->extract(2) &&
 
  +
for (@$student) {
$_->extract(3) == $correct->[0]->extract(3) &&
 
  +
my $nth = Value::List->NameForNumber($count++);
$pointOnCircle->($_->extract(4)));
 
  +
  +
# this checks if the student input matches the circle, type
  +
# (solid or dashed), the center of the circle and
  +
# checks if a point is on the circle.
   
push(@errors, "The $nth object graphed is not a " . $correct->[0]->extract(1)),
 
  +
$score += 1, next
next if ($_->extract(1) ne $correct->[0]->extract(1));
+
if ($_->extract(1) eq $correct->[0]->extract(1) &&
  +
$_->extract(2) eq $correct->[0]->extract(2) &&
  +
$_->extract(3) == $correct->[0]->extract(3) &&
  +
$pointOnCircle->($_->extract(4)));
   
push(@errors, "The $nth object graphed should be a " . $correct->[0]->extract(2) . " circle."),
 
  +
# the following gives additional information to the student
next if ($_->extract(2) ne $correct->[0]->extract(2));
 
  +
  +
push(@errors, "The $nth object graphed is not a circle"),
  +
next if ($_->extract(1) ne $correct->[0]->extract(1));
   
push(@errors, "The $nth object graphed is incorrect.");
+
push(@errors, "The $nth object graphed should be a " .
}
+
$correct->[0]->extract(2) . " circle."),
  +
next if ($_->extract(2) ne $correct->[0]->extract(2));
   
return ($score, @errors);
 
  +
push(@errors, "The $nth object graphed is incorrect.");
}
 
  +
}
}
 
  +
  +
return ($score, @errors);
  +
};
  +
  +
  +
$h = non_zero_random(-5, 5);
  +
$k = non_zero_random(-5, 5);
  +
$r = random(1, 4);
  +
  +
Context()->variables->add("y" => "Real");
  +
$circle_eq_lhs = Formula("(x-$h)^2 + (y-$k)^2")->reduce;
  +
  +
  +
  +
$gt = GraphTool("{circle, solid, ($h, $k), ($h + $r, $k)}")->with(
  +
bBox => [-11, 11, 11, -11],
  +
cmpOptions => { list_checker => $gt_checker }
 
);
 
);
 
</pre>
 
</pre>
Line 109: Line 120:
 
<b>Setup:</b>
 
<b>Setup:</b>
 
<ul>
 
<ul>
 
  +
<li>The subroutine <pre>$gt_checker</pre> at the top is the answer checker. It checks if the student input matches the correct answer. </li>
  +
<li>The variables <tt>$h, $k</tt> and <tt>$r</tt> randomly pick a center and radius of the circle.</li>
  +
<li>The lines:
  +
<pre>
  +
Context()->variables->add("y" => "Real");
  +
$circle_eq_lhs = Formula("(x-$h)^2 + (y-$k)^2")->reduce;
  +
</pre>
  +
are used to print out nicely the equation of the circle. </li>
  +
<li>The command <tt>GraphTool</tt> creates the graph tool (the axes and input buttons for the various types of graphs). The first argument is a string surrounded by {}. The arguments are:
  +
<ul>
  +
<li>The type of geometric figure (circle, line, parabola or fill)</li>
  +
<li>The type of figure (solid or dashed)</li>
  +
<li>Other information about the figure. For example, with the circle, the center and another point on the circle. See [https://webwork.maa.org/pod/pg/macros/parserGraphTool.html#GRAPH-OBJECTS Graph Objects] for more details.</li>
  +
</ul>
  +
</li>
  +
<li>Other parameters of the <tt>GraphTool</tt> can be set using <tt>with</tt>. The following include other features:
  +
<ul>
  +
<li><tt>bbox</tt>: this is an array reference of four values <tt>xmin, ymin, xmax, ymax</tt> indicating the lower left and upper right corners of the plot.</li>
  +
<li><tt>cmpOptions</tt>: this is a hash of options passed to the <tt>cmp</tt> method for checking the answer. The example here:
  +
<pre>
  +
cmpOptions => { list_checker => $gt_checker }
  +
</pre>
  +
has the checker use the one we defined above. </li>
  +
</ul></li>
  +
<li>More documentation can be found at: [https://webwork.maa.org/pod/pg/macros/parserGraphTool.html]</li>
 
</ul>
 
</ul>
</p>
 
<p>
 
Notes: on using this and related Contexts.
 
 
</p>
 
</p>
   
Line 125: Line 157:
 
<pre>
 
<pre>
 
BEGIN_PGML
 
BEGIN_PGML
  +
Graph the circle given by the following equation.
   
[@ image(insertGraph($graph_image), width => 300, tex_size => 1000) @]*
 
  +
[`[$circle_eq_lhs] = [$r ** 2]`]
   
  +
[_]{$gt}
 
END_PGML
 
END_PGML
 
</pre>
 
</pre>
Line 133: Line 167:
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
This is how to insert the tikz image. Note the <tt>width</tt> and <tt>tex_size</tt> parameters can change the size of the image on the web and as hardcopy.
 
  +
This asks to graph the circle given by the equation. And the code:
  +
<pre>
  +
[_]{$gt}
  +
</pre>
  +
inserts the GraphTool.
 
</p>
 
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
   
<!-- Answer section -->
+
<!-- Solution section -->
   
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
BEGIN_PGML_SOLUTION
  +
The equation of the circle of the form:
  +
  +
[`[$circle_eq_lhs] = [$r ** 2]`]
  +
  +
has a center at [`([$h],[$k])`] and radius [$r]. To enter the graph, click the circle tool, then click the center at [`([$h],[$k])`] and then click a second point that is [$r] units from the center. This is easist going left, right, up or down from the center.
  +
END_PGML_SOLUTION
  +
 
ENDDOCUMENT();
 
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
This doesn't have a question, so we aren't checking an answer.
 
  +
This is the solution.
 
</p>
 
</p>
 
</td>
 
</td>
Line 161: Line 207:
   
 
<ul>
 
<ul>
<li>POD documentation: [https://webwork.maa.org/pod/pg/macros/PGtikz.html PGtikz.pl]</li>
+
<li>POD documentation: [https://webwork.maa.org/pod/pg/macros/parserGraphTool.html]</li>
<li>PG macro: [https://github.com/openwebwork/pg/blob/PG-2.16/macros/PGtikz.pl PGtikz.pl]</li>
 
<!--<li>PG macro: [https://github.com/openwebwork/pg/blob/master/macros/PGtikz.pl PGtikz.pl]</li>-->
 
 
</ul>
 
</ul>

Latest revision as of 18:13, 17 August 2021

Graph Tool


This example shows how to get student input in the form of a graph (a circle) by using interactive graphing tools.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PGML.pl",
  "parserGraphTool.pl"
);
TEXT(beginproblem());

Initialization: It is important to include the parseGraphTool.pl macro.

## this is the answer checker for the graph tool

# This grader allows the student to graph the correct circle multiple 
# times.  The idea is that the graph is graded based on appearance.  
# No matter how many times the student graphs the correct circle, 
# the resulting graph appears the same.

$gt_checker = sub {
    my ($correct, $student, $ans, $value) = @_;
    return 0 if $ans->{isPreview};

    my $score = 0;
    my @errors;
    my $count = 1;

    # Get the center and point that define the correct circle and 
    # compute the square of the radius.

    my ($cx, $cy) = $correct->[0]->extract(3)->value;
    my ($px, $py) = $correct->[0]->extract(4)->value;
    my $r_squared = ($cx - $px) ** 2 + ($cy - $py) ** 2;

    my $pointOnCircle = sub {
        my $point = shift;
        my ($x, $y) = $point->value;
        return ($x - $cx) ** 2 + ($y - $cy) ** 2  == $r_squared;
    };

    # Iterate through the objects the student graphed and check to
    # see if each is the correct circle.
    for (@$student) {
        my $nth = Value::List->NameForNumber($count++);
        
        # this checks if the student input matches the circle, type
        # (solid or dashed), the center of the circle and
        # checks if a point is on the circle. 

        $score += 1, next
        if ($_->extract(1) eq $correct->[0]->extract(1) &&
            $_->extract(2) eq $correct->[0]->extract(2) &&
            $_->extract(3) == $correct->[0]->extract(3) &&
            $pointOnCircle->($_->extract(4)));

        # the following gives additional information to the student
        
        push(@errors, "The $nth object graphed is not a circle"),
        next if ($_->extract(1) ne $correct->[0]->extract(1));

        push(@errors, "The $nth object graphed should be a " .
                      $correct->[0]->extract(2) . " circle."),
        next if ($_->extract(2) ne $correct->[0]->extract(2));

        push(@errors, "The $nth object graphed is incorrect.");
    }

    return ($score, @errors);
};


$h = non_zero_random(-5, 5);
$k = non_zero_random(-5, 5);
$r = random(1, 4);

Context()->variables->add("y" => "Real");
$circle_eq_lhs = Formula("(x-$h)^2 + (y-$k)^2")->reduce;



$gt = GraphTool("{circle, solid, ($h, $k), ($h + $r, $k)}")->with(
    bBox => [-11, 11, 11, -11],
    cmpOptions => { list_checker => $gt_checker }
);

Setup:

  • The subroutine
    $gt_checker
    at the top is the answer checker. It checks if the student input matches the correct answer.
  • The variables $h, $k and $r randomly pick a center and radius of the circle.
  • The lines:
    Context()->variables->add("y" => "Real");
    $circle_eq_lhs = Formula("(x-$h)^2 + (y-$k)^2")->reduce;
    
    are used to print out nicely the equation of the circle.
  • The command GraphTool creates the graph tool (the axes and input buttons for the various types of graphs). The first argument is a string surrounded by {}. The arguments are:
    • The type of geometric figure (circle, line, parabola or fill)
    • The type of figure (solid or dashed)
    • Other information about the figure. For example, with the circle, the center and another point on the circle. See Graph Objects for more details.
  • Other parameters of the GraphTool can be set using with. The following include other features:
    • bbox: this is an array reference of four values xmin, ymin, xmax, ymax indicating the lower left and upper right corners of the plot.
    • cmpOptions: this is a hash of options passed to the cmp method for checking the answer. The example here:
      cmpOptions => { list_checker => $gt_checker }
      
      has the checker use the one we defined above.
  • More documentation can be found at: [1]

BEGIN_PGML
Graph the circle given by the following equation.

    [`[$circle_eq_lhs] = [$r ** 2]`]

[_]{$gt}
END_PGML

Main Text: This asks to graph the circle given by the equation. And the code:

[_]{$gt}

inserts the GraphTool.

BEGIN_PGML_SOLUTION
The equation of the circle of the form:

[`[$circle_eq_lhs] = [$r ** 2]`]

has a center at [`([$h],[$k])`] and radius [$r].   To enter the graph, click the circle tool, then click the center at [`([$h],[$k])`] and then click a second point that is [$r] units from the center.  This is easist going left, right, up or down from the center.
END_PGML_SOLUTION

ENDDOCUMENT();

This is the solution.

Problem Techniques Index


  • POD documentation: [2]