Difference between revisions of "FactoringAndExpanding"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 64: Line 64:
   
 
checker => sub {
 
checker => sub {
my $correct = shift; my $student = shift; my $self = shift;
+
my $correct = shift; my $student = shift; my $ansHash = shift;
 
my ($F,$G) = @{$correct};
 
my ($F,$G) = @{$correct};
 
my ($f,$g) = @{$student};
 
my ($f,$g) = @{$student};
   
Value::Error('Neither factor can be constant')
+
$ansHash->setMessage(1,"Neither factor can be constant")
unless $f->isFormula && $g->isFormula;
+
unless $f->isFormula;
+
$ansHash->setMessage(2,"Neither factor can be constant")
Value::Error('The product of your factors does not equal the original')
+
unless $g->isFormula;
unless $F*$G == $f*$g;
 
# return 0 unless $F*$G == $f*$g;
 
   
 
# use an adaptive parameter 'a'
 
# use an adaptive parameter 'a'
Line 79: Line 79:
 
my $a = Formula($context,'a');
 
my $a = Formula($context,'a');
 
$f = Formula($context,$f);
 
$f = Formula($context,$f);
my $result = ($a*$F == $f || $a*$G == $f);
 
  +
$g = Formula($context,$g);
Value::Error('Each factor should be linear') unless ($result==1);
 
  +
$F = Formula($context,$F);
return $result;
 
  +
$G = Formula($context,$G);
  +
  +
if ( (($a*$F == $f) && ($F*$G == $f*$g)) ||
  +
(($a*$G == $f) && ($F*$G == $f*$g))
  +
)
  +
{
  +
return [1,1];
  +
} elsif (($a*$F == $f) || ($a*$G == $f)) {
  +
return [1,0];
  +
} elsif (($a*$F == $g) || ($a*$G == $g)) {
  +
return [0,1];
  +
} else {
  +
return [0,0];
  +
}
   
 
}
 
}
   
 
);
 
);
 
 
</pre>
 
</pre>
 
</td>
 
</td>

Revision as of 23:37, 10 April 2010

Factored Answers


This is the PG code to check answers that require students to factor an expression into two pieces that may have a constant factor that could be moved between the pieces.

This example uses adaptive parameters and a MultiAnswer answer evaluator. For more details on these, please see AdaptiveParameters and MultiAnswerProblems.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

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

TEXT(beginproblem());

Initialization: We need to include the parserMultiAnswer.pl answer checker so we can take student answers from multiple answer blanks and check them as a whole.

Context("Numeric");

$fac1 = Compute("(2 x + 3)");
$fac2 = Compute("(8 x + 12)");

$multians = MultiAnswer($fac1,$fac2)->with(
   singleResult => 0,
   allowBlankAnswers => 1,

#  singleResult => 1,  
#  separator => " * ",
#  tex_separator => " \cdot ",

  checker => sub {
    my $correct = shift; my $student = shift; my $ansHash = shift;
    my ($F,$G) = @{$correct};
    my ($f,$g) = @{$student};

    $ansHash->setMessage(1,"Neither factor can be constant") 
    unless $f->isFormula;
    $ansHash->setMessage(2,"Neither factor can be constant") 
    unless $g->isFormula;

    #  use an adaptive parameter 'a'
    my $context = Context()->copy;
    $context->flags->set(no_parameters=>0);
    $context->variables->add('a'=>'Parameter');
    my $a = Formula($context,'a');
    $f = Formula($context,$f);
    $g = Formula($context,$g);
    $F = Formula($context,$F);
    $G = Formula($context,$G);

    if ( (($a*$F == $f) && ($F*$G == $f*$g)) ||
         (($a*$G == $f) && ($F*$G == $f*$g))
       ) 
    {
       return [1,1];
    } elsif (($a*$F == $f) || ($a*$G == $f)) {
       return [1,0];
    } elsif (($a*$F == $g) || ($a*$G == $g)) {
       return [0,1];
    } else {
       return [0,0];
    }

  }

);

Setup: This is a standard factoring problem for a non-monic polynomial (where the leading coefficient is not 1 or -1). The MultiAnswer answer checker allows us to collect student answers from several answer blanks and perform answer evaluation on several answer blanks simultaneously. It also allows students to enter their factors into answer blanks in either order. Since it is possible to factor 16 x^2 + 48 x + 36 as (2x + 3) (8x + 12) or (4x + 6) (4x + 6), we need to use an adaptive parameter to allow both of these answers to be marked correct.

The MultiAnswer makes sure that neither factor is constant and that the product of the student's factors equals the product of the correct factors. Then, it creates a copy of the current context as a local context, and creates an adaptive parameter in this local context. The adaptive parameter will allow us to determine whether each factor in the student's answer is equal to a constant multiple of some factor of the correct answer.

Context()->texStrings;
BEGIN_TEXT
Factor the following expression.
$BR
$BR
\( 16 t^2 + 48 t + 36 = \big( \) 
\{$multians->ans_rule(10)\}
\( \big) \big( \) 
\{$multians->ans_rule(10)\}
\( \big) \)
END_TEXT
Context()->normalStrings;

Main Text: Each answer blank must be a method of the $multians object, which is why we use $multians->ans_rule(10). The big parentheses will help students understand what the format of the answer should be.

$showPartialCorrectAnswers = 1;

install_problem_grader(~~&std_problem_grader);

ANS( $multians->cmp() );

ENDDOCUMENT(); 

Answer Evaluation: Everything is as expected. We give students feedback on whether their answers are correct by using $showPartialCorrectAnswers = 1;, but withhold credit until both factors are correct by using the standard problem grader.

Problem Techniques Index