WeBWorK Problems

nicestring(); display in PGML

nicestring(); display in PGML

by tim Payer -
Number of replies: 2
I am in the process of updating my problem sets from PG to PGML and I came across a "nicestring" variable declaration that displays proper LateX form in PG but not in PGML.

Is there a smooth adjustment that can make the declared variable display in PGML? I can not seem to find much documentation on this form shown below in its original PG form:

$a = random(1,3,1);
$b = random(1,5,1);
$pol = nicestring([$a, $b], ['t^{1/2}','']);



Can you suggest any adjustments to this form that will permit it to display correctly?

The code block is shown below, Many thanks for any insight on this one.

#DESCRIPTION
# Integration
# Arc length
#ENDDESCRIPTION

## sbrummel tagged and PAID on 2-20-2004
## DBsubject(Calculus - single variable)
## DBchapter(Applications of integration)
## DBsection(Average value of a function)
## Date(6/6/2005)
## Institution(UVA)
## Author(Jeff Holt)
## Level(5)
## TitleText1('Calculus: Early Transcendentals')
## AuthorText1('Stewart')
## EditionText1('6')
## Section1('6.5')
## Problem1('1 2 4 8')
## TitleText2('Calculus: Early Transcendentals')
## AuthorText2('Rogawski')
## EditionText2('1')
## Section2('6.2')
## Problem2('57')
## TitleText3('Calculus: Early Transcendentals')
## AuthorText3('Stewart')
## EditionText3('5')
## Section3('6.5')
## Problem3('2')
## KEYWORDS('Integration', 'Physics', 'Applications','average velocity','calculus', 'integrals', 'average value', 'function', 'application')
## Library/Rochester/setIntegrals22Average/ur_in_22_1
DOCUMENT(); # This should be the first executable line in the problem.

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGML.pl",
"parserNumberWithUnits.pl",
"PGauxiliaryFunctions.pl"
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

$a = random(1,3,1);
$b = random(1,5,1);
$c = random(1,5,1);
$d = $c + random(2,6,1);
$pol = nicestring([$a, $b], ['t^{1/2}','']);
$pp = "$a*\sqrt{t} +$b";
#$pol2 = Formula("sqrt(t)");
$dc = $d-$c;
$a2 = $a*2;
$dc3 = $dc*3;
$d2 = $d*2;
$b3 = $b*3;

## Pulling common factors...
$gc = gcd($a2 ,$dc3);
if($gc ==1)
{
$n1 = $a2;
$dn1 = $dc3;
$f1 ="\frac{$a2}{$dc3}";
} else {
$n1 = $a2/$gc;
$dn1 = $dc3/$gc;
if($d1 ==1)
{
$f1 = $n1;
} else {
$f1 ="\frac{$n1}{$dn1}";
}
}

## Reducing square roots..
if($c ==1 ||$c ==4)
{
$c1 =$c*$c**0.5;
$cd1 =$c*$c**0.5;
$cr =14;
} else {
$c1 = "$c \sqrt{$c}";
$cd1 = $c*$c**0.5;
$cn=3;
}

if($d ==4 ||$d ==9)
{
$d1 =$d*$d**0.5;
$dd1 =$d*$d**0.5;
$dr =49;
} elsif ($d==8)
{
$d1 = "$d2 \sqrt{2}";
$dd1 = $d2*2**0.5;
$dp =8;
} else {
$d1 ="$d \sqrt{$d}";
$dd1 =$d*$d**0.5;
$dn =3;
}

## pulling common factors, first step of reducing the last quantity.

if($c ==4 && $d ==9 ||$c ==1 && $d ==9||$c ==1 && $d ==4)
{
$cd =$d*$d**0.5-$c*$c**0.5;
$cf =1;
$n2 =$n1*$cd;
$fra = "\frac{$n2}{$dn1}";
} elsif ($c == 4 && $d == 10|| $c == 4 && $d == 6)
{
$dh =$d/2;
$cd ="$dh \sqrt{$d}-4";
$cf =2;
$n2 =$n1*2;
$fra = "\frac{$n2}{$dn1}($dh \sqrt{$d}-4)";
} elsif ($c == 2 && $d == 8)
{
$cd = "14 \sqrt{2}";
$cf =1;
$n2 =$n1*14;
$fra = "\frac{$n2 \cdot \sqrt{2}}{$dn1}";
} elsif ($c == 4 && $d == 8)
{
$cd = "2 \sqrt{2} -1";
$cf =8;
$n2 =$n1*8;
$fra = "\frac{$n2}{$dn1}( 2 \sqrt{2} -1)";
} elsif ($c == 1 && $d == 10|| $c == 1 && $d == 7|| $c == 1 && $d == 6|| $c == 1 && $d == 5|| $c == 1 && $d == 3)
{
$cd = "$d \sqrt{$d} -1";
$cf =1;
$n2 =$n1*1;
$fra = "\frac{$n2}{$dn1}( $d \sqrt{$d} -1)";
} elsif ($c == 1 && $d == 8)
{
$cd = "$d2 \sqrt{2} -1";
$cf =1;
$n2 =$n1*1;
$fra = "\frac{$n2}{$dn1}( $d2 \sqrt{2} -1)";
} elsif ($c == 3 && $d == 9)
{
$cd = "$d- \sqrt{$c}";
$cf =3;
$n2 =$n1*3;
$fra = "\frac{$n2}{$dn1}( $d2 \sqrt{2} -1)";
} else {
$cd = " $d \sqrt{$d} -$c \sqrt{$c}";
$cf =1;
$n2 =$n1;
$fra = "\frac{$n2}{$dn1}( $d \sqrt{$d} -$c \sqrt{$c})";
}

$acf = $a2*$cf;
#$acf = $a2;
$bd3 = $b3*$dc;

$gc2 = gcd($acf ,$bd3);
if($gc2 ==1)
{
$n3 = $n2;
$acf1 = $acf;
$bd4 = $bd3;
} else {
$acf1 = $acf/$gc2;
$bd4 = $bd3/$gc2;
}

$gc3 = gcd($gc2 ,$dc3);
if($gc3 ==1)
{
$n4 = $gc2;
$d4 = $dc3;
$f4 = "\frac{$n4}{$d4}";
} else {
$n4 = $gc2/$gc3;
$d4 = $dc3/$gc3;
$f4 = "\frac{$n4}{$d4}";
}



## Second step of reduction on the last quantity...

if($c ==4 && $d ==9 ||$c ==1 && $d ==9||$c ==1 && $d ==4)
{
$acf4 =$acf1*$cd;
$cd1="$acf4 +$bd4";
$cd2=$acf4+$bd4;
$n5 =$n4*$cd2;
$cd3 = "\frac{$n5}{$d4}";
} elsif ($c == 4 && $d == 10|| $c == 4 && $d == 6)
{
$acf4 =$acf1*$dh;
$acf44 =$acf1*4;
$cd1="$acf4 \sqrt{$d}-$acf44 +$bd4";
$bd5 = $bd4 -$acf44;
$cd2="$acf4 \sqrt{$d}+$bd5";
$n5 =$n4*$cd2;
$cd3 = "\frac{$n4($acf4 \sqrt{$d}+$bd5)}{$d4}";
} elsif ($c == 2 && $d == 8)
{
$cd1= "14 \sqrt{2} +27";
$cd2= "14 \sqrt{2} +27";
$cd3= "\frac{14 \sqrt{2} +27}{$d4}";
} elsif ($c == 4 && $d == 8)
{
$acf4 =$acf1*2;
$acf44 =$acf1*1;
$cd1 = "16 \sqrt{2} -8 +9";
$cd2 = "16 \sqrt{2} +1";
$n5 = $n4*2;
$cd3 = "\frac{16 \sqrt{2} +1}{$d4}";
} elsif ($c == 1 && $d == 10|| $c == 1 && $d == 7|| $c == 1 && $d == 6|| $c == 1 && $d == 5|| $c == 1 && $d == 3)
{
$ad6 = $acf1*$d;
$cd1 = "$ad6 \sqrt{$d} -$acf1+$bd4";
$ad7 = $bd4-$acf1;
$cd2 = "$ad6 \sqrt{$d} +$ad7";
$cd3 = "\frac{$ad6 \sqrt{$d} +$ad7}{$d4}";
} elsif ($c == 1 && $d == 8)
{
## This condition wont be visited with current d= c+5 max.
$cd1 = "$d2 \sqrt{2} -1";
} else {
$cd1 = "$d \sqrt{$d} -$c \sqrt{$c} + $bd4";
$cd2 = "$d \sqrt{$d} -$c \sqrt{$c} + $bd4";
$cd3 = "\frac{$d \sqrt{$d} -$c \sqrt{$c} + $bd4}{$d4}";
}

$answer = 2*$a*($d**(3/2) - $c**(3/2) )/(3*($d-$c)) + $b;
$ans3 = NumberWithUnits( "$answer m/s");
BEGIN_PGML
.
A car drives down a road in such a way that its velocity (in m/s) at time
[`t`] ( in seconds)
is [`v(t) =`] [$pol]

Find the car's average velocity between
[`t = `] [$c] and [`t = `] [$d] .
Answer = [_____]{$ans3}{20}
_(Include [@ helpLink('units') @]* in your answer.)_
END_PGML



#ANS(num_cmp($answer));
$ans2 =sprintf("%0.4f",$answer);

BEGIN_SOLUTION
$BR
$BR
To find the average velocity of the car (in m/s) between
\(t = $c \) and \( t = $d \) just multiply the definite integral by the reciprocal difference of the limits of integration like so: \(\displaystyle{A(t) = \frac{1}{$d-$c} \int_{$c}^{$d} $pol \,dx }\). Then integrate term by term first and then evaluate according to the limits of integration.$BR
$BR

\(\begin{aligned}&\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$d-$c} \int_{$c}^{$d} ($pol) \,dt && \text{Set up the integral over the limits for an average value. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc} \left(\frac{$a}{\frac{1}{2}+1} t^{\frac{1}{2}+1} +$b t\right) \Big|_{$c}^{$d} && \text{Reduce and integrate. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc} \left(\frac{$a}{\frac{1}{2}+\frac{2}{2}} t^{\frac{1}{2}+\frac{2}{2}}+$b t\right) \Big|_{$c}^{$d} && \text{Find common denominators. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc} \left(\frac{$a}{\frac{3}{2}} t^{\frac{3}{2}} +$b t\right) \Big|_{$c}^{$d} && \text{Combine fractions. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc} \cdot \frac{3}{3} \left(\frac{$a2}{3} t^{\frac{3}{2}} +$b t\right) \Big|_{$c}^{$d} && \text{Multiply by } \frac{3}{3} \text{ to clear denominator coefficients.} \\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc3} \left($a2 t^{\frac{3}{2}} +$b3 t\right) \Big|_{$c}^{$d} && \text{} \\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc3} \left($a2 t \cdot t^{\frac{1}{2}} +$b3 t \right) \Big|_{$c}^{$d} && \text{Reduce improper exponent fractions. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc3} \left($a2 t \sqrt{t} +$b3 t\right) \Big|_{$c}^{$d} && \text{Convert to a square root. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc3} \left[ $a2 \left($d \sqrt{$d} -$c \sqrt{$c} \right) + $b3 \left($d -$c \right)\right] && \text{Evaluate. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc3} \left[ $a2 \left( $d1 - $c1 \right) + $b3 ($dc) \right] && \text{Reduce roots if possible. }\\
A(t) \Big|_{$c}^{$d} & = \frac{1}{$dc3} \left[ $acf \left( $cd \right) + $bd3 \right] && \text{ }\\
A(t) \Big|_{$c}^{$d} & = \frac{$gc2}{$dc3} \left[ $acf1 \left( $cd \right) + $bd4 \right] && \text{ Pull common factors.}\\
A(t) \Big|_{$c}^{$d} & = \frac{$n4}{$d4} \left( $cd1 \right) && \text{ Simplify.}\\

A(t) \Big|_{$c}^{$d} & = \frac{$n4}{$d4} \left( $cd2 \right) && \text{ }\\
A(t) \Big|_{$c}^{$d} & = $cd3 && \text{An exact value. }\\
A(t) \Big|_{$c}^{$d} & \approx $ans2 && \text{A decimal approximation. }\\
\end{aligned}\)
$BR

END_SOLUTION

ENDDOCUMENT(); # This should be the last executable line in the problem.

In reply to tim Payer

Re: nicestring(); display in PGML

by Davide Cervone -
Since your $pol variable contains TeX code, you mist insert it into a math expression in TeX. So replace
 [`v(t) =`] [$pol]
with
 [`v(t) = [$pol]`]

If what you are after is the 1/2 being a horizontal fraction rather than a vertical one, you can do that using MathObjects as well using // for division rather than /:

 [: v(t) = [$a] t^(1//2) + [$b] :]

If you want to set this up as a formula ahead of time, use

Context("Numeric")->flags->set(reduceConstants=>0);

$pol = Compute("$a t^(1//2) + $b");

BEGIN_PGML
 [`v(t) = [$pol]`]
END_PGML
You need to set reduceConstants to prevent the 1/2 from being turned into .5 instead. The only drawback is that you can't use reduce() to clean up the situation when $a is 1, as that would convert 1/2 to .5 again. I suppose it would be possible to use Fraction context to manage that, or be more sophisticated about the handling of $a
In reply to Davide Cervone

Re: nicestring(); display in PGML

by tim Payer -
Thank You Davide!

Very informative and Useful!
This was the first time I had seen the nicestring(); format.
I can see that there is always another way to display an expression in webwork.

Tim