## WeBWorK Problems

### Using Context(Fraction-NoDecimal) on FormulaUpToConstant ### Using Context(Fraction-NoDecimal) on FormulaUpToConstant

by tim Payer -
Number of replies: 6

Hello, I am updating some old homework problems to include FormulaUpToConstant to prompt students to use the most general form of "+C" in their anti-derivatives.

I would like to require students to use fractions not decimals for their answer.
Also I would like that the "Correct Answer" to display fractions instead of decimals to match the students correct entry.

I attempted to use

Context("Fraction-NoDecimals");

But this resulted in a number of errors.
I have commented out the Context on line #48.
If you run the code block with this line un-commented you will see a number of complaints from webwork.
What is the correct approach to arrive at matching forms of fractions in the student's answer with the Correct Answer?

Any help is most appreciated...

## DESCRIPTION
## Determine an Indefinite Integral
## ENDDESCRIPTION

## Tagged by YJ

## DBsubject(Calculus - single variable)
## DBchapter(Integrals)
## DBsection(Indefinite integrals (without trigonometric functions))
## Date(5/26/2005)
## Updated for FormulaUpToConstant (7/2/2017)
## Institution(UVA)
## Author(Jeff Holt)
## MLT(indefinite_neg_exponent_single_term_not_logs)
## Level(2)
## TitleText1('Calculus: Early Transcendentals')
## AuthorText1('Stewart')
## EditionText1('5')
## Section1('5.4')
## Problem1('')
## TitleText2('Calculus: Early Transcendentals')
## AuthorText2('Stewart')
## EditionText2('6')
## Section2('5.4')
## Problem2('')
## KEYWORDS('calculus', 'integral', 'indefinite')
## Library/ASU-topics/setAntiderivatives/6-1-45

DOCUMENT();

"PG.pl",
"MathObjects.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGauxiliaryFunctions.pl",
"contextFraction.pl",
"parserFormulaUpToConstant.pl",
"PGML.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1; #Context("Fraction-NoDecimals");$a = random(6,18,2);
$b = random(3,5,1);$F = FormulaUpToConstant( "((1/$a)*x**(-$b+1))/(-$b+1) + C" ); BEGIN_PGML Evaluate the indefinite integral for the most general solution: [ \int \frac{dx}{[$a] x^{[$b]}} = ] [___]{$F->cmp()}{30}

END_PGML

BEGIN_SOLUTION
$BR$BR
To evaluate the indefinite integral of $$\displaystyle{ \int \frac{dx}{a x^{b}}}$$ use the anti-power rule that adds one to each exponent and divides each coefficient by the factor of each respective incremented exponent.$BR.$BR
\begin{aligned}&\\ F(x) & = \frac{1}{a}\int \frac{dx}{x^{b}} && \text{First pull the constant. }\\ F(x) & = \frac{1}{a}\int x^{-b} \, dx && \text{Convert denominator bases with negative exponents. }\\ F(x) & = \frac{1}{a} \frac{1}{1-b} x^{1-b} +C && \text{Add one to the exponent and divide by this value. }\\ F(x) & = \frac{-1}{\{a*(b-1)\}} x^{\{1-b\}} +C && \text{Combine fractions and reduce. }\\ \end{aligned}
$BR END_SOLUTION ENDDOCUMENT(); In reply to tim Payer ### Re: Using Context(Fraction-NoDecimal) on FormulaUpToConstant by Alex Jordan - The Fraction math object is only for a fraction of two numbers. Since "((1/$a)*x**(-$b+1))/(-$b+1) + C" is a Formula, not a Fraction, the special things that are done with fractions won't apply. (Even though the formula has lowercase f fractions in it.)

I think you can get what you want by turning off decimals:
http://webwork.maa.org/wiki/Real_(MathObject_Class)#Methods

and telling the parser not to reduce things like 1/4 to 0.25 when processing a Formula:

Note that this will not require students to enter "2/4 x" as "1/2 x". If that is important, then bizarroArithmetic.pl could help. ### Re: Using Context(Fraction-NoDecimal) on FormulaUpToConstant

by tim Payer -
Thanks Alex,

But unfortunately there seems to be a trade off here:

Context("Numeric");
Parser::Number::NoDecimals($context); Context()->flags->set(reduceConstants=>0); The lines above had no affect on the "Correct Answer" display. The answer is still being shown with decimals. this is because there was still a flag of "->reduce;" affixed to the end of the formula answer. It seems I must choose between one of two options: Either accept displays with un-reduced fractions, including (3/3)x or (6/4)x^2 for example. Or permit displays to reduce "(3/3)x" to "x", but then fractions that can't be reduced to integers will be displayed as decimals? I saw your suggestion for Bizzarro math but that code seems a little daunting. I will have to content myself with one of the forms above unless you see an easier way. Thanks for your help though.... ##DESCRIPTION #TYPE('word problem') # Anti-derivatives -- ##ENDDESCRIPTION ## DBsubject(Calculus - single variable) ## DBchapter(Integrals) ## DBsection(Antiderivatives (without trigonometric functions)) ## Institution(ASU) ## Author(Utah ww group) ## MLT(antideriv_poly_find_coeff_and_powers) ## Level(2) ## TitleText1('Calculus I') ## AuthorText1('Jerrold Marsden and Alan Weinstein') ## EditionText1('2') ## Section1('.') ## Problem1('') ## TitleText2('Calculus') ## AuthorText2('Dale Varberg, Edwin J. Purcell, and Steven E. Rigdon') ## EditionText2('9') ## Section2('.') ## Problem2('') ## TitleText3('Mathematical Applications') ## AuthorText3('Ronal J. Harshbarger and James J. Reynolds') ## EditionText3('8') ## Section3('.') ## Problem3('') ## TitleText4('Calculus: Early Transcendentals') ## AuthorText4('Rogawski') ## EditionText4('1') ## Section4('4.9') ## Problem4('1 2') ## TitleText5('Calculus: Early Transcendentals') ## AuthorText5('Stewart') ## EditionText5('6') ## Section5('4.9') ## Problem5('1 2') ## KEYWORDS('calculus','derivatives', 'antiderivatives', 'distance,velocity,acceleration') ## Library/Utah/AP_Calculus_I/set4_Graphing_and_Maximum-Minimum_Problems/1210set8p6 DOCUMENT(); # This should be the first executable line in the problem. loadMacros( "PG.pl", "PGbasicmacros.pl", "PGchoicemacros.pl", "PGanswermacros.pl", "PGauxiliaryFunctions.pl", "parserFormulaUpToConstant.pl", "PGML.pl", ); TEXT(beginproblem());$showPartialCorrectAnswers = 1;

Context("Numeric");
Parser::Number::NoDecimals($context); Context()->flags->set(reduceConstants=>0);$a1 = random(2,10,1);
$b1 = random(2,10,1);$c1 = random(2,10,1);
$d1 = random(1,10,1);$gca = gcd($a1,4); if ($gca ==1)
{
$ca = "\frac{$a1}{4}";
} else {
$na =$a1/$gca;$da =4/$gca; if ($da ==1)
{
$ca =$na;
} else{
$ca ="\frac{$na}{$da}"; } }$gcb = gcd($b1,3); if ($gcb ==1)
{
$cb = "\frac{$b1}{3}";
} else {
$nb =$b1/$gcb;$db =3/$gcb; if ($db ==1)
{
$cb =$nb;
} else{
$cb =Formula("$nb/$db"); } }$gcc = gcd($c1,2); if ($gcc ==1)
{
$cc = "\frac{$c1}{2}";
} else {
$nc =$c1/$gcc;$dc =2/$gcc; if ($dc ==1)
{
$cc =$nc;
} else{
$cc ="\frac{$nc}{$dc}"; } }$F = FormulaUpToConstant( "$a1/4 x**4 -$b1/3 x**3 +$c1/2 x**2 -$d1 x + C" )->reduce;
BEGIN_PGML
[ ]
Find the most general antiderivative for the function:

[f(x) = [$a1] x^3 - [$b1] x^2 + [$c1] x - [$d1]]

[F(x) = ] [___]{$F->cmp()}{35} END_PGML BEGIN_SOLUTION$BR
$BR To evaluate the indefinite integral of $$\displaystyle{ F(x) =\int \left( a1 x^3 - b1 x^2 + c1 x - d1\right) \, dx }$$ use the anti-power rule that adds one to each exponent and divides each coefficient by the factor of each respective incremented exponent.$BR
BR \begin{aligned}&\\ F(x) & = \int \left( a1 x^3 - b1 x^2 + c1 x - d1\right) \, dx && \text{Integrate by adding one to each exponent and dividing }\\ F(x) &= \frac{a1}{3+1} x^{3+1} - \frac{b1}{2+1} x^{2+1}+ \frac{c1}{2} x^2 - d1 x+ C && \text{each term by this amount. Include the constant of }+ C. \\ F(x) &= \frac{a1}{4} x^{4} - \frac{b1}{3} x^{3}+ \frac{c1}{2} x^2 - d1 x+ C && \text{Reduce denominators.}\\ F(x) &= ca x^{4} - cb x^{3}+ cc x^2 - d1 x+ C && \text{Simplify.}\\ F(x) &= F && \text{Simplify.}\\ \end{aligned}BR

$BR END_SOLUTION ENDDOCUMENT(); # This should be the last executable line in the problem. In reply to tim Payer ### Re: Using Context(Fraction-NoDecimal) on FormulaUpToConstant by Davide Cervone - It turns out that there were some errors in the Fraction context. In particular, fractions didn't have the derivative method implemented, and since that is used by FormulaUpToConstant(), that caused the error that you received. (Fractions inherited the default derivative method from the MathObject library, but it turns out that that caused fractions 0/0 to be requested, and that caused the devision-by-zero error). There was a second error as well that was causing problems with using fraction-valued formulas in answer blanks, but you didn't run into that. In any case, I've created an updated contextFraction.pl file, attached here (place it in your course's templates/macros directory), and have made a pull request so that it is included in the next release. Note that you don't need to use $F->cmp() in the PGML block; just using \$F is sufficient, as PGML will call the cmp() method automatically.

@Alex, the Fraction and Fraction-NoDecimals contexts do allow things like x^2/2, so that is no problem. It is the LimitedFraction contexts that don't. ### Re: Using Context(Fraction-NoDecimal) on FormulaUpToConstant

by Alex Jordan -
> @Alex, the Fraction and Fraction-NoDecimals contexts do allow things like x^2/2, so that is no problem. It is the LimitedFraction contexts that don't.

Sounds like I had a different misunderstanding. I thought that with Context("Fraction"), there was no actual difference with the Formula math object, compared to Context("Numeric"). But you are saying there is?

The Fraction context will tell a student if they enter "2/4" that it is not reduced (assuming the flags are set for that). Will it tell the student that the formula "2/4 x" is not reduced? ### Re: Using Context(Fraction-NoDecimal) on FormulaUpToConstant

by Davide Cervone -
I thought that with Context("Fraction"), there was no actual difference with the Formula math object, compared to Context("Numeric"). But you are saying there is?

Yes, there is a difference. When Context("Fraction") is in effect, fractions are retained as fractions and will display as fractions, even when reduceConstants is false (unless the fractions are combined with other constants). It's not much of a difference, but it one.

On the other hand, unreduced fractions do **not** produce warnings in formulas. The reason is that the warnings are produced by an answer checker post-filter that is applied to the Fraction object class. So it doesn't run for Formula objects (and would be hard to make work in that setting).

In order to make unreduced fractions report warnings in formulas, the warning message would need to be generated the the fraction is created (as part of the larger formula) rather than in a post-filter.

In some ways, this was a bad design decision, and one that affects several other objects as well (e.g., the check for parallel vectors is in a post-filter). I thought the post-filters would be a good way to do this, but it turns out it would be better to have those checks made in the objects themselves, so that equality checks in custom answer checkers, for example, could take advantage of them.

Unfortunately, at this time, there is no mechanism for forcing reduced fractions in formulas. 