It took me a while to figure out how to properly include solutions inside a WeBWorK problem. I’m recording this here for future reference. I’m modifying a problem from the National Problem Library here. I’ve restated the problem slightly and have included the solution, to match the format of the other questions we ask students on our WeBWorK system.

Code:

## DESCRIPTION
##  Calculus
## ENDDESCRIPTION
## KEYWORDS('Calculus','Derivatives')
 
## DBsubject('Calculus')
## DBchapter('Differentiation')
## DBsection('Related Rates')
## Date('')
## Author('')
## Institution('')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
DOCUMENT();        # This should be the first executable line in the problem.
 
loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl"
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
 
$a1 = random(2,5,1);
$r1 = random(1,5,1);
$deriv1 = 2*3.14159265*$r1*$a1;
TEXT(EV2(<<EOT));
Let \(A\)  be the area of a circle with radius \(r\). If
\( \displaystyle \frac { dr }{ dt } = $a1 \), find
\( \displaystyle \frac { dA }{ dt }  \) when \( r = $r1 \).
$BR
You may write the exact answer (use "pi" for \(\pi\)) or an approximation 
accurate to within 2 decimal places.
$BR
$BR
\(\displaystyle\frac{dA}{dt}=\)\{ans_rule(20) \}
EOT
 
$ans = $deriv1;
$prod = 2*$r1*$a1;
ANS(num_cmp($ans));
SOLUTION(EV3(<<'END_SOLUTION'));
 
$PAR SOLUTION $PAR
Viewing \(r\) as a function of \(t\), we have \(A=\pi r^2=\pi(r(t))^2\). 
Hence, the product rule gives us
\[\frac{dA}{dt}=\frac{d}{dt}(\pi r(t)r(t))=2\pi r(t)r'(t)=2\pi r\frac{dr}{dt}.\]
Filling in \(r=$r1\) and \(\displaystyle\frac{dr}{dt}=$a1\) gives
\[\frac{dA}{dt}=2\pi ($r1)($a1)=$prod\pi\approx $ans.\]
 
END_SOLUTION
ENDDOCUMENT();