WeBWorK Problems

Answers in terms of functions

Answers in terms of functions

by D. Brian Walton -
Number of replies: 1
Suppose I want to write a problem involving an unspecified function and the answer needs to be expressed in terms of that function. For example, this might be a recurrence relation between function values (sequences) and I want the student to type in the recurrence relation:

f(n) = 3 f(n-1) + 2

I would probably keep "f(n) =" outside of the answer block, so the student would really only need to type 3 f(n-1) + 2.

I know that I would add n as a variable to the context. But what do I do about f? In particular, I don't want to define f as a computable function, just that it is a function.


- D. Brian Walton

In reply to D. Brian Walton

Re: Answers in terms of functions

by Davide Cervone -
What you are asking for is somewhat difficult to accomplish in WeBWorK. This is because WeBWorK compares functions by evaluation, not by algebraic structure, so there would have to be a means of evaluating f(n) before WeBWorK would be able to handle it.

It is possible to use the parserFunction.pl macro library to do this, though it does present some possible (but unlikely) ways to get the answer marked correct without it being what you want.

Here is a sample code snippet that may do what you are after:


    parserFunction(f => "sin(pi^n)+e");  # something student is unlikely to type by hand
    $f = Formula("3 f(n-1) + 2");

    \(f(n)\) = \{ans_rule(20)\}.


Note that this defines f(n) to be something obscure that a student is unlikely to type by hand so that WeBWorK can evaluate the formula. It would be difficult to get a formula that matched without writing it using f (though if the definition of f is chosen badly, it might be able to be written in several ways using f).

Unfortunately, you can't include the f(n)= as part of the answer, even using the parserAssignment.pl macros, as they don't allow recursive definitions.

Alternatively, you could write a custom checker that looks for the specific algebraic structure you are using, but that would limit the form that the student could use to enter the answer (e.g., 3 f(n-1)+1+1 might not be accepted if you weren't careful about how you code your checker). This approach is difficult, and I wouldn't recommend it.