WeBWorK Problems

Piecewise functions with fractional exponents

Piecewise functions with fractional exponents

by Andrew Swanson -
Number of replies: 2
I'm developing some problems for my Algebra for Calculus course. I tried to include a piecewise function with a cube root but encountered problems with the fractional exponent. The problem seems to come from the interaction of Context("Fractions") which produces the fractional exponent that I would like to have my students see (and hopefully use) and Context("PiecewiseFunctions").

In this trimmed down question (to illustrate the issue), I've created the same function "5x^(2/3) if x>2 else 5x^(4/5)" four different ways (labeled g1-g4).

Only g2 displays the function correctly with fractional exponents.

It looks like g1 receives the two functions created in Context("Fractions") as
"5x^2/3 if x>2 else 5x^4/5" - which is how BOTH g1 and g2 display if included as text.

g3 and g4 display the same - with decimal exponents.

So it seems like g2 is the best choice - it reports "5x^2/3 if x>2 else 5x^4/5" as wrong, BUT "5x^(2/3) if x>2 else 5x^(4/5)" give a warning Formulas from different contexts can't be compared.

## DBsubject(Algebra)
## DBchapter(Functions)
## DBsection(Piecewise functions)
## Institution(UWEC)
## Author(Andrew Swanson)
## MO(1)
## TitleText1()
## AuthorText3()
## EditionText3()
## Section1()
## KEYWORDS('functions','piecewise functions')

####################################
# Initialization

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"contextFraction.pl",
"contextPiecewiseFunction.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

Context("Fraction");

# Set up two functions with fractional exponents for the two regions

$f1 = Compute("5x^(2/3)");
$f2 = Compute("5x^(4/5)");

Context("PiecewiseFunction");

#These should be the same function
$g1 = PiecewiseFunction("$f1 if x>2 else $f2");
$g2 = PiecewiseFunction("x>2"=>$f1, $f2);

#As should these
$g3 = PiecewiseFunction("5x^(2/3) if x>2 else 5x^(4/5)");
$g4 = PiecewiseFunction("x>2" => "5x^(2/3)", "5x^(4/5)");

#NOTE: Piecewise functions look better as TeX equations [` `] than as calculator equations [: :] in PGML

BEGIN_PGML
Here are some piecewise functions:

[` g_1(x) = [$g1]`] written [$g1]

[` g_2(x) = [$g2]`] written [$g2]

[` g_3(x) = [$g3]`] written [$g3]

[` g_4(x) = [$g4]`] written [$g4]


Enter function [:g_1:] here: [:g_1(x)=:] [_______________________________]{$g1}

Enter function [:g_2:] here: [:g_2(x)=:] [_______________________________]{$g2}

Enter function [:g_3:] here: [:g_3(x)=:] [_______________________________]{$g3}

Enter function [:g_4:] here: [:g_4(x)=:] [_______________________________]{$g4}

END_PGML

ENDDOCUMENT();
In reply to Andrew Swanson

Re: Piecewise functions with fractional exponents

by Davide Cervone -
The Fraction context is designed to control student input rather than modify mathematical display. If you are just after having fractions displayed in a formula, it is better just to turn off the automatic reduction of constants by setting the reduceConstants flag to 0. Here is a modified version where all four expressions produce the desired output, and all four process the correct answer properly.
TEXT(beginproblem());

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

$f1 = Compute("5x^(2/3)");
$f2 = Compute("5x^(4/5)");

#These should be the same function
$g1 = PiecewiseFunction("$f1 if x>2 else $f2");
$g2 = PiecewiseFunction("x>2"=>$f1, $f2);

#As should these
$g3 = PiecewiseFunction("5x^(2/3) if x>2 else 5x^(4/5)");
$g4 = PiecewiseFunction("x>2" => "5x^(2/3)", "5x^(4/5)");

#NOTE: Piecewise functions look better as TeX equations [` `] than as calculator equations [: :] in PGML

BEGIN_PGML
Here are some piecewise functions:

[`g_1(x) = [$g1]`] written [$g1]

[`g_2(x) = [$g2]`] written [$g2]

[`g_3(x) = [$g3]`] written [$g3]

[`g_4(x) = [$g4]`] written [$g4]


Enter function [:g_1:] here: [:g_1(x)=:] [_______________________________]{$g1}

Enter function [:g_2:] here: [:g_2(x)=:] [_______________________________]{$g2}

Enter function [:g_3:] here: [:g_3(x)=:] [_______________________________]{$g3}

Enter function [:g_4:] here: [:g_4(x)=:] [_______________________________]{$g4}

END_PGML
The reduceConstantFunctions setting isn't strictly necessary in this problem, but might come up in others.

As the warning message you received suggest, it is problematic to mix contexts. In fact, it is an error in the PiecewiseFunction context to allow you to do so in your g2 (note that g1 doesn't do that because when you substitution $f1 and $f2 into the quoted string, they are converted into string form and then reprised in the new context, so the Fraction context is lost).

In any case, you don't need the fraction context to get what you want, here. Just turn off reduction of constants, and I think you will be in business.

In reply to Davide Cervone

Re: Piecewise functions with fractional exponents

by Andrew Swanson -
Thanks! I'm still pretty new to this. I was thinking there had to be a way to prevent the decimalization of exponents but hadn't found the right way yet. Now I can put cube roots back into the REAL question. big grin