WeBWorK Problems

simplifying square roots

simplifying square roots

by Valerio De Angelis -
Number of replies: 8
I would like 2 sqrt(12) to be automatically simplified to 4 sqrt(3) in the solution section of this problem, even though the students may enter their answer in any form.  The current code forces the answer to be 2 sqrt(12) to avoid a decimal answer.

In fact, in other similar problems, I would also like to be able to have the option  to force the students to enter their answer as a simplified square root. 

Any suggestions will be greatly appreciated.
Thanks 

DOCUMENT();       

loadMacros("PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"AnswerFormatHelp.pl",
"PGcourse.pl");

TEXT(beginproblem());

$a1=random(12,12,1);
$ans2=Compute(2*sqrt($a1));

BEGIN_PGML

Find the following limit.

[``\lim_{x\rightarrow [$a1]} \frac{\sqrt{x}-\sqrt{[$a1]}}{x-[$a1]}=``][_______]{$ans2}

END_PGML
BEGIN_PGML_SOLUTION
*SOLUTION*

[``\lim_{x\rightarrow [$a1]} \frac{\sqrt{x}-\sqrt{[$a1]}}{x-[$a1]}=2\sqrt{[$a1]}``]

END_PGML_SOLUTION

ENDDOCUMENT();      
In reply to Valerio De Angelis

Re: simplifying square roots

by Dick Lane -
Is the following forum post relevant to your task?
http://webwork.maa.org/moodle/mod/forum/discuss.php?d=3802
In reply to Dick Lane

Re: simplifying square roots

by Valerio De Angelis -
Thank you for your reply.
I think this should do what I want but unfortunately I do not know enough about perl to understand how to use it. Would you be able to show me how to use this code to get the answer to the following problem to be 1/(4 sqrt(3)) instead of 1/(2 sqrt(12)?
Thanks again

DOCUMENT();       

loadMacros("PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"AnswerFormatHelp.pl",
"PGcourse.pl",
"contextFraction.pl",
);

TEXT(beginproblem());


Context("Fraction-NoDecimals");
$a1=random(12,12,1);
Context("Numeric");
$ans2=Compute(1/(2*sqrt($a1)));


BEGIN_PGML
Find the exact value of the following limit. Do not use decimals.

[``\lim_{x\rightarrow [$a1]} \frac{\sqrt{x}-\sqrt{[$a1]}}{x-[$a1]}=``][_______]{$ans2}

END_PGML
BEGIN_PGML_SOLUTION
*SOLUTION*

[``\lim_{x\rightarrow [$a1]} \frac{\sqrt{x}-\sqrt{[$a1]}}{x-[$a1]}=\frac{1}{2\sqrt{[$a1]}}``]

END_PGML_SOLUTION

ENDDOCUMENT();   
In reply to Valerio De Angelis

Re: simplifying square roots

by Alex Jordan -
I would write it this way, building the answer first, then reverse engineering the question.


DOCUMENT();

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

TEXT(beginproblem());

$c = list_random(2,3,5,6,7,10,11); #square-free
$b = random(2,4,1);
$a = $b**2*$c;

$b2 = 2*$b;
$b2c = $b2*$c;

Context("LimitedRadical");

$ansA = Formula("1/($b2*sqrt($c))");
$ansB = Formula("sqrt($c)/$b2c");


BEGIN_PGML
Find the exact value of the following limit. Do not use decimals.

>> [``\lim_{x\to [$a]} \frac{\sqrt{x}-\sqrt{[$a]}}{x-[$a]}=``][_]{$ansB}{10} <<

END_PGML
BEGIN_PGML_SOLUTION

>> [``\begin{aligned}[t]
\lim_{x\to [$a]} \frac{\sqrt{x}-\sqrt{[$a]}}{x-[$a]}
&=\lim_{x\to [$a]} \frac{\sqrt{x}-\sqrt{[$a]}}{\left(\sqrt{x}-\sqrt{[$a]}\right)\left(\sqrt{x}+\sqrt{[$a]}\right)}\\
&=\lim_{x\to [$a]} \frac{1}{\sqrt{x}+\sqrt{[$a]}}\\
&=\frac{1}{\sqrt{[$a]}+\sqrt{[$a]}}\\
&=\frac{1}{2\sqrt{[$a]}}\\
&=[$ansA]\\
&=[$ansB]
\end{aligned}``] <<

END_PGML_SOLUTION

ENDDOCUMENT();



You could maybe do something fancier with the reduced radical answer that still has an irrational answer ($ansA). For instance you could use AnswerHints.pl to give a special message for that, or you could use parserOneOf.pl to additionally count that as correct.
In reply to Alex Jordan

Re: simplifying square roots

by Valerio De Angelis -
Thank you, this does just what I wanted. I was able to use parserOneOf.pl to allow both $ansA and $ansB, but I could not get AnswersHints.pl to work. The documentation I found online seems to be all for the older versions of Webwork (before PGML).  
In reply to Valerio De Angelis

Re: simplifying square roots

by Alex Jordan -
In what way did AnswersHints.pl fail to work? I think all the documentation for that is current.

Sometimes what I find is that I need to use the replaceMessage=>1 option, as in the final example at http://webwork.maa.org/wiki/AnswerHints#.Vomkk7fASkg.

In this case, if $ansA has already led to a message like "your answer is not completely simplified", then AnswerHints will do nothing, because it doesn't want to overwrite that message. Using replaceMessage=>1 will allow the AnswerHints message to replace any earlier feedback messages.

Maybe there should also be a replaceMessage=>2 option, which would append any answerHints messages to any already existing feedback messages.
In reply to Alex Jordan

Re: simplifying square roots

by Valerio De Angelis -
Thanks for the link. I had actually found that page earlier today but I could not understand how to use AnswerHints.pl because I do not not understand much of the older versions of WebWork. I only know how to use the answer evaluator [___]{$ans}  in PGML.  Could you show me how to use it in the problem below, for the second question (the one I had posted earlier)?
Thank you.


DOCUMENT();       

loadMacros("PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"AnswerFormatHelp.pl",
"PGcourse.pl",
"contextFraction.pl",
"parserOneOf.pl"
);

TEXT(beginproblem());


Context("Fraction-NoDecimals");
$a=random(1,9,1);

$c = list_random(2,3,5,6,7,10); #square-free
$b = random(2,4,1);
$d = $b**2*$c;

$b2 = 2*$b;
$b2c = $b2*$c;

$ansA = Formula("1/($b2*sqrt($c))");
$ansB = Formula("sqrt($c)/$b2c");
$ans = OneOf($ansA,$ansB);


$a2=random(1,9,1);
$a3=random(5,8,1);
$ans1=Fraction(1,2*$a);
$ans3=Fraction(1,2*$a2);
Context("Numeric");
$ans4=Compute(2*sqrt($a3));




BEGIN_PGML

Use algebra to find the exact value of the following limits. Do not use decimals.

[``\lim_{x\rightarrow [$a*$a]} \frac{\sqrt{x}-[$a]}{x-[$a*$a]}=``][_______]{$ans1}

[``\lim_{x\rightarrow [$d]} \frac{\sqrt{x}-\sqrt{[$d]}}{x-[$d]}=``][_______]{$ans}

[``\lim_{h\rightarrow 0} \frac{\sqrt{[$a2*$a2]+h}-[$a2]}{h}=``][_______]{$ans3}

[``\lim_{h\rightarrow 0} \frac{h}{\sqrt{[$a3]+h}-\sqrt{[$a3]}}=``][_______]{$ans4}



END_PGML
BEGIN_PGML_SOLUTION
*SOLUTION*

[``\lim_{x\rightarrow [$a*$a]} \frac{\sqrt{x}-[$a]}{x-[$a*$a]}=[$ans1]``]

[``\lim_{x\rightarrow [$d]} \frac{\sqrt{x}-\sqrt{[$d]}}{x-[$d]}=\frac{\sqrt{[$c]}}{[2*$b*$c]}}``]

[``\lim_{h\rightarrow 0} \frac{\sqrt{[$a2*$a2]+h}-[$a2]}{h}=[$ans3]``]

[``\lim_{h\rightarrow 0} \frac{h}{\sqrt{[$a3]+h}-\sqrt{[$a3]}}=2\sqrt{[$a3]}``]

END_PGML_SOLUTION

ENDDOCUMENT();       
In reply to Valerio De Angelis

Re: simplifying square roots

by Alex Jordan -
I only learned these tricks this past summer, thanks to Davide Cervone.

Load AnswerHints.pl in the loadMacros call.

Before you get to the BEGIN_PGML, create the (correctness) evaluator object, packaged up as a reference variable. ("evaluator" is my own word; Davide probably has better perl/MathObjects vocabulary for whatever this things is.)

$evaluator = $ansB->cmp()->withPostFilter(AnswerHints(
$ansA => ["Rationalize the denominator", replaceMessage=>1],
));


Inside the PGML block, pass this evaluator instead of the answer:

...[___________]{$evaluator}

Or, my new favorite thing:

...[__]{$evaluator}{20}

where the 20 will be the width of the answer blank.
In reply to Alex Jordan

Re: simplifying square roots

by Valerio De Angelis -
I wasn't quite able to make it work. If I use Context("LimitedRadical") as in your example, I find that the system will mark the answer 1/(6sqrt(2)) as incorrect and gives the message "You must simplify your answer further". It does not give the custom message in $evaluator. I then tried using Context("Numeric") and then the custom message is displayed every time, no matter how the answer is entered.

Below is the code I used.
I like  ...[__]{$evaluator}{20}   to adjust the width
Thanks


DOCUMENT();       

loadMacros("PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"AnswerFormatHelp.pl",
"PGcourse.pl",
"contextFraction.pl",
"contextLimitedRadical.pl",
"parserOneOf.pl",
"answerHints.pl"
);

TEXT(beginproblem());



$a=random(1,9,1);

$c = list_random(2,3,5,6,7,10); #square-free
$b = random(2,4,1);
$d = $b**2*$c;

$b2 = 2*$b;
$b2c = $b2*$c;
Context("LimitedRadical");
$ansA = Formula("1/($b2*sqrt($c))");
$ansB = Formula("sqrt($c)/$b2c");


$evaluator = $ansB->cmp()->withPostFilter(AnswerHints($ansA => ["Rationalize the denominator", replaceMessage=>1]));
BEGIN_PGML

Use algebra to find the exact value of the following limits. Do not use decimals.



[``\lim_{x\rightarrow [$d]} \frac{\sqrt{x}-\sqrt{[$d]}}{x-[$d]}=``][_]{$evaluator}{20}



END_PGML
BEGIN_PGML_SOLUTION
*SOLUTION*



[``\lim_{x\rightarrow [$d]} \frac{\sqrt{x}-\sqrt{[$d]}}{x-[$d]}=\frac{\sqrt{[$c]}}{[$b2c]}``]



END_PGML_SOLUTION

ENDDOCUMENT();