Difference between revisions of "HeavisideStep1"

From WeBWorK_wiki
Jump to navigation Jump to search
(Created page with '<h2>Using the Heaviside Step Function</h2> 300px|thumb|right|Click to enlarge <p style="background-color:#f9f9f9;border:black solid 1px;padding:3px;"…')
 
Line 3: Line 3:
 
[[File:HeavisideStep1.png|300px|thumb|right|Click to enlarge]]
 
[[File:HeavisideStep1.png|300px|thumb|right|Click to enlarge]]
 
<p style="background-color:#f9f9f9;border:black solid 1px;padding:3px;">
 
<p style="background-color:#f9f9f9;border:black solid 1px;padding:3px;">
This PG code shows how to add a the Heaviside step function <code>step(x)</code>, which takes the value 1 if x &gt; 0, and the value 0 if x &lte; 0, to the context. It also shows how to add a named function <code>u(x)</code> to the context that has a reliable answer checker and can stand in for the Heaviside step function when the student answer is a function.
+
This PG code shows how to add a the Heaviside step function <code>step(x)</code>, which takes the value 1 if x &gt; 0, and the value 0 if x &le; 0, to the context. It also shows how to add a named function <code>u(x)</code> to the context that has a reliable answer checker and can stand in for the Heaviside step function when the student answer is a function.
 
</p>
 
</p>
 
* Download file: [[File:HeavisideStep1.txt]] (change the file extension from txt to pg when you save it)
 
* Download file: [[File:HeavisideStep1.txt]] (change the file extension from txt to pg when you save it)
Line 87: Line 87:
 
<p>
 
<p>
 
<b>Setup:</b>
 
<b>Setup:</b>
We add the (Legacy) step function to the context with the name <code>step</code>. The function <code>step(t)</code> takes the value 1 when t &gt; 0, and the value 0 when t &lte; 0. Using <code>parserFunction.pl</code>, we add another function named <code>u(x)</code> to the context. The reason for adding these two functions to the context is that
+
We add the (Legacy) step function to the context with the name <code>step</code>. The function <code>step(t)</code> takes the value 1 when t &gt; 0, and the value 0 when t &le; 0. Using <code>parserFunction.pl</code>, we add another function named <code>u(x)</code> to the context. The reason for adding these two functions to the context is that
   
 
* <code>step</code>, which is the Heaviside function, will be used when the answer is a number
 
* <code>step</code>, which is the Heaviside function, will be used when the answer is a number

Revision as of 02:03, 6 December 2010

Using the Heaviside Step Function

Click to enlarge

This PG code shows how to add a the Heaviside step function step(x), which takes the value 1 if x > 0, and the value 0 if x ≤ 0, to the context. It also shows how to add a named function u(x) to the context that has a reliable answer checker and can stand in for the Heaviside step function when the student answer is a function.

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


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT();

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

TEXT(beginproblem());

Initialization: We load parserFunction.pl to make adding a named function to the context easier.

Context("Numeric");
Context()->variables->are(t=>"Real");
Context()->functions->add(
  step => {
    class => 'Parser::Legacy::Numeric',
    perl => 'Parser::Legacy::Numeric::do_step'
  },
);

parserFunction("u(t)" => "1.5*sin(e*t)+2*pi/e");

$f = Formula("5*step(t-3)");
$g = Formula("5*u(t-3)");

$answer1 = List($f->eval(t=>2),$f->eval(t=>3),$f->eval(t=>4));
$answer2 = $g;

Setup: We add the (Legacy) step function to the context with the name step. The function step(t) takes the value 1 when t > 0, and the value 0 when t ≤ 0. Using parserFunction.pl, we add another function named u(x) to the context. The reason for adding these two functions to the context is that

  • step, which is the Heaviside function, will be used when the answer is a number
  • u will be used when the answer is a function

Since answers are checked numerically by comparing the student answer to the correct answer at several points in the domain (the default is 5 points) in an interval (the default is [-1,1]), the function step is not very robust when checking answers. For example, if a student types in the answer step(t-0.1) and the correct answer is step(t), there is a good chance that the student's answer will be marked correct, since the probability that a test point was chosen in the interval (0,0.1) is much less than 100%. Also, if the correct answer were step(t-5), then a student could enter the answer 0 and be marked correct because the correct answer is identically zero on the interval [-1,1].

Notice that the function u(t) is never zero, is not constant, is differentiable, and takes moderately sized values, which makes its answer checking very robust. Also, notice that the formula for u(t) is not something students are likely to input as an answer out of nowhere. The function u(t) is great as a named function that stands in for the Heaviside function when the answer is a function. However, if the answer is a number obtained by evaluating the Heaviside function, then step(t) should be used instead of u(t) for obvious reasons.

Context()->texStrings;
BEGIN_TEXT
Let \( u(t) \) be the Heaviside step function 
defined by
\( \displaystyle
u(t) =
\left\lbrace
\begin{array}{lcl}
0 && \mbox{ if } x > 0, \\
1 && \mbox{ if } x \leq 0.
\end{array}
\right.
\)
$BR
$BR
(a) Evaluate the function \( $g \) when  
\( t \) is 2, 3, and 4 and enter your answer 
as a comma separated list.
$BR
\{ ans_rule(20) \}
\{ AnswerFormatHelp("numbers") \} 
$BR
$BR
(b) Enter the function \( $g \).
$BR
\{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \}
END_TEXT
Context()->normalStrings;

Main Text:

$showPartialCorrectAnswers=1;

ANS( $answer1->cmp(ordered=>1) );

ANS( $answer2->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