WeBWorK Problems

FormulaUpToConstant won't give helpful messages

FormulaUpToConstant won't give helpful messages

by Liping Chen -
Number of replies: 2
I have a problem with the following code:

DOCUMENT(); 

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl",
"MathObjects.pl",
"parserFormulaUpToConstant.pl",
);

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

$fx = Formula("3 x sqrt(x - 8)");
$fx->{limits} = [9,10];

$ans = FormulaUpToConstant("6/5*(x-8)^(5/2) + 16*(x-8)^(3/2)");
$ans->{limits} = [9,10];

#########################################
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
Context()->texStrings;

BEGIN_TEXT
$PAR
Evaluate the indefinite integral:
$PAR
\( \displaystyle \quad\quad \int $fx \, dx = \)
\{ ans_rule(40) \}
END_TEXT

Context()->normalStrings;
#########################################

ANS( $ans -> cmp(showHints => 1) -> withPostFilter(sub { 
  my $ans = shift;

  $ans->{ans_message} = "" if $ans->{ans_message} eq 
  "This answer is equivalent to the one you just submitted.";

  return $ans; 
}));
ENDDOCUMENT();

If I submit an answer like 6/5*(x-8)^(5/2) + 16*(x-8)^(3/2) without adding a constant C, I cannot get the helpful hint message: Your answer is not the most general solution. This is different from other anti-derivative problems, for example, if I change this line:
$ans = FormulaUpToConstant("6/5*(x-8)^(5/2) + 16*(x-8)^(3/2)");
to
$ans = FormulaUpToConstant("x");
and submit an answer x, I will get a hint: Your answer is not the most general solution. Can anyone give me some help about how to fix this problem?

As a side note, I tried to fix it by adding some scripts like:
ANS( $ans->cmp() -> withPostFilter(sub { 
  my $ans = shift;
  if(($ans->{score} == 0) && (!($ans->{ans_message}))){
    $stuAns = $ans->{original_student_ans};
$stuAns =~ s/(sqrt)/::/g;
    $stuAns =~ s/([a-zA-Z]+)/x/g;
$stuAns =~ s/::/sqrt/g;
$stuAns = Formula("$stuAns")->D();
    $ans->{ans_message} = "Your answer is not the most general solution." if ($fx == $stuAns);
  }
  $ans->{ans_message} = "" if $ans->{ans_message} eq 
  "This answer is equivalent to the one you just submitted.";

  return $ans; 
}));
but it gives me some other troubles. For example, if I submit an answer like 6/5*(8-x)^(5/2) + 16*(8-x)^(3/2)+C, I will get an warning because of this line:

$ans->{ans_message} = "Your answer is not the most general solution." if ($fx == $stuAns);

Any insight on what could be going on would be really appreciated.

Thanks,
Liping
In reply to Liping Chen

Re: FormulaUpToConstant won't give helpful messages

by Davide Cervone -
It looks like the limits that you have assigned to $ans aren't being used at one point internally in the code that checks for whether that message is needed. Try using $ans->cmp(limits=>[9,10],showHints=>1) instead, and see if that gives you the needed message.
In reply to Davide Cervone

Re: FormulaUpToConstant won't give helpful messages

by Liping Chen -
Hi Davide,

That works!!! Thanks very much for your help!

Liping