DifferentiateFunction1
Differentiating and Evaluating a Function
This PG code shows how to create a function using MathObjects, differentiate it, and evaluate it.
- PGML location in OPL: FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1_PGML.pg
PG problem file | Explanation |
---|---|
Problem tagging: |
|
DOCUMENT(); loadMacros( 'PGstandard.pl', 'MathObjects.pl', 'PGML.pl', 'PGcourse.pl' ); TEXT(beginproblem()); |
|
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'); $ans1 = $fx; $ans2 = $fx->substitute(k=>$k); $ans3 = $fx->substitute(x=>$a*pi,k=>$k); |
Setup:
The partial differentiation operator is
The main difference between
$k into the Formula $f returns a Formula $k x , if we had used the eval method $ans2 = $fx->eval(k=>$k); instead of the substitute method, we would get errors because $k x is a Formula, not a Real. Note: You cannot use eval or substitute to perform function composition, i.e., you can only plug in numbers, not formulas.
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, For more details, see eval versus substitute, formatting correct answers, and constants in problems. |
BEGIN_PGML Suppose [` f(x) = [$f] `] where [` k `] is a constant. a. [` f ' (x) = `] [_______________]{$ans1} b. If [` k = [$k] `] then [` f ' (x) = `] [_______________]{$ans2} c. If [` k = [$k] `] then [` f ' ([$a]\pi) = `] [_______________]{$ans3} [@ helpLink('formulas') @]* END_PGML |
Main Text: |
BEGIN_PGML_SOLUTION Solution explanation goes here. END_PGML_SOLUTION COMMENT('Uses PGML.'); ENDDOCUMENT(); |
Solution: |