WeBWorK Problems

Passing a root into a fraction

Passing a root into a fraction

by Gregory Varner -
Number of replies: 1

I am working on some problems using the quadratic formula.

I am wanting the students to write the answer as a reduced fraction with radicals (when applicable).

I am able to force the students to enter fractions and everything works fine when the square root simplifies to a whole number.

However, if the radical does not simplify, it will not let me pass it to the fraction. I am sure this is an easy fix, but I can not seem to figure out how to make roots and fractions work together.

Thank you:

*I realize that the code below has options for 3 different answers. I had trouble combining them into an if, elsif, and else situation with the different contexts happening.


DOCUMENT();

loadMacros(
      "PGstandard.pl",
      "MathObjects.pl",
      "parserAssignment.pl",
      "answerHints.pl",
      "PGML.pl",
      "contextLimitedRadical.pl",
      "PCCmacros.pl",
      "contextFraction.pl",
  "PGcourse.pl",
);

##############################################
   
Context("LimitedRadical");
Context()->flags->set(reduceConstants=>0, reduceConstantFunctions=>0, formatStudentAnswer=>parsed,showExtraParens=>0);
                              
parser::Assignment->Allow;

Context()->operators->redefine(',',using=>',',from=>'Numeric');
    Context()->operators->redefine('or',using=>',',from=>'Numeric');
    Context()->operators->set(
      ','=>{string=>' or ',TeX=>'\hbox{ or }'},
      'or'=>{string=>' or ',TeX=>'\hbox{ or }'}
    );
    Context()->lists->set(List => {separator => " or "});

$var = "x";

$d = 3;
$e = 2;
$f = 1;
$g = 0;
$h = 0;

$a = $d;
$b = $e-$g;
$c = $f-$h;

$discr = $b*$b - 4*$a*$c;

Context("Fraction");

$ans1 = Fraction(-$b+sqrt($discr),2*$a);
$ans2 = Fraction(-$b-sqrt($discr),2*$a);

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

TEXT(beginproblem());
BEGIN_PGML
Solve the following quadratic equations by the quadratic formula.

     [` [$d][$var]^2 +[$e][$var]+[$f]= 0 `]
    
   
The solution is

    x= [_______________________]

[@KeyboardInstructions("If needed, enter multiple answers separated by commas, as in 1, -1. Answers must be given eactly, including as fractions. If you need to use the square root symbol, as in [`x=\\sqrt{17}`], type it as [|x=sqrt(17)|]*. If there are no real solutions, enter [|no real solutions|]*.")@]**

END_PGML

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

$showPartialCorrectAnswers = 1;


#if no solution
#Context("Vector");
#Context()->strings->add("no real solutions"=>{},
 # "no real solution"=>{alias=>'no real solutions'},
  #"none"=>{alias=>'no real solutions'},
  #);
 
#$ans = String("no real solutions");
#ANS($ans->cmp());

#if one solution
#$ans = $ans1;
#ANS(num_cmp($ans));

#if 2 solution

$ans = List($ans1,$ans2);

ANS($ans->cmp());

In reply to Gregory Varner

Re: Passing a root into a fraction

by Gregory Varner -

I figured this out so I thought I would update. It was much easier than I was thinking.


I used the following snippet:

Context("Numeric");

Context()->variables->add(s=>"Real");

Context()->flags->set(reduceConstants=>0,reduceConstantFunctions=>0);


$ans1 = Formula("(-$b+sqrt(s))/(2*$a)")->reduce;

$ans1 = $ans1->substitute(s=>$discr);


Worked like a charm!