WeBWorK Problems

FormulaWithUnits context error?

FormulaWithUnits context error?

by tim Payer -
Number of replies: 1
I am using the units of mg/ml as part of an answer with the FormulaWithUnits macro but I am getting the following error:
1. ERROR caught by Translator while processing problem file:Payer/M105_HW_20/H20_5p4_63.pg **************** ERRORS from evaluating PG file: 'mg' is not defined in this context; see position 14 of formula at line 26 of (eval 1496) Died within main::FormulaWithUnits called at line 26 of (eval 1496)
I have the used the NumberWithUnits macro without any problems but I do not see what I have done to create this problem.

It is True that "mg" and "ml" are not on the list of "approved" units. But this should not cause the problem should it?

Thanks for any help in pointing out a glitch on my part.

Tim

# Webwork Workshop 2015 for Payer, Homework 1, Practice:
# Exercises for Average value applications: pg432, #63

DOCUMENT();
loadMacros("PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"parserNumberWithUnits.pl",
"parserFormulaWithUnits.pl");
Context("Numeric");
Context()->variables->add(t=>"Real");
Context()->variables->add(M=>"Real");
Context()->variables->add(k=>"Real");
$a =Real(random(2, 10,1));
$b =Real(random(12,48,1));
$c =Real(random(15,45,1));
$d =Real(random(55,70,1));
$c10 =Compute("$c+10");
$d10 =Compute("$d+10");
$ac =Compute("$a/$c");
$ac2 =Compute("$a/(2*$c)");
$an1 = Formula("2/3*k");
$an2 = Formula("k^3/36");
$ans1 =Compute("$an1");
$ans11 = FormulaWithUnits("$an1 mg/mL");
$ans2 =Compute("$an2");
#($ar,$cbr) = reduce($a,$cb);
#loadMacros("contextFraction.pl");
#Context("Fraction");
#Context("Fraction-NoDecimals");
#Context("LimitedFraction");


TEXT(beginproblem());
BEGIN_PGML

In certain biological models, the human body's reaction to a particular kind of medication is measured by a function of the form:
[``F(M) = \frac{1}{3}\left(kM^2 - M^3\right)``] for [`0 \leq M \leq k`]
Where [`k`] is a positive constant and [`M`] is the amount of medication absorbed in the blood in [`ng/ml`]. The sensitivity of of the body to the medication is measured by the rate [`S = F'(M)`].
* Find the amount [`M`] of the medication for which the body is most sensitive. Your answer should be in terms of [`k`].
[______________]{$ans11}{20}
_(Include [@ helpLink('units') @]* in your answer.)_
* What is the average reaction to the medication over the interval [`0 \leq M \leq k`] ?
[______________]{$an2}


END_PGML

BEGIN_PGML_SOLUTION
*SOLUTION*

To find the amount [`M`] of the medication for which the body is most sensitive consider that if the sensitivity is the derivative, [`S = F'(M)`], then we can use the critical point of the derivative to indicate what amount of medication maximizes sensitivity. Then we have only to take the derivative of [` F'(M)`] which will be [`S' = F''(M)`] and then set the result equal to zero and solve for [`M`]. It is this value of [`M`] that maximizes sensitivity.


[``\begin{aligned}&\\
F''(M) &= \frac{1}{3}\left(k \left(M^2\right)' - \left(M^3\right)' \right) && \text{Apply prime tics to each term.}\\
F''(M) &= \frac{1}{3}\left(2kM - 3M^2 \right) && \text{ Take the derivative.}\\
0 &= \frac{1}{3}\left(2kM - 3M^2 \right) && \text{ Set the derivative equal to zero.}\\
0 &= 2kM - 3M^2 && \text{ Multiply both sides by }3.\\
0 &= M(2k - 3M) && \text{ Pull the common factor. }\\
0 &= M && \text{ Disregard the trivial case of } M = 0.\\
0 &= 2k - 3M && \text{ Set the remaining quantity to zero. }\\
3M &= 2k && \text{ Isolate the term holding } M.\\
M &= \frac{2k}{3} && \text{ Solve for } M.\\
\end{aligned}``]

Then the medication level of [`M = \frac{2k}{3}`] maximizes the sensitivity.

### Part 2: Find the average reaction to the medication over the interval [`0 \leq M \leq k`] .###

To find the average reaction to the medication we must integrate the function multiplied by the reciprocal of the difference of the limits of integration. The limits will span [`0`] to [`k`].

[``\begin{aligned}&\\
Average &= \frac{1}{k - 0} \int_{0}^{k} \frac{1}{3}\left(kM^2 - M^3\right)\, dM\\
Average &= \frac{1}{3k} \int_{0}^{k} \left(kM^2 - M^3\right)\, dM && \text{ Pull the constant and reduce.}\\
Average &= \frac{1}{3k} \left(\frac{kM^3}{3} - \frac{M^4}{4}\right) \Big|_{0}^{k} && \text{ Integrate.}\\
Average &= \frac{1}{3k} \frac{12}{12} \left(\frac{kM^3}{3} - \frac{M^4}{4}\right) \Big|_{0}^{k} && \text{ Multiply by one disguised as the common denominator.}\\
Average &= \frac{1}{12 \cdot 3k} \left(\frac{12 kM^3}{3} - \frac{12 M^4}{4}\right) \Big|_{0}^{k} && \text{ Distribute to clear the denominators.}\\
Average &= \frac{1}{36k} \left(4 kM^3 - 3 M^4\right) \Big|_{0}^{k} && \text{}\\
Average &= \frac{1}{36k} \left[4 k \left(k^3 - 0^3 \right) - 3 \left(k^4 - 0^4 \right)\right] && \text{ Evaluate.}\\
Average &= \frac{1}{36k} \left[4 k \left(k^3 \right) - 3 \left(k^4 \right)\right] && \text{ Reduce.}\\
Average &= \frac{1}{36k} \left(4 k^4 - 3 k^4 \right) && \text{ }\\
Average &= \frac{1}{36k} \left(k^4 \right) && \text{Collect like terms. }\\
Average &= \frac{k^4}{36k} && \text{ }\\
Average &= \frac{k^3}{36} && \text{The average reaction in terms of }k.\\
\end{aligned}``]
END_PGML_SOLUTION

ENDDOCUMENT();
In reply to tim Payer

Re: FormulaWithUnits context error?

by Davide Cervone -
Note that units are case sensitive, so mL is not the same as ml. The unit ml is defined, but mL is not, so in your expression
$ans11 = FormulaWithUnits("$an1 mg/mL");
the mg/mL is not recognized as a unit by FormulaWithUnits, and the expression is treated as a plain formula, in which mg and mL would be variables. But these aren't defined, so it reports that error.

If you switch to

$ans11 = FormulaWithUnits("$an1 mg/ml");
that should work.