# ComposingFunctions

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Composing Functions

This PG code shows how to check student answers that are a composition of functions.

PG problem file Explanation
DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"PGcourse.pl",
);
TEXT(beginproblem());


Initialization: We need to include the macros file answerComposition.pl, which provides an answer checker that determines if two functions compose to form a given function. This can be used in problems where you ask a student to break a given function into a composition of two simpler functions, neither of which is allowed to be the identity function..

Context("Numeric");
Context()->variables->are(x=>"Real",y=>"Real",u=>"Real");

$a = random(2,9,1);$f = Formula("sqrt(u)");
$g = Formula("x^2+$a");


Setup: Everything is as usual.

Context()->texStrings;
BEGIN_TEXT
Express the function $$y = \sqrt{ x^2 + a }$$
as a composition $$y = f(g(x))$$ of two simpler
functions $$y = f(u)$$ and $$u = g(x)$$.
$BR$BR
$$f(u)$$ = \{ ans_rule(20) \}
$BR $$g(x)$$ = \{ ans_rule(20) \} END_TEXT Context()->normalStrings;  Main Text: The text section is as we'd expect. $showPartialCorrectAnswers = 1;

COMPOSITION_ANS( $f,$g, vars=>['u','x'], showVariableHints=>1);

ENDDOCUMENT();


Answer Evaluation: We use the COMPOSITION_ANS() routine to evaluate both answer blanks.