ConstantsInProblems

From WeBWorK_wiki
Revision as of 13:43, 24 April 2008 by Glarose (talk | contribs) (New page: <h2>Named Constants in Problems: 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 include...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Named Constants in Problems: PG Code Snippet

This code snippet shows the essential PG code to include named constants in a WeBWorK 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
  Context("Numeric");
  Context()->constants->add(k=>0.01);

No changes are needed in the tagging and description or initialization sections of the problem file. In the problem set-up, we add the constants we're going to use to the Context. Here we define a constant k, and assign it a value that will be used when expressions involving k are evaluated.

In this case we specified constants->add(), so that the constant k is added to existing constants in the problem. If we had used constants->are(), we would also remove all predefined constants from the Context (in a manner similar to the use of variables->add() and variables->are() when defining variables in a problem.

  BEGIN_TEXT
  \(f(x) = x-k\) (where \(k>0\) is constant)
  is zero when $BR
  \(x = \)\{ ans_rule(15) \}
  END_TEXT

In the text section of the problem we then ask a question involving the constant.

  ANS( Compute("k")->cmp() );

And in the answer and solution section of the file we can refer to the constant in the solution to the problem.

Problem Techniques Index