HeavisideStep1
Using the Heaviside Step Function
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 <e; 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
PG problem file | Explanation |
---|---|
Problem tagging: |
|
DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "AnswerFormatHelp.pl", "parserFunction.pl", ); TEXT(beginproblem()); |
Initialization:
We load |
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
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
Notice that the 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 > 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: |