Difference between revisions of "RiemannSums1"

From WeBWorK_wiki
Jump to navigation Jump to search
m
(PGML example link)
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
This PG code shows how to make dynamically generated graphs with shaded (filled) Riemann sums.
 
This PG code shows how to make dynamically generated graphs with shaded (filled) Riemann sums.
 
</p>
 
</p>
* Download file: [[File:RiemannSums1.txt]] (change the file extension from txt to pg when you save it)
 
  +
* File location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/IntegralCalc/RiemannSums1.pg FortLewis/Authoring/Templates/IntegralCalc/RiemannSums1.pg]
* File location in NPL: <code>FortLewis/Authoring/Templates/IntegralCalc/RiemannSums1.pg</code>
+
* PGML location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/IntegralCalc/RiemannSums1_PGML.pg FortLewis/Authoring/Templates/IntegralCalc/RiemannSums1_PGML.pg]
 
   
 
<br clear="all" />
 
<br clear="all" />
Line 255: Line 255:
 
Context()->texStrings;
 
Context()->texStrings;
 
BEGIN_SOLUTION
 
BEGIN_SOLUTION
${PAR}SOLUTION:${PAR}
 
 
(A) The left endpoint Riemann sum is
 
(A) The left endpoint Riemann sum is
 
\( f($x[0]) \cdot 0.5 + f($x[1]) \cdot 0.5 + \cdots + f($x[$n-1]) \cdot 0.5
 
\( f($x[0]) \cdot 0.5 + f($x[1]) \cdot 0.5 + \cdots + f($x[$n-1]) \cdot 0.5

Revision as of 13:47, 14 June 2015

Dynamically Generated Graphs with Riemann Sums

Click to enlarge

This PG code shows how to make dynamically generated graphs with shaded (filled) Riemann sums.


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT();

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

TEXT(beginproblem());

install_weighted_grader();

$refreshCachedImages = 1;
$showPartialCorrectAnswers = 1;

Initialization:

# 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=>[250,250]);
$graphL->lb('reset');
foreach my $i (1..8) {
  $graphL->lb( new Label($i,-0.5,$i, 'black','center','middle'));
  $graphL->lb( new Label(-0.5,$i,$i, 'black','center','middle'));
}
$graphL->lb(new Label ( 8.5,0.25,'x','black','center','middle'));
$graphL->lb(new Label ( 0.25,8.5,'y','black','center','middle'));


$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+2; # right endpoint of interval
$n = 4; # 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..$n) {
   $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);
}




# Construct a graph for the right endpoint Riemann sum
$graphR = init_graph(-1,-1,9,9,ticks=>[10,10],axes=>[0,0],pixels=>[250,250]);
$graphR->lb('reset');
foreach my $i (1..8) {
  $graphR->lb( new Label($i,-0.5,$i, 'black','center','middle'));
  $graphR->lb( new Label(-0.5,$i,$i, 'black','center','middle'));
}
$graphR->lb(new Label ( 8.5,0.25,'x','black','center','middle'));
$graphR->lb(new Label ( 0.25,8.5,'y','black','center','middle'));

# 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..$n) {
   $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);
}

Setup:

Context()->texStrings;
BEGIN_TEXT
\{
ColumnTable(
"Suppose \( \displaystyle f(x) = $ftex \).".
$BR.
$BR.
"(a) The rectangles in the graph on the left illustrate 
a left endpoint Riemann sum for \( f(x) \) on the 
interval \( $a \leq x \leq $b \).  The value of this 
left endpoint Riemann sum is ".
NAMED_ANS_RULE('optional1',30). 
", and it 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.
"(b) The rectangles in the graph on the right illustrate 
a right endpoint Riemann sum for \( f(x) \) on the 
interval \( $a \leq x \leq $b \).  The value of this 
right endpoint Riemann sum is ".
NAMED_ANS_RULE('optional3',30).
", and it 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\)."
,
$BCENTER.
BeginTable(1).
AlignedRow( 
[image( insertGraph($graphL), height=>250, width=>250, tex_size=>450 ),
 image( insertGraph($graphR), height=>250, width=>250, tex_size=>450 )]
).
TableSpace(5,0).
AlignedRow(
["Left endpoint Riemann sum",
 "Right endpoint Riemann sum"]
).
EndTable().
$ECENTER
,
indent => 0, separation => 30, valign => "TOP"
);
\}
END_TEXT
Context()->normalStrings;

Main Text:

$LeftRiemannSum = 0;
foreach $k (0..$n-1) { $LeftRiemannSum = $LeftRiemannSum + $y[$k]; }
$LeftRiemannSum = Real("$deltax * $LeftRiemannSum");
NAMED_WEIGHTED_ANS('optional1',$LeftRiemannSum->cmp(),45);

NAMED_WEIGHTED_ANS('optional2',str_cmp("underestimate of"),5);

$RightRiemannSum = 0;
foreach $k (1..$n) { $RightRiemannSum = $RightRiemannSum + $y[$k]; }
$RightRiemannSum = Real("$deltax * $RightRiemannSum");
NAMED_WEIGHTED_ANS('optional3',$RightRiemannSum->cmp(),45);

NAMED_WEIGHTED_ANS('optional4',str_cmp("overestimate of"),5);

Answer Evaluation:

Context()->texStrings;
BEGIN_SOLUTION
(A) The left endpoint Riemann sum is 
\( f($x[0]) \cdot 0.5 + f($x[1]) \cdot 0.5 + \cdots + f($x[$n-1]) \cdot 0.5
= ( $y[0] + $y[1] + \cdots + $y[7] ) \cdot 0.5 = $LeftRiemannSum.\)
$BR
$BR
(B) The right endpoint Riemann sum is 
\( f($x[1]) \cdot 0.5 + f($x[2]) \cdot 0.5 + \cdots + f($x[$n]) \cdot 0.5
= ( $y[1] + $y[2] + \cdots + $y[$n] ) \cdot 0.5  = $RightRiemannSum.\)
END_SOLUTION
Context()->normalStrings;

COMMENT('MathObject version');

ENDDOCUMENT();

Solution:

Templates by Subject Area