DynamicImages3

From WeBWorK_wiki
Revision as of 18:53, 22 February 2010 by Pearson (talk | contribs) (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...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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("PGbasicmacros.pl",
           "PGchoicemacros.pl",
           "PGanswermacros.pl",
           "PGgraphmacros.pl",
           "PGnumericalmacros.pl",
           "extraAnswerEvaluators.pl",
           "weightedGrader.pl"
           );

TEXT(beginproblem());

install_weighted_grader();

$showPartialCorrectAnswers = 1;

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.

# 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 );


# Generate arrays of x and y values for the Riemann sum.
# There are n+1 entries in each array so that we can use
# 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]); }


# Graph the left endpoint Riemann sum
$lightblue = $graphL->im->colorAllocate(148,201,255);
$darkblue = $graphL->im->colorAllocate(100,100,255);
# Create arrays of pixel references for x and y values
foreach $k (0..8) {
   $xpixL[$k] = $graphL->ii($x[$k]);
   $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'));

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

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
\{ begintable(1) \}
\{ 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

$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
\{ begintable(1) \}
\{ 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

END_TEXT

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

$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);

$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();


Answer Evaluation: As is the answer.

Problem Techniques Index