Difference between revisions of "AlgebraicFractions"

From WeBWorK_wiki
Jump to navigation Jump to search
m
m (10 revisions: import all default namespace pages from old wiki)
(11 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
<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 shows how to format questions in which the answer is an algebraic fraction that has separate answer blanks for the numerator and denominator that are stacked on top of each other like a fraction. Stacking the answer blanks is nice formatting that simplifies how to ask students for the parts of a fraction separately. In addition, having two separate answer blanks is useful for requiring students to simplify their answer as much as possible.</em>
 
<em>This code shows how to format questions in which the answer is an algebraic fraction that has separate answer blanks for the numerator and denominator that are stacked on top of each other like a fraction. Stacking the answer blanks is nice formatting that simplifies how to ask students for the parts of a fraction separately. In addition, having two separate answer blanks is useful for requiring students to simplify their answer as much as possible.</em>
  +
</p>
  +
  +
  +
<ul type="square">
  +
<li><b>Example 1:</b> (Recommended) Algebraic fractions using MultiAnswer</li>
  +
<li><b>Example 2:</b> Algebraic fractions without using MultiAnswer</li>
  +
</ul>
  +
  +
  +
  +
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
  +
<em><b>Example 1:</b> (Recommended) Algebraic fractions using MultiAnswer</em>
 
</p>
 
</p>
   
Line 10: Line 22:
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
</p>
 
</p>
  +
  +
<table cellspacing="0" cellpadding="2" border="0">
  +
  +
<tr valign="top">
  +
<th> PG problem file </th>
  +
<th> Explanation </th>
  +
</tr>
  +
  +
<!-- Load specialized macro files section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#ddffdd;border:black 1px dashed;">
  +
<pre>
  +
DOCUMENT();
  +
  +
loadMacros(
  +
"PGstandard.pl",
  +
"MathObjects.pl",
  +
"PGunion.pl",
  +
"parserMultiAnswer.pl",
  +
"PGcourse.pl",
  +
);
  +
  +
TEXT(beginproblem());
  +
</pre>
  +
</td>
  +
<td style="background-color:#ccffcc;padding:7px;">
  +
<p>
  +
<b>Initialization:</b>
  +
We include the macros file <code>PGunion.pl</code> to be able to display the answer boxes on top of each other (as a fraction).
  +
</p>
  +
</td>
  +
</tr>
  +
  +
<!-- Setup section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#ffffdd;border:black 1px dashed;">
  +
<pre>
  +
Context("Numeric")->variables->are(y=>"Real");
  +
Context()->{error}{msg}{"Operands of '*' can't be words"} = " ";
  +
  +
$a = random(2,8,2);
  +
$b = random(3,9,2);
  +
$c = random(1,9,1);
  +
  +
while ($c == $b/$a) { $c = random(1,9,1); }
  +
  +
$fraction = "\frac{$a y}{y-$c} + \frac{$b}{$c - y} ";
  +
  +
$num = Formula("$a y - $b");
  +
$den = Formula("y - $c");
  +
  +
$numbogus = Formula("$a*y+$b");
  +
$denbogus = Formula("(y-$c)*($c-y)");
  +
  +
$multians = MultiAnswer($num, $den)->with(
  +
singleResult => 0,
  +
allowBlankAnswers => 1,
  +
checker => sub {
  +
my ( $correct, $student, $self ) = @_;
  +
my ( $f1stu, $f2stu ) = @{$student};
  +
my ( $f1, $f2 ) = @{$correct};
  +
  +
if ( ( $f1==$f1stu && $f2==$f2stu) ||
  +
(-$f1==$f1stu && -$f2==$f2stu) ) {
  +
return [1,1];
  +
} elsif ( $f1==$f1stu || -$f1==$f1stu) {
  +
return [1,0];
  +
} elsif ( ($numbogus==$f1stu || -$numbogus==$f1stu) ||
  +
($denbogus==$f2stu || -$denbogus==$f2stu) ) {
  +
$self->setMessage(1,"Find a common denominator first");
  +
$self->setMessage(2,"Find a common denominator first");
  +
return [0,0];
  +
} elsif ( $f2==$f2stu || -$f2==$f2stu ) {
  +
return [0,1];
  +
} elsif ( $f1*$f2stu==$f1stu*$f2 ) {
  +
$self->setMessage(1,"Simplify your answer further");
  +
$self->setMessage(2,"Simplify your answer further");
  +
return [0,0];
  +
} else {
  +
return [0,0];
  +
}
  +
}
  +
);
  +
  +
  +
#
  +
# Display the fraction and answer blanks nicely
  +
#
  +
Context()->texStrings;
  +
if ($displayMode eq 'TeX') {
  +
$showfraction =
  +
"\[ $fraction = ".$multians->ans_rule(10).$multians->ans_rule(10)." \]";
  +
} else {
  +
$showfraction =
  +
ColumnTable(
  +
"\( \displaystyle $fraction = \)",
  +
$multians->ans_rule(20).$BR.$HR.$multians->ans_rule(20),
  +
indent => 0, separation => 10, valign => "MIDDLE"
  +
);
  +
}
  +
Context()->normalStrings;
  +
</pre>
  +
</td>
  +
<td style="background-color:#ffffcc;padding:7px;">
  +
<p>
  +
<b>Setup:</b>
  +
We define a string <code>$fraction</code> that will be displayed in TeX mode.
  +
We define MathObjects formulas <code>$num</code> and <code>$den</code> that are the correct numerator and denominator for the answer, as well as some bogus answers <code>$numbogus</code> and <code>$denbogus</code> that result from not finding a common denominator. We use <code>MultiAnswer</code> to manipulate both student answers at the same time. In <code>$multians</code> we allow for answers to be left blank, which requires one of two things: either we disable the error message or do type checking on the students input by using <code>ref($f1) eq ref($f1stu)</code> to see if the correct numerator <code>$f1</code> and the student numerator <code>$f1stu</code> have the same type. We used the code <code>Context()->{error}{msg}{"Operands of '*' can't be words"} = " ";</code> to disable the error message because this method allows the "Simplify your answer" feature to work more reliably. We also allow for the student to enter the fraction as either <code>(6y-3)/(y-2)</code> or <code>(3-6y)/(2-y)</code>, since both are correct and it is not clear that one is preferable to the other, which requires that we check <code>$f1==$f1stu || -$f1==$f1stu</code>. Here <code>||</code> is perl's "or" operator. We provide some custom answer hints by testing for bogus numerators and denominators and displaying answer messages via <code>$self->setMessage(1,"Simplify your answer further");</code>, where the 1 stands for the first answer blank.
  +
</p>
  +
<p>
  +
We define a mode-dependent string <code>$showfraction</code> that will be the nicely formatted fraction and answer blanks. Notice that each answer rule must be a method of the <code>$multians</code> object via the code <code>$multians->ans_rule(20)</code>. To display the fraction nicely in TeX mode, we use displaystyle math <code>\[ ... \]</code> and append two concatenated answer blanks to the string <code>$fraction</code>. In other modes (such as html), we use a <code>ColumnTable</code> from <code>PGunion.pl</code> macros to display the answer blanks as a fraction.
  +
</p>
  +
<p>
  +
To get fractions that have a large font size, be sure to use the LaTeX command <code>\( \displaystyle \frac{a}{b} \)</code>.
  +
For fractions over fractions, to keep the font size large use the LaTeX commands
  +
<pre>
  +
\(
  +
\displaystyle\frac{
  +
\displaystyle\frac{a}{b}
  +
}{
  +
\displaystyle\frac{c}{d}
  +
}
  +
\)
  +
</pre>
  +
</p>
  +
</td>
  +
</tr>
  +
  +
<!-- Question text section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#ffdddd;border:black 1px dashed;">
  +
<pre>
  +
Context()->texStrings;
  +
BEGIN_TEXT
  +
Perform the indicated operations.
  +
Express your answer in reduced form.
  +
$BR
  +
$BR
  +
$BCENTER
  +
$showfraction
  +
$ECENTER
  +
END_TEXT
  +
Context()->normalStrings;
  +
</pre>
  +
<td style="background-color:#ffcccc;padding:7px;">
  +
<p>
  +
<b>Main Text:</b>
  +
Everything is as usual. Insert the fraction and answer blanks using <code>$showfraction</code>.
  +
</p>
  +
</td>
  +
</tr>
  +
  +
<!-- Answer section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#eeddff;border:black 1px dashed;">
  +
<pre>
  +
$showPartialCorrectAnswers = 1;
  +
  +
install_problem_grader(~~&std_problem_grader);
  +
  +
ANS( $multians->cmp() );
  +
  +
ENDDOCUMENT();
  +
</pre>
  +
<td style="background-color:#eeccff;padding:7px;">
  +
<p>
  +
<b>Answer Evaluation:</b>
  +
We want to give students feedback on whether their numerator and denominator are correct, so we set <code>$showPartialCorrectAnswers = 1;</code>. If you want to withhold credit until both answer blanks are correct, use the standard problem grader, otherwise omit it to use the default (average problem grader).
  +
</p>
  +
</td>
  +
</tr>
  +
</table>
  +
  +
<p style="text-align:center;">
  +
[[IndexOfProblemTechniques|Problem Techniques Index]]
  +
</p>
  +
  +
[[Category:Problem Techniques]]
  +
  +
  +
  +
  +
  +
  +
  +
  +
  +
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
  +
<em><b>Example 2:</b> Algebraic fractions without using MultiAnswer</em>
  +
</p>
  +
   
 
<table cellspacing="0" cellpadding="2" border="0">
 
<table cellspacing="0" cellpadding="2" border="0">
Line 29: Line 236:
 
"PGunion.pl",
 
"PGunion.pl",
 
"MathObjects.pl",
 
"MathObjects.pl",
  +
"answerHints.pl",
 
"PGcourse.pl",
 
"PGcourse.pl",
 
);
 
);
Line 59: Line 267:
 
# Display the fraction and answer blanks nicely
 
# Display the fraction and answer blanks nicely
 
#
 
#
  +
Context()->texStrings;
 
if ($displayMode eq 'TeX') {
 
if ($displayMode eq 'TeX') {
 
$showfraction =
 
$showfraction =
Line 70: Line 279:
 
);
 
);
 
}
 
}
 
  +
Context()->normalStrings;
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 104: Line 313:
 
Context()->texStrings;
 
Context()->texStrings;
 
BEGIN_TEXT
 
BEGIN_TEXT
 
 
Calculate the indicated derivative.
 
Calculate the indicated derivative.
 
Simplify your answer as much as possible.
 
Simplify your answer as much as possible.
Line 112: Line 320:
 
$showfraction
 
$showfraction
 
$ECENTER
 
$ECENTER
 
 
END_TEXT
 
END_TEXT
 
Context()->normalStrings;
 
Context()->normalStrings;
Line 133: Line 340:
 
install_problem_grader(~~&std_problem_grader);
 
install_problem_grader(~~&std_problem_grader);
   
ANS($num->cmp);
+
ANS( $num->cmp()
ANS($den->cmp);
+
->withPostFilter(AnswerHints(
  +
Formula("-2x(x^2-4)+4(x^2+4)(x^2-4)") =>
  +
"Simplify your answer further.",
  +
))
  +
);
  +
ANS( $den->cmp()
  +
->withPostFilter(AnswerHints(
  +
Formula("(x^2-4)^4") =>
  +
"Simplify your answer further.",
  +
))
  +
);
   
 
ENDDOCUMENT();
 
ENDDOCUMENT();
Line 142: Line 349:
 
<b>Answer Evaluation:</b>
 
<b>Answer Evaluation:</b>
 
If you want to give students feedback on whether their numerator and denominator are correct, set <code>$showPartialCorrectAnswers = 1;</code>, otherwise set it to 0 to withhold feedback. If you want to withhold credit until both answer blanks are correct, use the standard problem grader, otherwise omit it to use the default (average problem grader).
 
If you want to give students feedback on whether their numerator and denominator are correct, set <code>$showPartialCorrectAnswers = 1;</code>, otherwise set it to 0 to withhold feedback. If you want to withhold credit until both answer blanks are correct, use the standard problem grader, otherwise omit it to use the default (average problem grader).
  +
</p>
  +
<p>
  +
We added custom answer hints provided by <code>answerHints.pl</code> to let the student know when they have a correct answer that can be simplified. Alternatively, we could have used <code>parserMultiAnswer.pl</code> instead, but it would have involved writing even more lines of code.
 
</p>
 
</p>
 
</td>
 
</td>

Revision as of 16:19, 14 May 2010

Algebraic Fractions in Student Answers


This code shows how to format questions in which the answer is an algebraic fraction that has separate answer blanks for the numerator and denominator that are stacked on top of each other like a fraction. Stacking the answer blanks is nice formatting that simplifies how to ask students for the parts of a fraction separately. In addition, having two separate answer blanks is useful for requiring students to simplify their answer as much as possible.


  • Example 1: (Recommended) Algebraic fractions using MultiAnswer
  • Example 2: Algebraic fractions without using MultiAnswer


Example 1: (Recommended) Algebraic fractions using MultiAnswer

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

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

TEXT(beginproblem());

Initialization: We include the macros file PGunion.pl to be able to display the answer boxes on top of each other (as a fraction).

Context("Numeric")->variables->are(y=>"Real");
Context()->{error}{msg}{"Operands of '*' can't be words"} = " ";

$a = random(2,8,2);
$b = random(3,9,2);
$c = random(1,9,1);

while ($c == $b/$a) { $c = random(1,9,1); } 

$fraction = "\frac{$a y}{y-$c} + \frac{$b}{$c - y} ";

$num = Formula("$a y - $b");
$den = Formula("y - $c");

$numbogus = Formula("$a*y+$b");
$denbogus = Formula("(y-$c)*($c-y)");

$multians = MultiAnswer($num, $den)->with(
  singleResult => 0,
  allowBlankAnswers => 1,
  checker => sub {
      my ( $correct, $student, $self ) = @_;
      my ( $f1stu, $f2stu ) = @{$student};
      my ( $f1, $f2 ) = @{$correct};
 
      if ( ( $f1==$f1stu &&  $f2==$f2stu) || 
           (-$f1==$f1stu && -$f2==$f2stu) ) {
          return [1,1];
      } elsif ( $f1==$f1stu || -$f1==$f1stu) {
          return [1,0];
      } elsif ( ($numbogus==$f1stu || -$numbogus==$f1stu) ||
                ($denbogus==$f2stu || -$denbogus==$f2stu) ) {
          $self->setMessage(1,"Find a common denominator first");
          $self->setMessage(2,"Find a common denominator first");
          return [0,0];
      } elsif ( $f2==$f2stu || -$f2==$f2stu ) {
          return [0,1];
      } elsif ( $f1*$f2stu==$f1stu*$f2 ) {
          $self->setMessage(1,"Simplify your answer further");
          $self->setMessage(2,"Simplify your answer further");
          return [0,0];
      } else {
          return [0,0];
      }
  }
);


# 
#  Display the fraction and answer blanks nicely
#
Context()->texStrings;
if ($displayMode eq 'TeX') {
  $showfraction =
  "\[ $fraction = ".$multians->ans_rule(10).$multians->ans_rule(10)." \]";
} else {
  $showfraction =
  ColumnTable(
  "\( \displaystyle $fraction = \)",
  $multians->ans_rule(20).$BR.$HR.$multians->ans_rule(20),
  indent => 0, separation => 10, valign => "MIDDLE"
  );
}
Context()->normalStrings;

Setup: We define a string $fraction that will be displayed in TeX mode. We define MathObjects formulas $num and $den that are the correct numerator and denominator for the answer, as well as some bogus answers $numbogus and $denbogus that result from not finding a common denominator. We use MultiAnswer to manipulate both student answers at the same time. In $multians we allow for answers to be left blank, which requires one of two things: either we disable the error message or do type checking on the students input by using ref($f1) eq ref($f1stu) to see if the correct numerator $f1 and the student numerator $f1stu have the same type. We used the code Context()->{error}{msg}{"Operands of '*' can't be words"} = " "; to disable the error message because this method allows the "Simplify your answer" feature to work more reliably. We also allow for the student to enter the fraction as either (6y-3)/(y-2) or (3-6y)/(2-y), since both are correct and it is not clear that one is preferable to the other, which requires that we check $f1==$f1stu || -$f1==$f1stu. Here || is perl's "or" operator. We provide some custom answer hints by testing for bogus numerators and denominators and displaying answer messages via $self->setMessage(1,"Simplify your answer further");, where the 1 stands for the first answer blank.

We define a mode-dependent string $showfraction that will be the nicely formatted fraction and answer blanks. Notice that each answer rule must be a method of the $multians object via the code $multians->ans_rule(20). To display the fraction nicely in TeX mode, we use displaystyle math \[ ... \] and append two concatenated answer blanks to the string $fraction. In other modes (such as html), we use a ColumnTable from PGunion.pl macros to display the answer blanks as a fraction.

To get fractions that have a large font size, be sure to use the LaTeX command \( \displaystyle \frac{a}{b} \). For fractions over fractions, to keep the font size large use the LaTeX commands

\( 
\displaystyle\frac{ 
  \displaystyle\frac{a}{b} 
  }{ 
  \displaystyle\frac{c}{d} 
} 
\)

Context()->texStrings;
BEGIN_TEXT
Perform the indicated operations.
Express your answer in reduced form.
$BR
$BR
$BCENTER
$showfraction
$ECENTER
END_TEXT
Context()->normalStrings;

Main Text: Everything is as usual. Insert the fraction and answer blanks using $showfraction.

$showPartialCorrectAnswers = 1;

install_problem_grader(~~&std_problem_grader);

ANS( $multians->cmp() );

ENDDOCUMENT();

Answer Evaluation: We want to give students feedback on whether their numerator and denominator are correct, so we set $showPartialCorrectAnswers = 1;. If you want to withhold credit until both answer blanks are correct, use the standard problem grader, otherwise omit it to use the default (average problem grader).

Problem Techniques Index





Example 2: Algebraic fractions without using MultiAnswer


PG problem file Explanation
DOCUMENT();

loadMacros(
"PGstandard.pl",
"PGunion.pl",
"MathObjects.pl",
"answerHints.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

Initialization: We include the macros file PGunion.pl to be able to display the answer boxes on top of each other (as a fraction).

Context("Numeric");


$fraction = "\frac{d}{dx} \left( \frac{-(x^2+4)}{(x^2-4)^2} \right)";

$num = Formula("2 * x * (x**2 + 12)")->reduce;
$den = Formula("(x**2 - 4)**3")->reduce;

# 
#  Display the fraction and answer blanks nicely
#
Context()->texStrings;
if ($displayMode eq 'TeX') {
  $showfraction =
  "\[ $fraction = ".ans_rule(10).ans_rule(10)." \]";
} else {
  $showfraction =
  ColumnTable(
  "\( \displaystyle $fraction = \)",
  ans_rule(20).$BR.$HR.ans_rule(20),
  indent => 0, separation => 10, valign => "MIDDLE"
  );
}
Context()->normalStrings;

Setup: We define a string $fraction that will be displayed in TeX mode. We define MathObjects formulas $num and $den that are the correct numerator and denominator for the answer.

We define a mode-dependent string $showfraction that will be the nicely formatted fraction and answer blanks. To display the fraction nicely in TeX mode, we use displaystyle math \[ ... \] and append two concatenated answer blanks to the string $fraction. In other modes (such as html), we use a ColumnTable from PGunion.pl macros to display the answer blanks as a fraction.

To get fractions that have a large font size, be sure to use the LaTeX command \( \displaystyle \frac{a}{b} \). For fractions over fractions, to keep the font size large use the LaTeX commands

\( 
\displaystyle\frac{ 
  \displaystyle\frac{a}{b} 
  }{ 
  \displaystyle\frac{c}{d} 
} 
\)

Context()->texStrings;
BEGIN_TEXT
Calculate the indicated derivative.
Simplify your answer as much as possible.
$BR
$BR
$BCENTER
$showfraction
$ECENTER
END_TEXT
Context()->normalStrings;

Main Text: Everything is as usual. Insert the fraction and answer blanks using $showfraction.

$showPartialCorrectAnswers = 1;

install_problem_grader(~~&std_problem_grader);

ANS( $num->cmp() 
->withPostFilter(AnswerHints(
  Formula("-2x(x^2-4)+4(x^2+4)(x^2-4)") => 
  "Simplify your answer further.",
))
);
ANS( $den->cmp()
->withPostFilter(AnswerHints(
  Formula("(x^2-4)^4") => 
  "Simplify your answer further.",
))
);

ENDDOCUMENT();

Answer Evaluation: If you want to give students feedback on whether their numerator and denominator are correct, set $showPartialCorrectAnswers = 1;, otherwise set it to 0 to withhold feedback. If you want to withhold credit until both answer blanks are correct, use the standard problem grader, otherwise omit it to use the default (average problem grader).

We added custom answer hints provided by answerHints.pl to let the student know when they have a correct answer that can be simplified. Alternatively, we could have used parserMultiAnswer.pl instead, but it would have involved writing even more lines of code.

Problem Techniques Index