WeBWorK Main Forum

Issues while factoring with the quadratic formula

Issues while factoring with the quadratic formula

by Jonathan Desaulniers -
Number of replies: 2

Hi!

I'm having an issue with a question that ask the students to factorize a polynomial expression using the quadratic formula. The thing is that the expression can be factorize in the forme of (ax + b)^2 and it work with the contextPolynomialFactors. But, it doesn't work if the answer is in the forme of a^2(x+b/a)^2. This forme is obtained if the student use the quadratic formula. Since it is a correct answer, i'm trying to find a workaround to force the answer to be accepted. Any idea?

Here is the code i use:

DOCUMENT();


loadMacros(

"PGstandard.pl",

"MathObjects.pl",

"PGcourse.pl",

"contextPolynomialFactors.pl",

"contextLimitedPowers.pl",

);


TEXT(beginproblem());



#######################################

#  Set-up


#Context("PolynomialFactors");

Context("PolynomialFactors-Strict");

Context()->flags->set(singleFactors=>0);

Context()->{error}{msg}{"Each factor can appear only once (combine like factors)"} 

 = "Combiner les facteurs communs en un seul facteur";



#(ax+b)^2 = a^2 x^2 +2ab x + b^2  où b < 0

$var = "x";

do {

    $a = random(2,4,1);

    $b = random(-10,-2,1);

    }

until (gcd($a,$b)==1);



$signe = "négative";

$signe1 = "positive";

$coef1 = $a * $a;

$coef2 = 2*$a*$b;

$coef3 = $b * $b;

$s = $coef2;

$p = $coef1 * $coef3;

$ab = $a * $b;

$abs = abs($b);

$racine1 = $b;

$racine2 = "$a $var";

$a2 = $a*$a;


$terme1 = "$coef1 $var^2";

$terme2 = "$coef2 $var";

$terme3 = "$coef3";

$expression = Formula("$terme1 + $terme2 + $terme3")->reduce;

@terme = ("\textcolor{blue}{$terme1}", 

            "\textcolor{red}{$terme2}", 

            "\textcolor{blue}{$terme3}",

            "\textcolor{blue}{$terme3 \ \cdot \ $terme1}",

            "\textcolor{red}{$ab $var}",

            "$racine2($racine2+$racine1)",

            "$racine1($racine2+$racine1)",

            "(\textcolor{blue}{$racine2}+\textcolor{blue}{$racine1})");

@coef = ("\textcolor{red}{$s}", 

        "\textcolor{blue}{$p}",

        "\textcolor{red}{$terme2}");

       

$answer = Compute("($racine2 + $racine1)^2")->reduce;



################################################

#  Main text


Context()->texStrings;


BEGIN_TEXT


Factorise le trinôme suivant. $BR

$PAR

$BCENTER

\($expression \quad = \quad \) \{ ans_rule(30) \} <button type="button" class="btn btn-link" data-toggle="collapse" data-target="#demo">

<b> Notation </b>

</button>

<br> <br>

<div id="demo" class="collapse">

<table width="60%" border="1" cellspacing="0">

  <tr class="btn-primary">

    <td valign="top" align="left"  width="100%">

      <text style="color:white;padding-left:15px;">Notation de la réponse</text>

    </td>

  </tr>

    <tr style="background-color:#fdfdfe;">

      <td colspan="2"style="color:black;padding:15px;">

          <p align="left">


<u>Exemple</u> : Pour écrire \( x^2 \), vous devez inscrire <b> x^2</b>.

         </p>         

      </td>

    </tr>

</table>

</div>  

$ECENTER  

    

END_TEXT


Context()->normalStrings;



################################################

#  Answer evaluation


$showPartialCorrectAnswers = 1;


ANS($answer->cmp(

checker => sub {

        my ($correct,$student,$ans) = @_; 

    $answer1 = Compute("(-$racine2 - $racine1)^2")->reduce;

    $answer2 = Compute("($racine1 + $racine2)^2")->reduce;

    $answer3 = Compute("(-$racine1 - $racine2)^2")->reduce;

    $answer4 = Compute("$a2($racine1/$a + $racine2/$a)^2")->reduce;

    if($student == $correct) {

    if ($student eq $expression) {

       return 0;

     } elsif ($student eq $answer || $student eq $answer1 || $student eq $answer2 || $student eq $answer3 || $student eq $answer4){

       return 1;

     } else {

     return 0;

     }

  } else {

  return 0;

}

}

));


Thanks!

In reply to Jonathan Desaulniers

Re: Issues while factoring with the quadratic formula

by Alex Jordan -

Here is some code you can adapt for this. First, this uses contextForm to distinguish factored from unfactored. That alone would also distinguish your two variants though, so then this uses parserOneOf to declare the two variants. But then there is an issue that is described in more detail at this other post:

https://webwork.maa.org/moodle/mod/forum/discuss.php?d=4599#p13478

So the code below takes Davide's code from that other post to address that issue.

DOCUMENT();
loadMacros(qw(
    PGstandard.pl
    PGML.pl
    contextForm.pl
    parserOneOf.pl
));
package my::OneOf;
our @ISA = ('parser::OneOf');
sub cmp_compare {
  my ($self, $other, $ans) = @_;
  for $answer (@{$self->{data}}) {
    if ($answer->typeMatch($other)) {
      my $result = $answer->cmp->evaluate($other);
      if ($result->{score}) {
        $ans->score($result->{score});
        $ans->{ans_message} = $result->{ans_message};
        return $ans->{score};
      }
      if ($result->{ans_message}) {
        $ans->{ans_message} = $result->{ans_message}
           if !$ans->{ans_message} || length($ans->{ans_message}) > length($result->{ans_message});
      }
    }
  }
  return 0;
}
package main;
sub myOneOf {my::OneOf->new(@_)}
Context("Form");
$ans = myOneOf(Formula("(2x+1)^2"), Formula("4(x+1/2)^2"));
BEGIN_PGML
[`[$ans] ={}`] [_]{$ans}{5}
END_PGML
ENDDOCUMENT();


Note that this not only accepts (2x+1)^2 and 4(x+1/2)^2, but also things like (-2x-1)^2. However it will not accept 4x^2+4x+1.


                                    
In reply to Alex Jordan

Re: Issues while factoring with the quadratic formula

by Jonathan Desaulniers -

Great, thanks a lot!!!!! 

I thought that OneOf wouldn't restrict the student from re-entering the original formula but didn't know about the typematch option!!!...il go on ahead and try that right away!!! 

Thanks again!!

Jonathan