Difference between revisions of "RecursivelyDefinedFunctions"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 56: Line 56:
 
<b>Setup:</b>
 
<b>Setup:</b>
 
We define a new named function <code>f</code> as something the student is unlikely to guess. The named function <code>f</code> is, in some sense, just a placeholder since the student will enter expressions involving <code>f(n-1)</code>, WeBWorK will interpret it internally as <code>sin(pi^(n-1))+e</code>, and the only thing the student sees is <code>f(n-1)</code>. If the
 
We define a new named function <code>f</code> as something the student is unlikely to guess. The named function <code>f</code> is, in some sense, just a placeholder since the student will enter expressions involving <code>f(n-1)</code>, WeBWorK will interpret it internally as <code>sin(pi^(n-1))+e</code>, and the only thing the student sees is <code>f(n-1)</code>. If the
recursion has an closed-form solution (e.g., the Fibonacci numbers are given by f(n) = (a^n - (1-a)^n)/sqrt(5) where a = (1+sqrt(5))/2), it would be good to define f using that explicit solution in case the student tries to answer the question by writing out the explicit solution (a^n - (1-a)^n)/sqrt(5) instead of using the shorthand f(n).
+
recursion has an closed-form solution (e.g., the Fibonacci numbers are given by f(n) = (a^n - (1-a)^n)/sqrt(5) where a = (1+sqrt(5))/2) and you want to allows students to enter the closed-form solution, it would be good to define f using that explicit solution in case the student tries to answer the question by writing out the explicit solution (a^n - (1-a)^n)/sqrt(5) instead of using the shorthand f(n).
 
</p>
 
</p>
 
</td>
 
</td>

Revision as of 15:07, 21 March 2010

Recursively Defined Functions


This PG code shows how to check student answers that are recursively defined functions.

Problem Techniques Index

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

Initialization: We will be defining a new named function and adding it to the context, and the easiest way to do this is using parserFunction.pl. There is a more basic way to add functions to the context, which is explained in example 2 at AddingFunctions

Context("Numeric")->variables->are(n=>"Real");
parserFunction(f => "sin(pi^n)+e");

$fn = Formula("3 f(n-1) + 2");

Setup: We define a new named function f as something the student is unlikely to guess. The named function f is, in some sense, just a placeholder since the student will enter expressions involving f(n-1), WeBWorK will interpret it internally as sin(pi^(n-1))+e, and the only thing the student sees is f(n-1). If the recursion has an closed-form solution (e.g., the Fibonacci numbers are given by f(n) = (a^n - (1-a)^n)/sqrt(5) where a = (1+sqrt(5))/2) and you want to allows students to enter the closed-form solution, it would be good to define f using that explicit solution in case the student tries to answer the question by writing out the explicit solution (a^n - (1-a)^n)/sqrt(5) instead of using the shorthand f(n).

BEGIN_TEXT
The current value \( f(n) \) is three 
times the previous value plus two.  Find
a recursive definition for \( f(n) \).  
Enter \( f_{n-1} \) as \( f(n-1) \).
$BR
\( f(n) \) = \{ ans_rule(20) \} 
END_TEXT

Main Text: The problem text section of the file is as we'd expect. We should tell students to use function notation rather than subscript notation so that they aren't confused about syntax.

$showPartialCorrectAnswers=1;

ANS( $fn->cmp() );

ENDDOCUMENT();

Answer Evaluation: As is the answer.

Problem Techniques Index