AnswerUpToMultiplication1

From WeBWorK_wiki
Revision as of 14:40, 3 December 2010 by Pearson (talk | contribs) (Created page with '<h2>Answer is a Function up to Multiplication by a Nonzero Constant</h2> 300px|thumb|right|Click to enlarge <p style="background-color:#f9…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Answer is a Function up to Multiplication by a Nonzero Constant

Click to enlarge

This PG code shows how to

  • Download file: File:AnswerUpToMultiplication1.txt (change the file extension from txt to pg when you save it)
  • File location in NPL: FortLewis/Authoring/Templates/Precalc/AnswerUpToMultiplication1.pg


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
);

TEXT(beginproblem()); 

Initialization:

Context("Numeric");

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

Setup:

Context()->texStrings;
BEGIN_TEXT
Find a quadratic equation in terms of the variable 
\( x \) with roots \( -1 \) and \( 2 \).
$BR
$BR
\( y = \) \{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \}
END_TEXT
Context()->normalStrings;

Main Text:

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

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.

Context()->texStrings;
BEGIN_SOLUTION
${PAR}SOLUTION:${PAR}
Solution explanation goes here.
END_SOLUTION
Context()->normalStrings;

COMMENT('MathObject version.');

ENDDOCUMENT();

Solution:

Templates by Subject Area