Difference between revisions of "HeavisideStep1"

From WeBWorK_wiki
Jump to navigation Jump to search
(add historical tag and give links to newer problems.)
 
(14 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/DiffEq/HeavisideStep.html a newer version of this problem]</p>
  +
 
<h2>Using the Heaviside Step Function</h2>
 
<h2>Using the Heaviside Step Function</h2>
   
Line 5: Line 9:
 
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.
 
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)
 
  +
* File location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/DiffEq/HeavisideStep1.pg FortLewis/Authoring/Templates/DiffEq/HeavisideStep1.pg]
* File location in NPL: <code>FortLewis/Authoring/Templates/DiffEq/HeavisideStep1.pg</code>
 
  +
* PGML location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/DiffEq/HeavisideStep1_PGML.pg FortLewis/Authoring/Templates/DiffEq/HeavisideStep1_PGML.pg]
   
 
<br clear="all" />
 
<br clear="all" />
Line 56: Line 60:
 
<b>Initialization:</b>
 
<b>Initialization:</b>
 
We load <code>parserFunction.pl</code> to make adding a named function to the context easier.
 
We load <code>parserFunction.pl</code> to make adding a named function to the context easier.
  +
Please see the POD documentation [http://webwork.maa.org/pod/pg/macros/parserFunction.html parserFunction.pl].
 
</p>
 
</p>
 
</td>
 
</td>
Line 66: Line 71:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
$ftex = "5 u(t-3)";
 
 
 
Context("Numeric");
 
Context("Numeric");
 
Context()->variables->are(t=>"Real");
 
Context()->variables->are(t=>"Real");
Line 77: Line 80:
 
);
 
);
   
  +
$a = 3;
   
$f = Formula("5*step(t-3)");
+
$ftex = "5 u(t-$a)";
   
$answer1 = List($f->eval(t=>2),$f->eval(t=>3),$f->eval(t=>4));
 
  +
$fstep = Formula("5*step(t-$a)");
  +
  +
$answer1 = List($fstep->eval(t=>2),$fstep->eval(t=>3),$fstep->eval(t=>4));
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 86: Line 92:
 
<p>
 
<p>
 
<b>Setup 1:</b>
 
<b>Setup 1:</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 &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
+
We add the step function to the context with the name <code>step</code>. The function <code>step(t)</code> is the Heaviside function and takes the value 1 when t &gt; 0, and the value 0 when t &le; 0. We will use the function <code>step</code> when evaluating the Heaviside function to obtain an answer that is a number.
 
* <code>step</code>, which is the Heaviside function, will be used when the answer is a number
 
* <code>u</code> will be used when the answer is a function
 
 
</p>
 
</p>
 
<p>
 
<p>
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 <code>step</code> is not very robust when checking answers. For example, if a student types in the answer <code>step(t-0.1)</code> and the correct answer is <code>step(t)</code>, 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 <code>(0,0.1)</code> is much less than 100%. Also, if the correct answer were <code>step(t-5)</code>, then a student could enter the answer <code>0</code> and be marked correct because the correct answer is identically zero on the interval <code>[-1,1]</code>.
 
  +
For more details on adding the Heaviside function to the context, see the forum discussion on the [http://webwork.maa.org/moodle/mod/forum/discuss.php?d=458 Heaviside step function]
</p>
 
<p>
 
Notice that the function <code>u(t)</code> 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 <code>u(t)</code> is not something students are likely to input as an answer out of nowhere. The function <code>u(t)</code> 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 <code>step(t)</code> should be used instead of <code>u(t)</code> for obvious reasons.
 
</p>
 
<p>
 
Note: Currently, functions that are added to the context do not work with <code>test_points</code>, but they do work with changes to the domain <code>limits=>[a,b]</code> and the number of points <code>num_points=>c</code> set using Context flags.
 
 
</p>
 
</p>
 
</td>
 
</td>
Line 113: Line 113:
 
\left\lbrace
 
\left\lbrace
 
\begin{array}{lcl}
 
\begin{array}{lcl}
0 && \mbox{ if } x > 0, \\
+
0 && \mbox{ if } x \leq 0, \\
1 && \mbox{ if } x \leq 0.
+
1 && \mbox{ if } x > 0.
 
\end{array}
 
\end{array}
 
\right.
 
\right.
Line 121: Line 121:
 
$BR
 
$BR
 
(a) Evaluate the function \( $ftex \) when
 
(a) Evaluate the function \( $ftex \) when
\( t \) is 2, 3, and 4 and enter your answer
+
\( t \) is \(2\), \(3\), and \(4\) and enter
as a comma separated list.
+
your answers as a comma separated list.
 
$BR
 
$BR
 
\{ ans_rule(20) \}
 
\{ ans_rule(20) \}
Line 163: Line 163:
 
Context("Numeric");
 
Context("Numeric");
 
Context()->variables->are(t=>"Real");
 
Context()->variables->are(t=>"Real");
Context()->flags->set(
+
Context()->functions->add(
limits=>[-5,5],
+
step => {
num_points=>10,
+
class => 'Parser::Legacy::Numeric',
  +
perl => 'Parser::Legacy::Numeric::do_step'
  +
},
 
);
 
);
parserFunction("u(t)" => "1.5*sin(e*t)+2*pi/e");
 
   
  +
parserFunction("u(t)" => "step(t)");
  +
$f = Formula("5 u(t-$a)");
   
$answer2 = Formula("5 u(t-3)");
+
$answer2 = $f->with(
  +
limits=>[$a-5,$a+5],
  +
test_at => [[$a-1],[$a],[$a+0.0000001],[$a+1]],
  +
num_points=>10,
  +
);
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
<b>Setup 2:</b>
+
<b>Setup 2:</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 &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
+
We reset the context using <code>Context("Numeric")</code> and add the function <code>step(t)</code> as before. The reason for resetting the context is that in part (a) of the question we don't want to allow students to type in <code>u(2),u(3),u(4)</code> and get the right answer, because we want students to evaluate the Heaviside function themselves.
 
* <code>step</code>, which is the Heaviside function, will be used when the answer is a number
 
* <code>u</code> will be used when the answer is a function
 
 
</p>
 
</p>
 
<p>
 
<p>
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 <code>step</code> is not very robust when checking answers. For example, if a student types in the answer <code>step(t-0.1)</code> and the correct answer is <code>step(t)</code>, 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 <code>(0,0.1)</code> is much less than 100%. Also, if the correct answer were <code>step(t-5)</code>, then a student could enter the answer <code>0</code> and be marked correct because the correct answer is identically zero on the interval <code>[-1,1]</code>.
 
  +
Using a different method for adding functions to the context, we add the named function <code>u(t)</code> using <code>parserFunction</code> and make it identical to the function <code>step(t)</code>. The reason for adding <code>u(t)</code> to the context is that students will then be able to enter it as a named function in their answer.
 
</p>
 
</p>
 
<p>
 
<p>
Notice that the function <code>u(t)</code> 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 <code>u(t)</code> is not something students are likely to input as an answer out of nowhere. The function <code>u(t)</code> 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 <code>step(t)</code> should be used instead of <code>u(t)</code> for obvious reasons.
 
  +
Since answers are checked numerically by comparing the student answer to the correct answer at several randomly points in the domain (the default is 5 points) in an interval (the default is [-1,1]), the function <code>step(t) = u(t)</code> is not very robust when checking answers using these defaults. For example, if a student types in the answer <code>u(t-0.1)</code> and the correct answer is <code>u(t)</code>, 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 <code>(0,0.1)</code> is much less than 100%. Also, if the correct answer were <code>u(t-5)</code>, then a student could enter the answer <code>0</code> and be marked correct because the correct answer is identically zero on the interval <code>[-1,1]</code>.
 
</p>
 
</p>
 
<p>
 
<p>
Note: Currently, functions that are added to the context do not work with <code>test_points</code>, but they do work with changes to the domain <code>limits=>[a,b]</code> and the number of points <code>num_points=>c</code> set using Context flags.
 
  +
To make the answer checking robust, in <code>$answer2</code> we specify a larger domain centered at <code>$a</code> using <code>limits</code>, we require four of the test points always be used, and that there should be 10 test points total (the four we specified and six others generated at random). Notice that we used the construction <code>$f->with(...)</code> to do this (using <code>$f->{test_at} = [[1],[2]]</code> would generate an error because the functions we added to the context aren't "blessed" with enough permissions to modify <code>$f</code> in that way).
  +
</p>
  +
<p>
  +
In part (b), since the students never actually see the values of the function <code>u(t)</code>, we could have defined the function as
  +
<pre>
  +
parserFunction("u(t)" =>
  +
"1.5 * sin(e*t) + 5*pi/3 + arctan(t)"
  +
);
  +
</pre>
  +
If we had defined <code>u(t)</code> this way, we would not have had to add the function <code>step(t)</code> to the context and we could have used the defaults for the answer checker. Notice that the function <code>u(t)</code> is never zero, is not constant, is differentiable, and takes moderately sized values, which makes its answer checking very robust using the defaults for the answer checker. Further, because of the arctangent, it is not periodic and so <code>u(t)-u(t-a)</code> should never be identically zero. Also, the formula for <code>u(t)</code> is not something students are likely to input as an answer out of nowhere. The function <code>u(t)</code> 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 <code>step(t)</code> should be used or the function <code>u(t)</code> should be properly defined as the Heaviside function for obvious reasons.
 
</p>
 
</p>
 
</td>
 
</td>
Line 199: Line 209:
 
$BR
 
$BR
 
$BR
 
$BR
(b) Enter the function \( $ftex \).
+
(b) Suppose the answer is the function \( $ftex \).
 
$BR
 
$BR
 
\{ ans_rule(20) \}
 
\{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \}
+
\{ AnswerFormatHelp("formulas") \}
 
END_TEXT
 
END_TEXT
 
Context()->normalStrings;
 
Context()->normalStrings;
Line 237: Line 247:
 
Context()->texStrings;
 
Context()->texStrings;
 
BEGIN_SOLUTION
 
BEGIN_SOLUTION
${PAR}SOLUTION:${PAR}
 
 
Solution explanation goes here.
 
Solution explanation goes here.
 
END_SOLUTION
 
END_SOLUTION
Line 260: Line 269:
   
 
[[Category:Top]]
 
[[Category:Top]]
[[Category:Authors]]
+
[[Category:Sample Problems]]
  +
[[Category:Subject Area Templates]]

Latest revision as of 05:27, 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

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.


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. Please see the POD documentation parserFunction.pl.

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

$a = 3;

$ftex  = "5 u(t-$a)";

$fstep = Formula("5*step(t-$a)");

$answer1 = List($fstep->eval(t=>2),$fstep->eval(t=>3),$fstep->eval(t=>4));

Setup 1: We add the step function to the context with the name step. The function step(t) is the Heaviside function and takes the value 1 when t > 0, and the value 0 when t ≤ 0. We will use the function step when evaluating the Heaviside function to obtain an answer that is a number.

For more details on adding the Heaviside function to the context, see the forum discussion on the Heaviside step function

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 \leq 0, \\
1 && \mbox{ if } x > 0.
\end{array}
\right.
\)
$BR
$BR
(a) Evaluate the function \( $ftex \) when  
\( t \) is \(2\), \(3\), and \(4\) and enter 
your answers as a comma separated list.
$BR
\{ ans_rule(20) \}
\{ AnswerFormatHelp("numbers") \} 
END_TEXT
Context()->normalStrings;

Main Text 1:

$showPartialCorrectAnswers=1;

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

Answer Evaluation 1:

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

parserFunction("u(t)" => "step(t)");
$f = Formula("5 u(t-$a)");

$answer2 = $f->with(
  limits=>[$a-5,$a+5], 
  test_at => [[$a-1],[$a],[$a+0.0000001],[$a+1]],
  num_points=>10,
);

Setup 2: We reset the context using Context("Numeric") and add the function step(t) as before. The reason for resetting the context is that in part (a) of the question we don't want to allow students to type in u(2),u(3),u(4) and get the right answer, because we want students to evaluate the Heaviside function themselves.

Using a different method for adding functions to the context, we add the named function u(t) using parserFunction and make it identical to the function step(t). The reason for adding u(t) to the context is that students will then be able to enter it as a named function in their answer.

Since answers are checked numerically by comparing the student answer to the correct answer at several randomly points in the domain (the default is 5 points) in an interval (the default is [-1,1]), the function step(t) = u(t) is not very robust when checking answers using these defaults. For example, if a student types in the answer u(t-0.1) and the correct answer is u(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 u(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].

To make the answer checking robust, in $answer2 we specify a larger domain centered at $a using limits, we require four of the test points always be used, and that there should be 10 test points total (the four we specified and six others generated at random). Notice that we used the construction $f->with(...) to do this (using $f->{test_at} = [[1],[2]] would generate an error because the functions we added to the context aren't "blessed" with enough permissions to modify $f in that way).

In part (b), since the students never actually see the values of the function u(t), we could have defined the function as

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

If we had defined u(t) this way, we would not have had to add the function step(t) to the context and we could have used the defaults for the answer checker. 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 using the defaults for the answer checker. Further, because of the arctangent, it is not periodic and so u(t)-u(t-a) should never be identically zero. Also, 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 or the function u(t) should be properly defined as the Heaviside function for obvious reasons.

Context()->texStrings;
BEGIN_TEXT
$BR
$BR
(b) Suppose the answer is the function \( $ftex \).
$BR
\{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \} 
END_TEXT
Context()->normalStrings;

Main Text 2:

ANS( $answer2->cmp() );

Answer Evaluation 2:

Context()->texStrings;
BEGIN_SOLUTION
Solution explanation goes here.
END_SOLUTION
Context()->normalStrings;

COMMENT('MathObject version.');

ENDDOCUMENT();

Solution:

Templates by Subject Area