Difference between revisions of "DynamicImages3"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Dynamic Graphic Images, with Filled Regions</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px...)
 
Line 24: Line 24:
 
<pre>
 
<pre>
 
DOCUMENT();
 
DOCUMENT();
loadMacros("PGbasicmacros.pl",
+
loadMacros(
"PGchoicemacros.pl",
+
"PGstandard.pl",
"PGanswermacros.pl",
+
"PGgraphmacros.pl",
"PGgraphmacros.pl",
+
# "PGnumericalmacros.pl", # might be useful
"PGnumericalmacros.pl",
+
);
"extraAnswerEvaluators.pl",
 
"weightedGrader.pl"
 
);
 
   
 
TEXT(beginproblem());
 
TEXT(beginproblem());
 
install_weighted_grader();
 
 
$showPartialCorrectAnswers = 1;
 
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 52: Line 48:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
# Construct a graph for the left endpoint Riemann sum,
 
# define the function to be graphed, and add it to the graph
 
$graphL = init_graph(-1,-1,9,9,ticks=>[10,10],axes=>[0,0],pixels=>[400,400]);
 
$c = random(8,12,1); # a constant for scaling the function
 
$f = FEQ("x**2/$c for x in <-1,9> using color:blue and weight:2");
 
$ftex = "\frac{x^2}{$c}";
 
# the parentheses around $fRefL are necessary
 
($fRefL) = plot_functions( $graphL, $f );
 
   
  +
$xmin = random(-3,-1,1);
  +
$xmax = random(1,3,1);
   
# Generate arrays of x and y values for the Riemann sum.
 
  +
$ymin = random(-3,-1,1);
# There are n+1 entries in each array so that we can use
 
  +
$ymax = random(1,3,1);
# only one pair of arrays for both the left and the right
 
# endpoint Riemann sums.
 
$a = random(2,4,1); # left endpoint of interval
 
$b = $a+4; # right endpoint of interval
 
$n = 8; # number of rectangles
 
$deltax = ($b - $a)/$n;
 
foreach $k (0..$n) { $x[$k] = $a + $k * $deltax; }
 
foreach $k (0..$n) { $y[$k] = &{$fRefL->rule}($x[$k]); }
 
   
  +
# filled triangle with dark border
  +
$gr1 = init_graph(-4,-4,4,4,grid=>[8,8],axes=>[0,0],pixels=>[300,300]);
  +
$gr1->new_color("lightgreen",156,215,151); # RGB
  +
$gr1->new_color("darkgreen", 0, 86, 34);
  +
$gr1->moveTo($xmin,$ymin);
  +
$gr1->lineTo($xmax,$ymin,"darkgreen",2); # bottom edge
  +
$gr1->lineTo($xmin,$ymax,"darkgreen",2); # hypotenuse
  +
$gr1->lineTo($xmin,$ymin,"darkgreen",2); # left edge
  +
$gr1->fillRegion([$xmin+0.1,$ymin+0.1,"lightgreen"]);
   
# Graph the left endpoint Riemann sum
 
  +
# filled rectangle with dark border
$lightblue = $graphL->im->colorAllocate(148,201,255);
 
  +
$gr2 = init_graph(-4,-4,4,4,grid=>[8,8],axes=>[0,0],pixels=>[300,300]);
$darkblue = $graphL->im->colorAllocate(100,100,255);
 
  +
$gr2->new_color("lightblue",148,201,255);
# Create arrays of pixel references for x and y values
 
  +
$gr2->new_color("darkblue", 100,100,255);
foreach $k (0..8) {
 
  +
$gr2->im->filledRectangle($xmin,$ymin,$xmax,$ymax,"lightblue");
$xpixL[$k] = $graphL->ii($x[$k]);
 
  +
$gr2->im->rectangle($xmin,$ymin,$xmax,$ymax,"darkblue");
$ypixL[$k] = $graphL->jj($y[$k]);
 
}
 
$xaxisL = $graphL->jj(0);
 
# Plot the rectangles in the Riemann sum
 
foreach $k (0..$n-1) {
 
$graphL->im->filledRectangle($xpixL[$k],$ypixL[$k],$xpixL[$k+1],$xaxisL,$lightblue);
 
$graphL->im->rectangle($xpixL[$k],$ypixL[$k],$xpixL[$k+1],$xaxisL,$darkblue);
 
}
 
$graphL->lb(new Label ( 8.5,0,'x','black','right','top'));
 
$graphL->lb(new Label ( -0.25,8.5,'y','black','right','top'));
 
 
 
# Construct a graph for the right endpoint Riemann sum
 
$graphR = init_graph(-1,-1,9,9,ticks=>[10,10],axes=>[0,0],pixels=>[400,400]);
 
# the parentheses around $fRefR are necessary
 
($fRefR) = plot_functions( $graphR, $f );
 
 
 
# Graph the right endpoint Riemann sum
 
$lightblue = $graphR->im->colorAllocate(148,201,255);
 
$darkblue = $graphR->im->colorAllocate(100,100,255);
 
# Create arrays of pixel references for x and y values
 
foreach $k (0..8) {
 
$xpixR[$k] = $graphR->ii($x[$k]);
 
$ypixR[$k] = $graphR->jj($y[$k]);
 
}
 
$xaxisR = $graphR->jj(0);
 
# Plot the rectangles in the Riemann sum
 
foreach $k (1..$n) {
 
$graphR->im->filledRectangle($xpixR[$k-1],$ypixR[$k],$xpixR[$k],$xaxisR,$lightblue);
 
$graphR->im->rectangle($xpixR[$k-1],$ypixR[$k],$xpixR[$k],$xaxisR,$darkblue);
 
}
 
$graphR->lb(new Label ( 8.5,0,'x','black','right','top'));
 
$graphR->lb(new Label ( -0.25,8.5,'y','black','right','top'));
 
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 134: Line 91:
 
<pre>
 
<pre>
 
BEGIN_TEXT
 
BEGIN_TEXT
 
The rectangles in the graph below illustrate a left endpoint Riemann sum for \( \displaystyle f(x) = $ftex \) on the interval \( \lbrack $a, $b \rbrack \).
 
$BR
 
The value of this left endpoint Riemann sum is \{NAMED_ANS_RULE('optional1',30)\},
 
and this Riemann sum is an \{ NAMED_POP_UP_LIST('optional2',['?','overestimate
 
of','equal to','underestimate of','there is ambiguity']) \} the area of the region enclosed by \(\displaystyle y = f(x) \), the x-axis, and the vertical lines x = $a and x = $b.
 
 
$BR
 
$BR
 
 
 
$BCENTER
 
$BCENTER
\{ begintable(1) \}
 
  +
\{ image( insertGraph($gr1), height=>300, width=>300, tex_size=>800 ) \}
\{ row( image( insertGraph($graphL), height=>400, width=>400, tex_size=>800 ) ) \}
 
\{ row("Left endpoint Riemann sum for \( y = $ftex \) on \( \lbrack $a, $b \rbrack \)") \}
 
\{ endtable() \}
 
 
$ECENTER
 
$ECENTER
 
  +
$PAR
$BR
 
$HR
 
$BR
 
 
The rectangles in the graph below illustrate a right endpoint Riemann sum for \( \displaystyle f(x) = $ftex \) on the interval \( \lbrack $a, $b \rbrack \).
 
$BR
 
The value of this right endpoint Riemann sum is \{NAMED_ANS_RULE('optional3',30)\},
 
and this Riemann sum is an \{ NAMED_POP_UP_LIST('optional4',['?','overestimate of','equal to','underestimate of','there is ambiguity']) \} the area of the region enclosed by \(\displaystyle y = f(x) \), the x-axis, and the vertical lines x = $a and x = $b.
 
$BR
 
$BR
 
 
 
$BCENTER
 
$BCENTER
\{ begintable(1) \}
 
  +
\{ image( insertGraph($gr2), height=>300, width=>300, tex_size=>800 ) \}
\{ row( image( insertGraph($graphR), height=>400, width=>400, tex_size=>800 ) ) \}
 
\{ row("Right endpoint Riemann sum for \( y = $ftex \) on \( \lbrack $a, $b \rbrack \)") \}
 
\{ endtable() \}
 
 
$ECENTER
 
$ECENTER
 
 
END_TEXT
 
END_TEXT
 
</pre>
 
</pre>
Line 184: Line 113:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
$LeftRiemannSum = 0;
 
foreach $k (0..$n-1) { $LeftRiemannSum = $LeftRiemannSum + $y[$k]; }
 
$LeftRiemannSum = $deltax * $LeftRiemannSum;
 
NAMED_WEIGHTED_ANS('optional1',num_cmp($LeftRiemannSum),45);
 
   
NAMED_WEIGHTED_ANS('optional2',str_cmp("underestimate of"),5);
 
  +
$showPartialCorrectAnswers = 1;
 
$RightRiemannSum = 0;
 
foreach $k (1..$n) { $RightRiemannSum = $RightRiemannSum + $y[$k]; }
 
$RightRiemannSum = $deltax * $RightRiemannSum;
 
NAMED_WEIGHTED_ANS('optional3',num_cmp($RightRiemannSum),45);
 
 
NAMED_WEIGHTED_ANS('optional4',str_cmp("overestimate of"),5);
 
   
 
ENDDOCUMENT();
 
ENDDOCUMENT();
 
 
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">

Revision as of 20:50, 22 February 2010

Dynamic Graphic Images, with Filled Regions


This code snippet shows the essential PG code to check student answers that are equations. 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
DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGgraphmacros.pl",
# "PGnumericalmacros.pl", # might be useful
);

TEXT(beginproblem());

Initialization: To do ..(what you are doing)........., we don't have to change the tagging and documentation section of the problem file. In the initialization section, we need to include the macros file -------.pl.


$xmin = random(-3,-1,1);
$xmax = random(1,3,1);

$ymin = random(-3,-1,1);
$ymax = random(1,3,1);

#  filled triangle with dark border
$gr1 = init_graph(-4,-4,4,4,grid=>[8,8],axes=>[0,0],pixels=>[300,300]);
$gr1->new_color("lightgreen",156,215,151); # RGB
$gr1->new_color("darkgreen",   0, 86, 34);
$gr1->moveTo($xmin,$ymin);
$gr1->lineTo($xmax,$ymin,"darkgreen",2); # bottom edge
$gr1->lineTo($xmin,$ymax,"darkgreen",2); # hypotenuse
$gr1->lineTo($xmin,$ymin,"darkgreen",2); # left edge
$gr1->fillRegion([$xmin+0.1,$ymin+0.1,"lightgreen"]);

#  filled rectangle with dark border
$gr2 = init_graph(-4,-4,4,4,grid=>[8,8],axes=>[0,0],pixels=>[300,300]);
$gr2->new_color("lightblue",148,201,255);
$gr2->new_color("darkblue", 100,100,255);
$gr2->im->filledRectangle($xmin,$ymin,$xmax,$ymax,"lightblue");
$gr2->im->rectangle($xmin,$ymin,$xmax,$ymax,"darkblue");

Setup: We specify that the Context should be ......, and define the answer to be a formula.

Notes: on using this and related Contexts.

BEGIN_TEXT
$BCENTER
\{ image( insertGraph($gr1), height=>300, width=>300, tex_size=>800 ) \}
$ECENTER
$PAR
$BCENTER
\{ image( insertGraph($gr2), height=>300, width=>300, tex_size=>800 ) \}
$ECENTER
END_TEXT

Main Text: The problem text section of the file is as we'd expect.


$showPartialCorrectAnswers = 1;

ENDDOCUMENT();

Answer Evaluation: As is the answer.

Problem Techniques Index