WeBWorK Problems

Combining Functions

Combining Functions

by Gregory Varner -
Number of replies: 4

I have run into an issue. I am working on authoring questions that use the algebra of functions (and later composition of functions). I currently am not as concerned about students simplifying (though I may change on that), but I want the answers that they see to be the simplified version.

For instance, perhaps I am adding/subtracting/dividing/multiplying rational functions. Is there a way to define the functions (I am currently using formula), say as f and g, and then do f+g simplified. 

Ideally this would also work for cases where there is no reasonable simplification as well like adding a polynomial and radical expression together.

What I have is included below. It accepts the simplified and unsimplified version (as they are equivalent formulas). 

Thank you!


DOCUMENT();      


loadMacros(

   "PGstandard.pl",     # Standard macros for PG language

   "MathObjects.pl",

   "PGML.pl",

   "contextLimitedPolynomial.pl",

);


# Print problem number and point value (weight) for the problem

TEXT(beginproblem());


# Show which answers are correct and which ones are incorrect

$showPartialCorrectAnswers = 1;

######

Context("Numeric");

Context()->noreduce('(-x)-y','(-x)+y');


$f = Formula("3/(x^2 -x -2)")->reduce;

$g = Formula("-5/(x^2 -4)")->reduce;

$fPlusg = Formula("$f+$g")->reduce;

$gMinusf = Formula("$g-$f")->reduce;

$fTimesg = Formula("$f*$g")->reduce;

$fByg = Formula("$f/$g")->reduce;


##############################################################

BEGIN_PGML

Consider [` f(x) = [$f] `] and [` g(x) = [$g] `].

a. What is the sum of [`f(x)`] and [`g(x)`]?  

[`(f+g)(x) = `][_____________________________]{$fPlusg}


b. What is the difference of [`f(x)`] from [`g(x)`]?  

[`(g-f)(x) = `][_____________________________]{$gMinusf}


c. What is the product of [`f(x)`] and [`g(x)`]?  

[`(fg)(x) = `][_____________________________]{$fTimesg}


d. What is the quotient of [`f(x)`] by [`g(x)`]?  

[`\left(\dfrac{f}{g}\right)(x) = `][_____________________________]{$fByg}

END_PGML

ENDDOCUMENT();        

In reply to Gregory Varner

Re: Combining Functions

by Alex Jordan -

You can use contextForm.pl: https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/macros/PCC/contextForm.pl

WeBWorK is not a Computer Algebra System, so it can't honestly do what you would like it to do for this. But contextForm.pl is one way that can sufficiently "fake it".

In your example:
$f = Formula("3/(x^2 -x -2)")->reduce;
the "reduce" does nothing, since this fixed Formula has no applicable reductions from the reduction set. The same happens with:
Formula("-5/(x^2 -4)")->reduce;

Then:
Formula("$f+$g")->reduce;

is the same as:
Formula("3/(x^2-x-2)+-5/(x^2-4)")->reduce;
and the "reduce" might be what turns the "+-" into just a "-". But I am guessing you would like the answer to be
Formula("(-2x+1) / ((x-2)(x+1)(x+2))
")

To do that and use contextForm.pl, you as the problem coder have to come up with that simplified expression. And then if a student answers with "3/(x^2-x-2)-5/(x^2 -4)", they will be told that they have an answer that is equivalent to the right answer, but not in the expected form. There is a suggestion to simplify, factor, etc.

It is flexible enough to accept:
(2x-1) / ((2-x)(x+2)(x+1))
etc.

However it will count the following as something different:
(-2x+1) / (x^3+x^2-4x-4)

Your options are either to give specific instructions to the student, account for multiple answers using OneOf, or adjust the bizarro arithmetic settings that you can see described in contextForm.pl's commentary.

In reply to Alex Jordan

Re: Combining Functions

by Gregory Varner -

Thank you for the reply. You are correct that the reduce commands were not doing much (and nothing in most instances). I left them there in case I needed to go back and do it the long way.

It seems like the contextForm.pl messes with my display of negative numbers (puts parenthesis around them...), but otherwise could work.

I will have to look into it more, though my other options is to write the answer as I expect them to display it and be okay with any form (which works for what I am currently testing).

In reply to Gregory Varner

Re: Combining Functions

by Alex Jordan -
Oh, sorry. You clearly said "I currently am not as concerned about students simplifying" but my eyes read that to be the opposite. So you don't need contextForm.pl. You just need to construct the answer the way you want it to look and define it that way.

I said WW is not a CAS. That's true, but there is a way to get WW to use Sage, and maybe it can do this sort of thing automatically. You have to have a Sage cell running somewhere and get WW and Sage to communicate with each other. It's been a while since I tried to use this. But for completeness, take a look at: https://webwork.maa.org/wiki/AskSage.
In reply to Alex Jordan

Re: Combining Functions

by Gregory Varner -

No worries. The ability to let them know that their answer is equivalent but not in the expected form could be very useful. So thank you for letting me know about that option.

Since it serves my purposes currently (and is fairly easy to randomize later if I want), I am going the route of defining the answer the way I expect it to be.

I will certainly have to look into the Sage tie in though as that could be useful!