## WeBWorK Problems

### Customize function in answer

by Yuncong Chen -
Number of replies: 4
Hi, I was writing questions on probability of a Gaussian tail which uses the Q-function. I would like the students to be able to write things like "Q(3)" in their answer rather than the numerical result. I know I can use normal_prob() to evaluate this input, but how can I make the answer checker recognize a customized function Q?

Thanks.
In reply to Yuncong Chen

### Re: Customize function in answer

by Paul Pearson -
Hi,

You will want to use the parserFunction.pl macro, which allows you to add a named function to the context. This macro is documented at

http://webwork.maa.org/pod/pg_TRUNK/macros/parserFunction.pl.html

and a working example of how it is used is

http://webwork.maa.org/wiki/ScalingTranslating1

Good luck!

Paul Pearson

In reply to Paul Pearson

### Re: Customize function in answer

by Yuncong Chen -
Thanks for the reply. So I tried adding:

Context(""Numeric);
parserFunction(Q => "normal_prob(x, 'infty', mean=>0, deviation=>1)");

it gives me an error:
ERROR caught by Translator while processing problem file:setAssignment14/ur_pb_11_3.pg
****************
'al' is not defined in this context; see position 5 of formula at line 2 of (eval 1446)
Died within parserFunction::Create called at line 2 of (eval 1446)
from within main::parserFunction called at line 39 of (eval 1336)

1		## DESCRIPTION
2		##   Apply the Central Limit Theorem
3		## ENDDESCRIPTION
4
5		## KEYWORDS('Probability', 'Central Limit Theorem', 'Normal', 'Mean', 'Standard Deviation')
6		## Tagged by nhamblet
7
8		## DBsubject('Probability')
9		## DBchapter('Theory')
10		## DBsection('The Central Limit Theorem')
11		## Date('')
12		## Author('')
13		## Institution('Rochester')
14		## TitleText1('')
15		## EditionText1('')
16		## AuthorText1('')
17		## Section1('')
18		## Problem1('')
19
20		DOCUMENT();        # This should be the first executable line in the problem.
21
23		"PG.pl",
24		"MathObjects.pl",
25		"parserFunction.pl",
26		"PGstandard.pl",
27		"PGbasicmacros.pl",
28		"PGchoicemacros.pl",
30		"PGgraphmacros.pl",
31		"PGnumericalmacros.pl",
32		"PGstatisticsmacros.pl"
33		);
34
35		TEXT(beginproblem());
36		$showPartialCorrectAnswers = 1; 37 38 Context("Numeric"); 39 parserFunction(erf => "normal_prob(-1, x, mean=>0, deviation=>1/sqrt(2))"); 40 41$n = random(10,20,1);
42		$score = random(570,589,0.5); 43 44$newdev = 112/sqrt($n); 45 46$ans1 = normal_prob($score, 'infty', mean=>509, deviation=>112); 47$ans2 = normal_prob($score, 'infty', mean=>509, deviation=>$newdev);
48
49		$m = new_multiple_choice(); 50$m ->qa("Two students are debating about this diminishing tail probability a consequence of which statistics rule. Student A argues that the Law of Large Number explains this behavior because this is basically what the weak LLN states, while student B argues that CLT explains it because as n gets larger the Gaussian gets more concentrated and the tail gets smaller. Use your own judgement, which of the following statement do you think is correct ?",
51		"Just the Law of Large Number is sufficient");
52		$m->makeLast("Just the Law of Large Number is sufficient", "You also need Central Limit Theorem to justify this answer"); 53 54 BEGIN_TEXT 55 56 Scores for men on the verbal portion of the SAT-I test are normally distributed with a mean of $$509$$ and a standard deviation of $$112$$.$BR
57
58		(a)  If $$1$$ man is randomly selected, find the probability that his score is at least $$score$$.$BR 59 60 \{ans_rule(10)\}$BR
61
62		(b)  If $$n$$ men are randomly selected, find the probability that their mean score is at least $$score$$. $BR 63 64 \{ans_rule(10)\}$BR
65
66		(c) As more and more men are randomly selected, the probability that their mean score is at least $$score$$ approaches \{ans_rule(10)\} $BR$BR
67
68		\{ $m->print_q \} 69 \{$m->print_a \}
70
71		END_TEXT
72
73		ANS(num_cmp($ans1,tol=>0.003)); 74 ANS(num_cmp($ans2,tol=>0.0005));
75		ANS(num_cmp(0));
76		ANS(radio_cmp($m->correct_ans)); 77 78 ENDDOCUMENT(); # This should be the last executable line in the problem. In reply to Yuncong Chen ### Re: Customize function in answer by Davide Cervone - The mechanism Paul suggested only lets you specify a function via a valid parser formula. Since normal_prob is a perl function (not available in the MathObject parser), you would need to use a slightly more complicated approach, outlined in the Wiki documentation, that allows you to tie a perl subroutine to a parser function. Here is an example code snippet that does this  loadMacros( "PGnumericalmacros.pl", "PGstatisticsmacros.pl", ); package my::Function::numeric; our @ISA = ('Parser::Function::numeric'); sub Q { shift; my$x = shift;
return main::normal_prob($x, 'infty', mean=>0, deviation=>1); } package main; Context("Numeric"); Context()->functions->add(Q => {class=>"my::Function::numeric", nocomplex=>1});$f = Compute("Q(3)");

Context()->texStrings;
BEGIN_TEXT
$$f$$ = \{ans_rule(20)\}
END_TEXT
Context()->normalStrings;

ANS(\$f->cmp);

Note that you could also use Compute("Q(x)") rather than having a constant value.

Hope that helps.

In reply to Davide Cervone

### Re: Customize function in answer

by Yuncong Chen -
Thanks, it works very well.