Difference between revisions of "DifferentiateFunction1"
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…') |
|||
Line 55: | Line 55: | ||
<p> |
<p> |
||
<b>Initialization:</b> |
<b>Initialization:</b> |
||
+ | We load <code>unionLists.pl</code> to create an enumerated list in the Main Text section. |
||
</p> |
</p> |
||
</td> |
</td> |
||
Line 83: | Line 84: | ||
$answer[1] = $fx->substitute(k=>$k); # formula |
$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->substitute(x=>$a*pi,k=>$k); # formula |
||
Line 91: | Line 91: | ||
<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>->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>, the eval method <code>$answer[1] = $fx->eval(k=>$k);</code> gives errors because eval returns a Real (not a Formula). The substitute method returns a Formula, so there are no errors using <code>$answer[1] = $fx->substitute(k=>$k);</code>. |
||
+ | </p> |
||
+ | |||
+ | Setting the context flag <code>reduceConstants=>1</code> would reduce answers to decimals, and setting it to zero does not evaluate to decimals. |
||
</p> |
</p> |
||
</td> |
</td> |
Revision as of 17:26, 4 December 2010
Differentiating and Evaluating a Function
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
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); # formula $answer[2] = $fx->substitute(x=>$a*pi,k=>$k); # formula #$answer[2] = $fx->eval(x=>$a*pi,k=>$k); # real |
Setup:
The partial differentiation operator is
The main difference between
$k into the Formula $f returns a Formula $k x , the eval method $answer[1] = $fx->eval(k=>$k); gives errors because eval returns a Real (not a Formula). The substitute method returns a Formula, so there are no errors using $answer[1] = $fx->substitute(k=>$k); .
Setting the context flag |
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: |