Difference between revisions of "DifferentiateFunction1"
Paultpearson (talk | contribs) (Add link to PGML version in OPL) |
|||
(9 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
This PG code shows how to create a function using MathObjects, differentiate it, and evaluate it. |
This PG code shows how to create a function using MathObjects, differentiate it, and evaluate it. |
||
</p> |
</p> |
||
− | * Download file: [[File:DifferentiateFunction1.txt]] (change the file extension from txt to pg when you save it) |
||
+ | * File location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1.pg FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1.pg] |
||
− | * File location in NPL: <code>FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1.pg</code> |
||
+ | * PGML location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1_PGML.pg FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1_PGML.pg] |
||
<br clear="all" /> |
<br clear="all" /> |
||
Line 83: | Line 83: | ||
$answer[0] = $fx; |
$answer[0] = $fx; |
||
− | $answer[1] = $fx->substitute(k=>$k); |
+ | $answer[1] = $fx->substitute(k=>$k); |
− | $answer[2] = $fx->substitute(x=>$a*pi,k=>$k); |
+ | $answer[2] = $fx->substitute(x=>$a*pi,k=>$k); |
− | #$answer[2] = $fx->eval(x=>$a*pi,k=>$k); # real |
||
</pre> |
</pre> |
||
</td> |
</td> |
||
Line 97: | Line 97: | ||
* <code>eval()</code> returns a Real (a number) |
* <code>eval()</code> returns a Real (a number) |
||
* <code>substitute()</code> returns a Formula |
* <code>substitute()</code> returns a Formula |
||
− | Since plugging a particular number <code>$k</code> into the Formula <code>$f</code> returns a Formula <code>$k x</code>, the eval method <code>$answer[1] = $fx->eval(k=>$k);</code> |
+ | Since plugging a particular number <code>$k</code> into the Formula <code>$f</code> returns a Formula <code>$k x</code>, if we had used the eval method <code>$answer[1] = $fx->eval(k=>$k);</code> instead of the substitute method, we would get errors because <code>$k x</code> 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. |
</p> |
</p> |
||
− | |||
+ | <p> |
||
− | Setting the context flag <code>reduceConstants=>1</code> would reduce answers to decimals, and setting it to zero does not evaluate to decimals. |
||
+ | 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, <code>$answer[2] = $fx->eval(x=>$a*pi,k=>$k);</code> 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 |
||
+ | <code>reduceConstants</code> controls whether the answer will be reduced to a single number in decimal format, the flag <code>reduceConstantFunctions</code> controls whether or not expressions such as <code>4+5*2</code> are reduced to <code>14</code>, and setting the context flag <code>formatStudentAnswer=>'parsed'</code> will prevent the student's answer from being reduced to a single number in decimal format and will also display <code>pi</code> instead of <code>3.14159...</code> |
||
+ | </p> |
||
+ | <p> |
||
+ | For more details, see [http://webwork.maa.org/wiki/Eval%28%29vs.substitute%28%29 eval versus substitute], [http://webwork.maa.org/wiki/FormattingCorrectAnswers:_NumbersAndFormulas formatting correct answers], and [http://webwork.maa.org/wiki/ConstantsInProblems constants in problems]. |
||
</p> |
</p> |
||
</td> |
</td> |
||
Line 120: | Line 124: | ||
\{ AnswerFormatHelp("formulas") \} |
\{ AnswerFormatHelp("formulas") \} |
||
+ | $ITEMSEP |
||
$ITEM If \( k = $k \) then \( f'(x) = \) |
$ITEM If \( k = $k \) then \( f'(x) = \) |
||
\{ ans_rule(20) \} |
\{ ans_rule(20) \} |
||
\{ AnswerFormatHelp("formulas") \} |
\{ AnswerFormatHelp("formulas") \} |
||
+ | $ITEMSEP |
||
$ITEM If \( k = $k \) then \( f'($a\pi) = \) |
$ITEM If \( k = $k \) then \( f'($a\pi) = \) |
||
\{ ans_rule(20) \} |
\{ ans_rule(20) \} |
||
Line 135: | Line 141: | ||
<p> |
<p> |
||
<b>Main Text:</b> |
<b>Main Text:</b> |
||
+ | We use an ordered list to display the three parts to this question. The <code>$ITEMSEP</code> command puts extra separation between items. This list is provided by the macro <code>unionLists.pl</code>. |
||
</p> |
</p> |
||
</td> |
</td> |
||
Line 164: | Line 171: | ||
Context()->texStrings; |
Context()->texStrings; |
||
BEGIN_SOLUTION |
BEGIN_SOLUTION |
||
− | ${PAR}SOLUTION:${PAR} |
||
Solution explanation goes here. |
Solution explanation goes here. |
||
END_SOLUTION |
END_SOLUTION |
||
Line 187: | Line 193: | ||
[[Category:Top]] |
[[Category:Top]] |
||
− | [[Category: |
+ | [[Category:Sample Problems]] |
+ | [[Category:Subject Area Templates]] |
Revision as of 17:01, 7 June 2015
Differentiating and Evaluating a Function
This PG code shows how to create a function using MathObjects, differentiate it, and evaluate it.
- File location in OPL: FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1.pg
- PGML location in OPL: FortLewis/Authoring/Templates/DiffCalc/DifferentiateFunction1_PGML.pg
PG problem file | Explanation |
---|---|
Problem tagging: |
|
DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "AnswerFormatHelp.pl", "unionLists.pl", ); TEXT(beginproblem()); |
Initialization:
We load |
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
The main difference between
$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. 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. |
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") \} $ITEMSEP $ITEM If \( k = $k \) then \( f'(x) = \) \{ ans_rule(20) \} \{ AnswerFormatHelp("formulas") \} $ITEMSEP $ITEM If \( k = $k \) then \( f'($a\pi) = \) \{ ans_rule(20) \} \{ AnswerFormatHelp("formulas") \} \{ EndList("OL") \} END_TEXT Context()->normalStrings; |
Main Text:
We use an ordered list to display the three parts to this question. The |
$showPartialCorrectAnswers = 1; foreach my $i (0..2) { ANS( $answer[$i]->cmp() ); } |
Answer Evaluation: |
Context()->texStrings; BEGIN_SOLUTION Solution explanation goes here. END_SOLUTION Context()->normalStrings; COMMENT("MathObject version."); ENDDOCUMENT(); |
Solution: |