WeBWorK Problems

Keeping Recognized functions from evaluating

Keeping Recognized functions from evaluating

by Gregory Varner -
Number of replies: 4

I am writing exponential equation problems. While an approximate answer is fine in many cases, I am also wanting to force a correct answer at time. For instance, I may want the students to give the answer in the form: ln(5)/7 or (3ln(3)-4ln(2))/(2ln(2)-5ln(3)) instead of as the decimal.

I have a solution that is easily adaptable to a wide array of such problems, but am struggling to get the solutions to output the way I want (it is giving the decimal wonderfully). I could force the students to not use a decimal, but I want the solution output to match the answer they put in.


Here is what I have:

DOCUMENT();      


loadMacros(

   "PGstandard.pl",     # Standard macros for PG language

   "MathObjects.pl",

   "PGML.pl",

  "answerHints.pl",

  "PGML.pl",

  "PGcourse.pl",

);


# Print problem number and point value (weight) for the problem

TEXT(beginproblem());


# Show which answers are correct and which ones are incorrect

$showPartialCorrectAnswers = 1;


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

#

#  Setup

#

#


$base1 = e;

$base2 = 5;

$a = 7;

$b = 0;

$power1 = Formula("$a x+$b")->reduce;

$c = 0;

$d = 1;

$power2 = Formula("$c x+$d")->reduce;


Context("Numeric");

##y is base1, z is base2

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

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


$num = Formula("$d ln(z)-$b ln(y)")->reduce;

$den = Formula("$a ln(y)-$c ln(z)")->reduce;


$ans = Formula("$num/$den");

$ans=$ans->substitute(y=>$base1)->substitute(z=>$base2);

ANS($ans->cmp());


Any help would be appreciated.

Thank you!


In reply to Gregory Varner

Re: Keeping Recognized functions from evaluating

by Alex Jordan -

There is a context flag called "reduceConstantFunctions" which controls whether a function applied to a constant in a Formula is reduced to the output. For example, whether "ln(5)" is turned into 1.609...

I think you want:

Context("Numeric")->flags->set(reduceConstantFunctions=>0);
Parser::Number::NoDecimals();

then you can do things like:

$ans = Formula("($d ln(5)-$b)/($a-$c ln(5))");

The correct answer will display as desired, and the NoDecimals line will tell students that decimals are not allowed should they try to enter a decimal in an answer.

In reply to Alex Jordan

Re: Keeping Recognized functions from evaluating

by Gregory Varner -
That worked but created a different issue. While I can manually fix it in this part, would here be a way to selectively enfore the non-reducing?

For instance, this creates the solution: (1ln(5)-0ln(e))/(7ln(e)-0ln(5)) when I really want it to output to ln(5)/7.

I imagine that it should be possible to get it to simplify the multiplications without too much trouble (though my attempts keep getting an error of decimals not allowed at the $ans step), but I would like it to simplify ln(e) but not ln(5).

Thanks
In reply to Gregory Varner

Re: Keeping Recognized functions from evaluating

by Alex Jordan -

WeBWorK is not a CAS, and I'm not aware of anything it has that would automatically reduce "ln(e)" to "1" but leave "ln(5)" alone. The parser can see "ln" is a function and "e" or "5" are inputs, but it does not have special information about the nature of the "ln" function to do that next thing.

You could combine what I posted previously with your approach of using substitution and also with using cases to handle "1" and "e" as inputs to ln(). So for example if you want to end up with:

($a ln($p)+ $b ln($q))/($c ln($r) + $d ln($s))

Then you could do:
Context()->variables->add(p=>'Real',q=>'Real',r=>'Real',s=>'Real');
$answer = Formula("($a p + $b q) / ($c r + $d s)");
if ($p == 1) {$answer = $answer->substitute(p=>Formula("0"))}
if ($p == e) {$answer = $answer->substitute(p=>Formula("1"))}
# repeat for q,r,s
$answer = $answer->reduce;  #handle any 0s and 1s at this point
$answer = $answer->substitute(p=>Formula("ln($p)"),q=>Formula("ln($q)"),r=>Formula("ln($r)"),s=>Formula("ln($s)"));

In reply to Alex Jordan

Re: Keeping Recognized functions from evaluating

by Gregory Varner -

Thank you very much! That worked great. (Though it took me longer than I would like to admit to implement it.)