Difference between revisions of "FactoringAndExpanding"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Your title here: PG Code Snippet</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>Thi...)
 
Line 1: Line 1:
<h2>Your title here: PG Code Snippet</h2>
 
  +
<h2>Factored Answers</h2>
   
 
<!-- Header for these sections -- no modification needed -->
 
<!-- Header for these sections -- no modification needed -->
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>This code snippet shows the essential PG code to check student answers that are equations. Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
+
<em>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 from one piece to another.</em>
 
</p>
 
</p>
   
Line 23: Line 23:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("any macros files that are needed");
 
  +
DOCUMENT();
  +
  +
loadMacros(
  +
"PGstandard.pl",
  +
"MathObjects.pl",
  +
"parserMultiAnswer.pl",
  +
);
  +
  +
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 29: Line 37:
 
<p>
 
<p>
 
<b>Initialization:</b>
 
<b>Initialization:</b>
To do ..(what you are doing)........., we don't have to change the
 
  +
We need to include the <code>parserMultiAnswer.pl</code> answer checker so we can take student answers from multiple answer blanks and check them as a whole.
tagging and documentation section of the problem file.
 
In the initialization section, we need to include the macros file <code>-------.pl</code>.
 
 
</p>
 
</p>
 
</td>
 
</td>
Line 41: Line 47:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context(".....");
+
Context("Numeric");
Define context and variables for the questions
+
  +
$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 $self = shift;
  +
my ($F,$G) = @{$correct};
  +
my ($f,$g) = @{$student};
  +
  +
Value::Error('Neither factor can be constant')
  +
unless $f->isFormula && $g->isFormula;
  +
  +
Value::Error('Your product does not equal the original (it is incomplete)')
  +
unless $F*$G == $f*$g;
  +
# return 0 unless $F*$G == $f*$g;
  +
  +
# 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);
  +
my $result = ($a*$F == $f || $a*$G == $f);
  +
Value::Error('Factor as the product of two linear functions')
  +
unless ($result == 1);
  +
return $result;
  +
  +
}
  +
  +
);
   
$expr = Formula("....");
 
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 50: Line 55:
 
<p>
 
<p>
 
<b>Setup:</b>
 
<b>Setup:</b>
We specify that the Context should be <code>......</code>, and define the answer to be a formula.
 
  +
This is a standard factoring problem for a non-monic polynomial (where the leading coefficient is not 1 or -1). The <code>MultiAnswer</code> answer checker allows us to collect student answers from several answer blanks and perform answer evaluation on several answer blanks simultaneously. Since it is possible to factor <code>16 x^2 + 48 x + 36</code> as <code>(2x + 3) (8x + 12)</code> or <code>(4x + 6) (4x + 6)</code>, we need to use an adaptive parameter to allow both of these answers to be marked correct.
 
</p>
 
</p>
<p>
+
<p>
Notes: on using this and related Contexts.
+
The <code>MultiAnswer</code> 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.
 
</p>
 
</p>
 
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 64: Line 68:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
Context()->texStrings;
 
BEGIN_TEXT
 
BEGIN_TEXT
...... question 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) \)
  +
\{ AnswerFormatHelp("formulas") \}
 
END_TEXT
 
END_TEXT
  +
Context()->normalStrings;
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
The problem text section of the file is as we'd expect.
 
  +
Each answer blank must be a method of the <code>$multians</code> object, which is why we use <code>$multians->ans_rule(10)</code>. The big parentheses will help students understand what the format of the answer should be.
 
</p>
 
</p>
 
</td>
 
</td>
Line 81: Line 95:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( $expr->cmp() );
 
  +
$showPartialCorrectAnswers = 1;
  +
  +
ANS( $multians->cmp() );
  +
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
 
<b>Answer Evaluation:</b>
 
<b>Answer Evaluation:</b>
As is the answer.
 
  +
Everything is as expected.
 
</p>
 
</p>
 
</td>
 
</td>
Line 97: Line 115:
   
 
[[Category:Problem Techniques]]
 
[[Category:Problem Techniques]]
  +
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/doc/cvs/pg_CURRENT/macros/parserMultiAnswer.pl.html parserMultiAnswer.pl.html]</li>
  +
<li>PG macro: [http://cvs.webwork.rochester.edu/viewcvs.cgi/pg/macros/parserMultiAnswer.pl parserMultiAnswer.pl]</li>
  +
</ul>

Revision as of 22:59, 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 from one piece to another.

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 $self = shift;
    my ($F,$G) = @{$correct};
    my ($f,$g) = @{$student};

    Value::Error('Neither factor can be constant')
    unless $f->isFormula && $g->isFormula;

    Value::Error('Your product does not equal the original (it is incomplete)') 
    unless $F*$G == $f*$g;
#    return 0 unless $F*$G == $f*$g;

    #  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);
    my $result = ($a*$F == $f || $a*$G == $f);
    Value::Error('Factor as the product of two linear functions') 
    unless ($result == 1);
    return $result;

  }

);

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. 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) \)
\{ AnswerFormatHelp("formulas") \}
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;

ANS( $multians->cmp() );

ENDDOCUMENT(); 

Answer Evaluation: Everything is as expected.

Problem Techniques Index