## WeBWorK Problems

### Calling substitute with negative value gives incorrect value ### Calling substitute with negative value gives incorrect value

by Tim Alderson -
Number of replies: 3

The following results in an incorrect evaluation at the lower limit of integration that happens to be negative.

Not quite sure what is going on. [work around is easy, but I am puzzled]

# DESCRIPTION
#    average value of  b x^2 (x - c)  on  [a,c]  with  a < 0 < b,c
# ENDDESCRIPTION

DOCUMENT();
"freemanMacros.pl" );

Context("Numeric");

$b = random(2,5,1) ;$c  = random(2,9,1) ;
$bc =$b * $c ;$F = Formula( "$b x^3 -$bc x^2" ) ;

####    avoid centering interval at 0
do  { $a = random(-5,-1,1) ; } until ($a + $b != 0) ; TEXT(beginproblem());$showPartialCorrectAnswers = 0 ;

Context() -> texStrings;
BEGIN_TEXT
Find the average value of $$f(x) = F$$ on the interval $$[ a , c ]$$
:    \{ ans_rule() \}
END_TEXT

Context() -> normalStrings;

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

####    g(x) is antiderivative of f(x) = b x^3 - bc x^2
($pn,$pd) = reduce($b,4) ; #### pn/pd = b/4 is coefficient of x^4$G4 = Compute( "$pn* (x)^4 /$pd" ) -> reduce ;
$G4c =$G4 -> substitute( x => "$c" ) ;$G4a = $G4 -> substitute( x =>$a ) ;
$G4a2 = Compute( "$pn* ($a)^4 /$pd" ) -> reduce ;

($qn,$qd) = reduce($bc,3) ; #### qn/qd = bc/3 is coefficient of x^3$G3 = Formula( "$qn *(x)^3 /$qd" ) -> reduce ;
$G3c =$G3 -> substitute( x => "$c" ) ;$G3a = $G3 -> substitute( x => "$a" ) ;

$ans = Compute( "(($G4c - $G3c) - ($G4a - $G3a)) / ($c - $a)" ) ; ANS($ans -> cmp() );

$ca = Compute( "$c - $a" ) ; Context() -> texStrings; BEGIN_SOLUTION$SOLUTION
\begin{aligned} \hbox{avg value} & = \frac{1}{c - (a)} \int_{a}^{c} f(x) \, dx \\ & = \frac{1}{ca} \int_{a}^{c} \left( F \right) \, dx \\ & = \frac{1}{ca} \cdot \left( G4 - G3 \right) \Bigg|_{a}^{c} \\ & = \frac{1}{ca} \cdot \left( \left[ G4c - G3c \right] - \left[ G4a - G3a \right] \right) \\ & \approx ans \end{aligned}

END_SOLUTION
Context() -> normalStrings;

ENDDOCUMENT(); ### Re: Calling substitute with negative value gives incorrect value

by Danny Glin -

This appears to be a bug when using "substitute" when reduceConstants is set to 0.

I have filed a bug report at https://github.com/openwebwork/pg/issues/658. ### Re: Calling substitute with negative value gives incorrect value

by Tim Alderson -

Thank you Danny.

Sorry for not reducing to MWE.

TIm. 