Difference between revisions of "FormulasUpToMultiplication"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Formulas Up To Multiplication by a Nonzero Constant</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;pad...)
 
Line 103: Line 103:
 
<p>
 
<p>
 
<b>Answer Evaluation:</b>
 
<b>Answer Evaluation:</b>
We use a local context with an adaptive parameter to check the answer.
 
  +
We use a local context with an adaptive parameter to check the answer. For more on adaptive parameters, see [http://webwork.maa.org/wiki/AdaptiveParameters AdaptiveParameters] When <code>$aSolution</code> is "complicated", you may need to replace <code>$c0 * $aSolution</code> in the custom answer checker by its value <code>$c0 * (x-2)(x+1)</code> in order to get things to work correctly.
 
</p>
 
</p>
 
</td>
 
</td>

Revision as of 19:46, 14 April 2010

Formulas Up To Multiplication by a Nonzero Constant


This PG code shows how to check student answers that are correct up to multiplication by a nonzero constant.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
);

TEXT(beginproblem()); 

Initialization: We need only essential macros.

Context("Numeric");

$aSolution = Compute("(x-2)(x+1)");

Setup: Nothing surprising here.

BEGIN_TEXT
Find a quadratic equation in terms of the variable 
\( x \) with roots \( -1 \) and \( 2 \).
$PAR
y = \{ ans_rule(30) \}
END_TEXT

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

$showPartialCorrectAnswers = 1;

ANS( $aSolution->cmp(checker => sub {
      my ( $correct, $student, $self ) = @_;
      my $context = Context()->copy;
      return 0 if $student == 0;
      $context->flags->set(no_parameters=>0);
      $context->variables->add('C0'=>'Parameter');
      my $c0 = Formula($context,'C0');
      $student = Formula($context,$student);
      $correct = Formula($context,"$c0 * $aSolution");
      return $correct == $student;
    }
) );

ENDDOCUMENT();

Answer Evaluation: We use a local context with an adaptive parameter to check the answer. For more on adaptive parameters, see AdaptiveParameters When $aSolution is "complicated", you may need to replace $c0 * $aSolution in the custom answer checker by its value $c0 * (x-2)(x+1) in order to get things to work correctly.

Problem Techniques Index