StepFunctions

From WeBWorK_wiki
Revision as of 09:27, 29 October 2009 by Glarose (talk | contribs) (New page: <h2>Step Functions: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to use step functions i...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Step Functions: PG Code Snippet

This code snippet shows the essential PG code to use step functions in a problem. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation
  ## this uses the LegacyNumeric Context
  # Context("LegacyNumeric");

  ## alternately, we can just add the step
  ##    function to the Numeric Context
  Context("Numeric");
  Context()->functions->add(
    step => {
      class => 'Parser::Legacy::Numeric',
      perl => 'Parser::Legacy::Numeric::do_step'
    },
  );

  ## then we can define an answer
  $step1 = Compute("step(x-1)");

No changes are necessary in the tagging and description or initialization sections of the problem file. In the problem set-up section, we declare a Context that will include the step function. This can be done in two ways: we can simply declare the Context to be the LegacyNumeric Context, which includes the step function step(x).

Alternately, we can add the step function to the standard Numeric Context. This has the advantage that we can name it step(x) (which is its default moniker) or, if we like, use something like u(x) instead (which is used in some other contexts, especially differential equations).

The code here defines the step function as step(x). To name it u(x), we would make the obvious change:

  Context()->functions->add(
    u => {
      class => 'Parser::Legacy::Numeric',
      perl => 'Parser::Legacy::Numeric::do_step'
    },
  );
  BEGIN_TEXT
  What is a function \(f(x)\) that is zero for 
  \(x<1\) and one for \(x\ge1\)?
  $BR
  \(f(x) = \) \{ ans_rule(35) \}
  END_TEXT

The text section of the problem is what we'd expect.

  ANS( $step1->cmp() );

As is the answer and solution section.

Problem Techniques Index