PREP 2015 Question Authoring - Archived

"undefined" Subroutine "reduce" not recognized.

"undefined" Subroutine "reduce" not recognized.

by tim Payer -
Number of replies: 4
But now this is having problems in version 2.8.
Can you see a solution?
Many Thanks,
Problem8
ERROR caught by Translator while processing problem file:Payer/M105_HW_5/gravity_ball1.pg
****************
ERRORS from evaluating PG file: 
begin|||Undefined subroutine &main::reduce called at line 62 of (eval 26914) |||end
****************
------Input Read 1 ## DESCRIPTION 2 ## Rates of Change 3 ## ENDDESCRIPTION 4 5 ## Tagged by tda2d 6 7 ## DBsubject(Calculus - single variable) 8 ## DBchapter(Applications of differentiation) 9 ## DBsection(Rates of change - engineering and physics) 10 ## Institution(ASU) 11 ## Level(5) 12 ## KEYWORDS('Differentiation' 'Rates of Change') 13 14 # Library/ASU-topics/setQuadraticFunction/p4 15 # 16 # First comes some stuff that appears at the beginning of every problem 17 # 18 19 DOCUMENT(); # This should be the first executable line in the problem. 20 21 loadMacros( 22 "PG.pl", 23 "PGbasicmacros.pl", 24 "MathObjects.pl", 25 "PGanswermacros.pl", 26 "contextFraction.pl", 27 "PGcourse.pl", 28 ); 29 30 31 TEXT(beginproblem()); 32 33 # 34 # Now we do the randomization of variables, and other computations 35 # as needed for this problem. Sometimes we compute the answers here. 36 # 37 Context("Numeric"); 38 $h = random(200,400,10); 39 $b = random(10,32,2); 40 $b16 = -$b/16; 41 $bsq =$b16*$b16; 42 $b32 =-$b/32; 43 $b32p =$b/32; 44 $b32p = Compute("$b/32"); 45 $h16 = -$h/16; 46 $bt = -$bsq/4 +$h16; 47 $btp = -$bt; 48 $btpr =$btp**0.5; 49 $ta = $b/32; 50 $hmax = $h+($b**2)/64; 51 $tb = ($b+sqrt($b**2+64*$h))/32; 52 $ans3 = $btpr+$b32p; 53 $h = 32; 54 55 #Context("LimitedFraction"); 56 #$d = Compute("4 2/3"); 57 #$e = Compute("-1 1/2"); 58 #$b32p = Fraction($b,32); 59 60 Context("Fraction"); 61 62 ($br,$hr) = reduce($b,$h); 63 $frac1 =Compute("$b/$h"); 64 65 #Context("Fraction"); 66 #$b32p =$b/32; 67 #($b,32) = reduce($b,32); 68 #$frac =Compute("$m/$h"); 69 70 BEGIN_TEXT 71 A ball is thrown upward from the top of a building $h feet tall. 72 The height of the ball is described by the function 73 \( h(t) = - 16 t^2 +$b t + $h \), where \( t \) is in seconds and \( t = 0 \) 74 corresponds to the moment the ball is thrown. 75 $BR$BR 76 a) Determine for what value of \( t \) the ball reaches the maximum height and 77 determine this maximum height. 78 $BR$BR 79 \( t = \) \{ans_rule(20)\} 80 and maximum height = \{ans_rule(20)\} 81 $BR$BR 82 b) Determine when the ball reaches the ground. 83 $BR$BR 84 \( t = \) \{ans_rule(20)\} 85 END_TEXT 86 87 # 88 # Tell WeBWork how to test if answers are right. These should come in the 89 # same order as the answer blanks above. You tell WeBWork both the type of 90 # "answer evaluator" to use, and the correct answer. 91 # 92 93 ANS(num_cmp($ta)); 94 ANS(num_cmp($hmax, tolType=>"absolute",tol=>0.1)); 95 ANS(num_cmp($tb)); 96 97 98 SOLUTION(EV3(<<'END_SOLUTION')); 99 $PAR SOLUTION $PAR 100 101 To find the maximum height and the time it take to reach that height we must take the derivative of the quadratic function: \( h(t) = - 16 t^2 +$b t + $h\). By setting the derivative to zero we can solve for the critical point, `t`, the time at which the peak height occurred. Then we take the same `t` value and plug it into the original height function to determine the maximum height achieved. To determine the time when the ball reaches the ground we merely set the height function to zero and then solve for time, `t`. $BR 102 $PAR 103 ``\begin{aligned}&\\ 104 h(t) &= - 16 t^2 +$b t + $h\\ 105 h'(t) &= - 16 \left(t^2\right)' +$b t' + $h' && \text{Apply prime tics to each term. }\\ 106 h'(t) &= - 32 t +$b && \text{Apply the power rule to each term. }\\ 107 h'(t) &= - 32 t +$b = 0 && \text{Set the derivative to zero and solve for }t. \\ 108 -32 t &= - $b && \text{Isolate the term holding } t.\\ 109 t &= \frac{- $b}{-32} && \text{Divide by } -32.\\ 110 t &= $b32p && \text{The time in seconds to reach the maximum height. }\\ 111 h($frac1) &= - 16 \cdot ({$b32p})^2 +$b \cdot $b32p + $h && \text{Solve for the maximum height by inputting } h($b32p).\\ 112 h($b32p) &= $hmax && \text{The maximum height in feet. } 113 \end{aligned}`` 114 115 $BR 116 $BR 117 Now set `h(t) = 0` and then solve for `t` to find the time in seconds when the ball strikes the ground. $BR 118 $BR 119 ``\begin{aligned}&\\ 120 h(t) &= 0 \\ 121 - 16 t^2 +$b t + $h &= 0 && \text{Setting } h(t) = 0.\\ 122 \frac{-16 t^2}{-16} +\frac{$b t}{-16} + \frac{$h}{-16} &= 0 && \text{Divide each term by } -16.\\ 123 t^2 $b16 t $h16 &= 0 && \text{Prepare to complete the square. }\\ 124 t^2 $b16 t +\left(\frac{$b16}{2}\right)^2 - \left(\frac{$b16}{2}\right)^2 $h16 &= 0 && \text{Add and subtract half the middle coefficient squared.} \\ 125 t^2 $b16 t +\frac{$bsq}{4} - \frac{$bsq}{4} $h16 &= 0 && \text{Square both constants.} \\ 126 (t $b32)^2 - \frac{$bsq}{4} $h16 &= 0 && \text{Complete the square. } \\ 127 (t $b32)^2 $bt &= 0 && \text{Combine constants. } \\ 128 (t $b32)^2 &= $btp && \text{Isolate the square. } \\ 129 \sqrt{(t $b32)^2} &= \sqrt{$btp} && \text{Take the square root of both sides.} \\ 130 t $b32 &= $btpr && \text{Reduce.}\\ 131 t &= $btpr +$b32p && \text{Isolate }t.\\ 132 t &= $ans3 && \text{The time in seconds when the ball strikes the ground. } 133 \end{aligned}`` 134 $BR 135 136 137 END_SOLUTION 138 139 ENDDOCUMENT(); # This should be the last executable line in the problem.

In reply to tim Payer

Re: "undefined" Subroutine "reduce" not recognized.

by Davide Cervone -
Can you see a solution?

Sure. The reduce() function is defined in PGauxiliaryFunctions.pl, which you have not included in your loadMacros() call. I recommend loading PGstandard.pl rather than PG.pl and PGbasicmacros.pl as PGstandard.pl loads a standard set of macro files (including PG.pl, PGbasicmacros.pl, and PGauxiliaryFunctions.pl).

But it is really not necessary to use reduce() yourself, as the Function context includes code for reducing fractions already. In fact, fractions reduce themselves automatically by default. For example, if you define $f = Compute("2/4") and then insert $f into the output of your problem, you will get 1/2 not 2/4.

"But wait", you say, "the correct answer is showing up as 2/4!". That is because Compute() sets the correct answer to the string you passed it, which is the unreduced form, even though the fraction is itself reduced. This is one of the few times when not using Compute() is the correct choice. So use

    $f = Fraction("2/4");
or
    $f = Fraction(2,4);
or
   $f = Compute("2/4")->reduce();
instead. There is no need to use the plain reduce() function first.
In reply to Davide Cervone

Re: "undefined" Subroutine "reduce" not recognized.

by tim Payer -
Hello, 

I tried all variants of the Fraction code snippets with mixed results:

    $f = Fraction("2/4");   
## This works but only for a diagonal fraction bar and only with parentheses wrapped around the resulting fraction.
or
    $f = Fraction(2,4);
## This does not work. Only a comma appears between the entries. 
or
   $f = Compute("2/4")->reduce();

## Same result as given in the first line above. (Are we to place the "2/4" within the parentheses of the reduce statement?)

Is it possible to create horizontal fraction bars in the reduced quantity?
In reply to tim Payer

Re: "undefined" Subroutine "reduce" not recognized.

by Davide Cervone -
All three work fine for me. You haven't given the code that you are using for this last message, so I can't tell what you did, but it sounds like you are substituting the fractions into BEGIN_TEXT/END_TEXT without first setting the context to use TeX strings, and so the MathObjects are producing algebra strings like (1/2) rather than TeX strings \frac{1}{2}.

You should use

Context()->texStrings;
before any get block where you want $fto produce TeX code, and
Context()->normalStrings;
after it.

For example,

loadMacros(
  "contextFraction.pl",
  "PGML.pl"
);

Context("Fraction");

$f1 = Fraction("2/4");
$f2 = Fraction(2,4);
$f3 = Compute("2/4")->reduce();

#
#  Uses algebra strings (ugly)
#
BEGIN_TEXT
\(f_1 = $f1\), \(f_2 = $f2\), and \(f_3 = $f3\)
$PAR
END_TEXT

#
#  Uses TeX strings (pretty)
#
Context()->texStrings();
BEGIN_TEXT
\(f_1 = $f1\), \(f_2 = $f2\), and \(f_3 = $f3\)
END_TEXT
Context()->texStrings();

#
#  Uses TeX strings when substituted into math mode
#  and algebra strings otherwise
#
BEGIN_PGML
[`f_1 = [$f1]`], [`f_2 = [$f2]`], and [`f_3 = [$f3]`]
END_PGML
produces the results below. Note that the first row is probably what you are getting, while the other two are what I think you want.

I'm not sure about your comment that the second one produces a comma-separated list, as I don't see how that would be possible. Perhaps you used Compute() rather than Fraction() for that one?

Attachment fractions.png
In reply to Davide Cervone

Re: "undefined" Subroutine "reduce" not recognized.

by tim Payer -
Davide!

Thank You, Thank You,

That was the ticket.  Things look so much better now!

The command of 

Context()->texStrings();

made all the difference.

Tim