Difference between revisions of "DifferentiateFunction1"

From WeBWorK_wiki
Jump to navigation Jump to search
(Created page with '<h2>Differentiating and Evaluating a Function</h2> 300px|thumb|right|Click to enlarge <p style="background-color:#f9f9f9;border:black solid 1…')
 
(add historical tag and give links to newer problems.)
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/DiffCalc/DifferentiateFunction.html a newer version of this problem]</p>
  +
  +
 
<h2>Differentiating and Evaluating a Function</h2>
 
<h2>Differentiating and Evaluating a Function</h2>
   
Line 5: Line 10:
 
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 16: Line 21:
   
 
<tr valign="top">
 
<tr valign="top">
<th> PG problem file </th>
+
<th style="width: 50%"> PG problem file </th>
 
<th> Explanation </th>
 
<th> Explanation </th>
 
</tr>
 
</tr>
Line 43: Line 48:
   
 
loadMacros(
 
loadMacros(
"PGstandard.pl",
+
'PGstandard.pl',
"MathObjects.pl",
+
'MathObjects.pl',
"AnswerFormatHelp.pl",
+
'PGML.pl',
"unionLists.pl",
+
'PGcourse.pl'
 
);
 
);
   
Line 54: Line 59:
 
<td style="background-color:#ddffdd;padding:7px;">
 
<td style="background-color:#ddffdd;padding:7px;">
 
<p>
 
<p>
<b>Initialization:</b>
 
 
</p>
 
</p>
 
</td>
 
</td>
Line 65: Line 69:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context("Numeric")->variables->add(k=>"Real");
+
Context('Numeric')->variables->add(k=>'Real');
 
Context()->flags->set(
 
Context()->flags->set(
 
reduceConstants=>0, # no decimals
 
reduceConstants=>0, # no decimals
Line 75: Line 79:
 
$k = random(3,5,1);
 
$k = random(3,5,1);
   
$f = Formula("k x^2");
+
$f = Formula('k x^2');
 
$fx = $f->D('x');
 
$fx = $f->D('x');
   
@answer = ();
+
$ans1 = $fx;
+
$ans2 = $fx->substitute(k=>$k);
$answer[0] = $fx;
+
$ans3 = $fx->substitute(x=>$a*pi,k=>$k);
 
$answer[1] = $fx->substitute(k=>$k); # formula
 
# $answer[1] = $fx->eval(k=>$k); # gives errors, must eval to real
 
 
$answer[2] = $fx->substitute(x=>$a*pi,k=>$k); # formula
 
#$answer[2] = $fx->eval(x=>$a*pi,k=>$k); # real
 
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
<b>Setup:</b>
+
<b>Setup:</b>
  +
The partial differentiation operator is <code>-&gt;D('x')</code>.
  +
</p>
  +
<p>
  +
The main difference between <code>eval()</code> and <code>substitute()</code> is
  +
* <code>eval()</code> returns a Real (a number)
  +
* <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>, if we had used the eval method <code>$ans2 = $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>
  +
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>$ans3 = $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 95: Line 99:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context()->texStrings;
 
  +
BEGIN_PGML
BEGIN_TEXT
 
  +
Suppose [` f(x) = [$f] `] where [` k `] is a constant.
Suppose \( f(x) = $f \) where \( k \) is a
 
constant.
 
\{ BeginList("OL",type=>"a") \}
 
   
$ITEM \( f'(x) = \)
 
  +
a. [` f ' (x) = `] [_______________]{$ans1}
\{ ans_rule(20) \}
 
\{ AnswerFormatHelp("formulas") \}
 
   
$ITEM If \( k = $k \) then \( f'(x) = \)
+
b. If [` k = [$k] `] then [` f ' (x) = `] [_______________]{$ans2}
\{ ans_rule(20) \}
 
\{ AnswerFormatHelp("formulas") \}
 
   
$ITEM If \( k = $k \) then \( f'($a\pi) = \)
+
c. If [` k = [$k] `] then [` f ' ([$a]\pi) = `] [_______________]{$ans3}
\{ ans_rule(20) \}
 
\{ AnswerFormatHelp("formulas") \}
 
   
\{ EndList("OL") \}
 
  +
[@ helpLink('formulas') @]*
END_TEXT
 
  +
END_PGML
Context()->normalStrings;
 
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
</p>
 
</td>
 
</tr>
 
 
<!-- Answer evaluation section -->
 
 
<tr valign="top">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
$showPartialCorrectAnswers = 1;
 
 
foreach my $i (0..2) {
 
ANS( $answer[$i]->cmp() );
 
}
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<b>Answer Evaluation:</b>
 
 
</p>
 
</p>
 
</td>
 
</td>
Line 143: Line 123:
 
<td style="background-color:#ddddff;border:black 1px dashed;">
 
<td style="background-color:#ddddff;border:black 1px dashed;">
 
<pre>
 
<pre>
Context()->texStrings;
 
  +
BEGIN_PGML_SOLUTION
BEGIN_SOLUTION
 
${PAR}SOLUTION:${PAR}
 
 
Solution explanation goes here.
 
Solution explanation goes here.
END_SOLUTION
 
  +
END_PGML_SOLUTION
Context()->normalStrings;
 
   
COMMENT("MathObject version.");
+
COMMENT('Uses PGML.');
   
 
ENDDOCUMENT();
 
ENDDOCUMENT();
Line 168: Line 145:
   
 
[[Category:Top]]
 
[[Category:Top]]
[[Category:Authors]]
+
[[Category:Sample Problems]]
  +
[[Category:Subject Area Templates]]

Latest revision as of 05:09, 18 July 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem


Differentiating and Evaluating a Function

Click to enlarge

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


Templates by Subject Area

PG problem file Explanation

Problem tagging data

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 ->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 $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, $ans3 = $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 controls whether the answer will be reduced to a single number in decimal format, the flag reduceConstantFunctions 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 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:

Templates by Subject Area