DifferentiateFunction1

From WeBWorK_wiki
Revision as of 17:42, 4 December 2010 by Pearson (talk | contribs)
Jump to navigation Jump to search

Differentiating and Evaluating a Function

Click to enlarge

This PG code shows how to create a function using MathObjects, differentiate it, and evaluate it.

  • Download file: File:DifferentiateFunction1.txt (change the file extension from txt to pg when you save it)
  • File location in NPL: FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1.pg


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT(); 

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"unionLists.pl",
);

TEXT(beginproblem());

Initialization: We load unionLists.pl to create an enumerated list in the Main Text section.

Context("Numeric")->variables->add(k=>"Real");
Context()->flags->set(
  reduceConstants=>0, # no decimals
  reduceConstantFunctions=>1, # combine 4+5*2?
  formatStudentAnswer=>'parsed', # no decimals
);

$a = random(6,9,1);
$k = random(3,5,1);

$f = Formula("k x^2");
$fx = $f->D('x');

@answer = ();

$answer[0] = $fx;

$answer[1] = $fx->substitute(k=>$k);

$answer[2] = $fx->substitute(x=>$a*pi,k=>$k);

Setup: The partial differentiation operator is ->D('x').

The main difference between eval() and substitute() is

  • eval() returns a Real (a number)
  • substitute() returns a Formula
Since plugging a particular number $k into the Formula $f returns a Formula $k x, if we had used the eval method $answer[1] = $fx->eval(k=>$k); instead of the substitute method, we would get errors because $k x is a Formula, not a Real.

When the answer is a constant, we can use either the eval method, in which case the answer would be a Real, or the substitute method, in which case the answer would be a constant Formula. If you use the eval method, $answer[2] = $fx->eval(x=>$a*pi,k=>$k); the answer will be a Real and will display as a single number in decimal format. If you use the substitute method instead, you have more control over how the answer will be displayed. In particular, the context flag reduceConstants=>0 controls whether the answer will be reduced to a single number in decimal format, the flag reduceConstantFunctions=>1 controls whether or not expressions such as 4+5*2 are reduced to 14, and setting the context flag formatStudentAnswer=>'parsed' will prevent the student's answer from being reduced to a single number in decimal format and will also display pi instead of 3.14159...

For more details, see [versus substitute], [correct answers], and [in problems].

Context()->texStrings;
BEGIN_TEXT
Suppose \( f(x) = $f \) where \( k \) is a 
constant.  
\{ BeginList("OL",type=>"a") \}

$ITEM \( f'(x) = \) 
\{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \}

$ITEM If \( k = $k \) then \( f'(x) = \)
\{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \}

$ITEM If \( k = $k \) then \( f'($a\pi) = \)
\{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \}

\{ EndList("OL") \}
END_TEXT
Context()->normalStrings;

Main Text:

$showPartialCorrectAnswers = 1;

foreach my $i (0..2) {
  ANS( $answer[$i]->cmp() );
}

Answer Evaluation:

Context()->texStrings;
BEGIN_SOLUTION
${PAR}SOLUTION:${PAR}
Solution explanation goes here.
END_SOLUTION
Context()->normalStrings;

COMMENT("MathObject version.");

ENDDOCUMENT();

Solution:

Templates by Subject Area