Back to Teaching Page

WeBWork Resources


Answer Help

\{ AnswerFormatHelp("numbers") \}
\{ AnswerFormatHelp("formulas") \}


Reduction Rules

[WeBWork Formula]
[WeBWork Context]

Context()->noreduce("(-x)-y","(-x)+y");


Display Decimal Digits

Context()->{format}{number} = "%.10f";


Aternative Correct Answers

$x1 = Formula("A * cos($w*t) + B * sin($w*t)")->reduce;
$x2 = Formula("A * sin($w*t) + B * cos($w*t)")->reduce;

ANS( $x1->cmp( checker=>sub {
   my ( $correct, $student, $ansHash ) = @_;
return ($student == $x1) || ($student == $x2);
} ) );


Unordered Answers

loadMacros("unorderedAnswer.pl")

$showPartialCorrectAnswers = 1;

$factor1 = Formula("x-$a")->reduce;
$factor2 = Formula("x-$b")->reduce;
UNORDERED_ANS( $factor1->cmp(), $factor2->cmp() );

(*) UNORDERED_ANS cannot be used multiple times in one question


If Condition

if ( $a > 0 ) {
     $ans = "increasing, concave up";
} else {
     $ans = "decreasing, concave down"; }


Do Loop

$a = non_zero_random(-9, 9, 1);
do {$b = non_zero_random(-9, 9, 1);} until ($b != $a);


Name Pattern

Context()->variables->{namePattern} = qr/[a-z][a-z0-9_]*'*/i;
Context()->variables->add(
       "y" => "Real",
       "y'" => "Real",
       "y''" => "Real",
       "C" => "Real",
       "C1" => ["Real", TeX=>"C_1"],
       xk => ["Real",TeX=>"x_k"],
       xk1 => ["Real",TeX=>"x_{k+1}"]
);


Correct Answers up to a Multiplicity

$ans = Formula("x^2+y^2");
ANS( $ans->cmp( checker=>sub {
     my ( $correct, $student, $ansHash ) = @_;
     $tmp = Formula("$student/$ans");
return ( Formula($tmp->D('x'))==Formula(0) &&
       ( Formula($tmp->D('t'))==Formula(0) &&
       ( Formula($tmp->D('X'))==Formula(0)
} ) );


MultiAnswers

# Factor 2xy - 6y + x - 3 into g(x)h(y)
# The answer can be (x-3)(2y+1) or (2x-6)(y+0.5)

loadMacros(
"parserMultiAnswer.pl",
);
$gofx = Formula("x-3") -> reduce;
$hofy = Formula("2*y + 1") -> reduce();
$multiAns = MultiAnswer($gofx, $hofy)->with(
   singleResult => 1,
   checker => sub {
   my ( $correct, $student, $self ) = @_;
   my ( $f1stu, $f2stu ) = @{$student};
   my ( $f1, $f2 ) = @{$correct};

   $tmp0 = Formula("$f1stu/$f1");
   $tmp1 = Formula("$f1 * $f2");
   $tmp2 = Formula("$f1stu * $f2stu");

   if ( (Formula($tmp0->D('x'))!=Formula(0)) ||
     (Formula($tmp0->D('y'))!=Formula(0)) ||
     ($tmp1 != $tmp2) ) {
     return 0;
   } else {
     return 1;
   }
   }
);

BEGIN_TEXT
Factor \(2xy - 6y + x - 3\) into \(h(x)g(y)\):
$PAR
\(2xy - 6y + x - 3 =\) \{$multiAns -> ans_rule()\} \(\cdot\) \{$multiAns -> ans_rule()\}
$SPACE$SPACE\{ AnswerFormatHelp("formulas") \}
END_TEXT

ANS($multiAns -> cmp());


Sytem of Linear Equations with Answer Entries

loadMacros("PGmatrixmacros.pl");

System of linear equations
\{ mbox(
display_matrix([[ans_rule(1), "0" , "0", "0"],
       [ans_rule(1),ans_rule(1), "0", "0"],
       ["0",ans_rule(1),ans_rule(1), "0"],
       ["0","0",ans_rule(1),ans_rule(1)]], 'align'=>"cccc"),
display_matrix([["\(m_1\)"], ["\(m_2\)"], ["\(m_3\)"], ["\(m_4\)"]], 'align'=>"c"),
"=",
display_matrix([[ans_rule(1)], [ans_rule(1)], [ans_rule(1)], [ans_rule(1)]], 'align'=>"c")
)\}


Layout Table

loadMacros("niceTables.pl");

\{LayoutTable(
[
     ['\(1^2={}\)'.ans_rule(3),'\(2^2={}\)'.ans_rule(3)],
     ['\(3^2={}\)'.ans_rule(3),'\(4^2={}\)'.ans_rule(3)],
],
align => 'r | r',
center => 0,
);\}

\{LayoutTable(
[
     [' ', ans_rule(3), ' '],
     [ans_rule(3),' ', ' '],
],
align => 'r | r | r',
center => 0,
);\}


Layout Table: Display Fractions

loadMacros("niceTables.pl");

\{LayoutTable(
[ [ "\(\displaystyle\frac{12}{20}\) =",
   LayoutTable([[ [ans_rule(1), bottom => 1] ], [ ans_rule(1) ],], padding => [ 0.5, 0.5 ]),
   "\( = \)",
   ans_rule(1),] ],
   padding => [ 0, 0.25 ],
   valign => 'middle',
);\}


Pop-Up

loadMacros(
   "parserPopUp.pl",
);

$popup0 = PopUp(["?",
     "Big Oh h^3", "Big Oh h^2",
     "Small oh h^3", "Small oh h^2",],
     "Big Oh h^2");

BEGIN_TEXT
Choose the correct answer
$PAR
\(O(h^3) + O(h^2) = \) $SPACE \{$popup0 -> menu()\}
END_TEXT

ANS($popup0 -> cmp());


Checkbox Multiple Choice

loadMacros(
   "parserPopUp.pl",
);

$hm = Formula("h**3")->reduce;
$hn = Formula("h**4")->reduce;
$hmn = Formula("h**7")->reduce;

$mc0 = new_checkbox_multiple_choice();
$mc0 -> qa(
   "Select all correct answers. There may be more than one correct answer.",
   "\( O$hmn \)",
   "\( o$hn \)",
   "\( o$hm \)"
);
$mc0 -> extra("\( o$hmn \)");
$mc0 -> makeLast("All of the above", "None of the above");

Context()->texStrings;
BEGIN_TEXT
$PAR
\(O($hm) \cdot $hn =\ ?\)
$PAR
\{$mc0 -> print_q() \}
$BR
\{$mc0 -> print_a() \}
END_TEXT

ANS( checkbox_cmp($mc0 -> correct_ans() ) );


Add Solution

Context()->texStrings;
BEGIN_SOLUTION
...
END_SOLUTION


Draw Graphs of Functions

$a = random(1,2); # left end
$c = $a+random(1,2); # parabola vertex x
$b = 2*$c-$a+1; # the right end is above the left end
$y1c = random(1,2); # vertex y


# parabola
$y1 = Formula("x^2-(2*$c)*x + ($c*$c+$y1c)")->reduce;
$y1a = $y1 -> eval(x=>$a);
$y1b = $y1 -> eval(x=>$b);

# line defined by 2 points (a,y2a) and (b,y2b)
$y2a = $y1a;
$y2b = $y1b;
$y2 = Formula("(($y2b-$y2a)/($b-$a))*x + ($y2a-$a*(($y2b-$y2a)/($b-$a)))")->reduce;

# GRAPH
$xmin = min($a,0)-1;
$xmax = $b+1;
$ymin = -1;
$ymax = max($y2a,$y2b)+1;

$gr = init_graph($xmin,$ymin,$xmax,$ymax,
  axes=>[0,0],
  grid=>[$xmax-$xmin,$ymax-$ymin],
  size=>[300,300]
);

add_functions($gr, "$y1 for x in <$a,$b>" . " using color:blue and weight:2");
add_functions($gr, "$y2 for x in <$a,$b>" . " using color:blue and weight:2");

# vertical line
$gr->moveTo($a,$y1a);
$gr->lineTo($a,$y2a,'blue',2);

# add points
$gr->stamps(closed_circle($a,$y1a,'red',10) );
$gr->stamps(closed_circle($b,$y1b,'red',10) );

Context()->texStrings;
BEGIN_TEXT
Let \(R\) be the region bounded by the curves \(y=$y1\) and \(y=$y2\).

$PAR
$BCENTER
\{ image( insertGraph($gr), width=>300,height=>300,tex_size=>800 ) \}
$BR
(Click on graph to enlarge)
$ECENTER