# Difference between revisions of "SampleProblem2"

Line 57: | Line 57: | ||

"MathObjects.pl", |
"MathObjects.pl", |
||

"PGchoicemacros.pl", |
"PGchoicemacros.pl", |
||

− | "PGcourse.pl", |
||

); |
); |
||

</pre> |
</pre> |

## Revision as of 16:11, 10 April 2008

## A Second WeBWorK Sample Problem: Problem Types

*This sample problem shows how to write three commonly used problem types: formulas making use of more of the MathObjects Formula functionality, multiple choice, and string entry problems.*

Recall that a !WeBWorK PG file has five sections: (1) a *tagging and description section*, (2) an *initialization section*, (3) a *problem set-up section*, (4) a *text section*, and (5) an *answer and solution section*. These sections are shown below, and the parts of each of each that are related to the different problem types being discussed here are indicated.

The sample file explained below is
this file * (need attachment)*.

PG problem file | Explanation |
---|---|

# DESCRIPTION # A simple sample problem that illustrates # three common problem types. # WeBWorK problem written by Gavin LaRose, # <glarose(at)umich(dot)edu> # ENDDESCRIPTION ## DBsubject('WeBWorK') ## DBchapter('Demos') ## DBsection('Problem') ## KEYWORDS('') ## TitleText1('') ## EditionText1('') ## AuthorText1('') ## Section1('') ## Problem1('') ## Author('Gavin LaRose') ## Institution('UMich') |
This is the Recall that the tagging information exists to allow the problem to be easily indexed. Lists of tag values are on-line: current chapter and section names, and current keywords. The list of keywords should be comma separated and quoted (e.g., KEYWORDS('calculus,derivatives')). |

DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "PGchoicemacros.pl", ); |
This is the |

# make sure we're in the context we want Context("Numeric"); # INITIALIZATION FOR PART 1 # set up a function for our formula problem. Context()->variables->add(t=>'Real'); $a = random(2,9,1); $func = Formula("cos($a t)"); $funcDeriv = $func->D('t'); $m = $funcDeriv->eval(t=>2); $y0 = $func->eval(t=>2); $line = Formula("$m (t - 2) + $y0"); # INITIALIZATION FOR PART 2 # set up for a multiple choice problem. $radio = new_multiple_choice(); $radio->qa("This problem is", "easy"); $radio->extra("very easy", "hard"); $radio->makeLast("impossible"); # INITIALIZATION FOR PART 3 Context()->strings->add(True=>{},False=>{}); $strAns = String('True'); |
This is the
For part 1, we define a function of the variable t. By default,
Note: we're using a number of characteristics of Formulas here.
For part 2, we define a radio object to include a multiple choice problem. The For part 3, we are asking the student to enter a String answer. To avoid error messages we first add to the Context all valid strings (in this case, the strings "True" and "False"). Then we define a String object which gives the correct answer. |

TEXT(&beginproblem); Context()->texStrings; BEGIN_TEXT ${BBOLD}Part 1$EBOLD $BR Find the equation of the line tangent to \(f(t) = $func\) at \(t=2\). $BR \(y = \) \{ ans_rule(35) \} (note that this must be a function of \(t\).) $PAR ${BBOLD}Part 2$EBOLD $BR \{ $radio->print_q() \} \{ $radio->print_a() \} $PAR ${BBOLD}Part 3$EBOLD $BR (Enter True or False: ) All instructors love WeBWorK. \{ ans_rule(6) \} END_TEXT Context()->normalStrings; |
This is the
For Part 1, we display the equation in-line with
For Part 2, we can just use the radio button object to print the question and options. Recall that For Part 3, we just ask a question and insert an answer rule. |

ANS( $trigDeriv->cmp() ); ANS( radio_cmp( $radio->correct_ans() ) ); ANS( $strAns->cmp() ); Context()->texStrings; SOLUTION(EV3(<<'END_SOLUTION')); $PAR SOLUTION $PAR ${BBOLD}Part 1$EBOLD $BR The derivative of \(f(t) = $func\) is \(f'(t) = $funcDeriv\), so the slope of the line is \(f'(2) = $m\). Then when \(t = 2\) we have \(f(2) = $y0\), so the equation of the line is \(y = $line\). $PAR ${BBOLD}Part 2$EBOLD $BR This problem is clearly easy. $PAR ${BBOLD}Part 3$EBOLD $BR A careful study of one WeBWorK instructor showed that 100% of all respondents thought this statement was true. END_SOLUTION Context()->normalStrings; ENDDOCUMENT(); |
This is the
For the first part of the problem, we want to check that the student entered the right equation for the line. We therefore use the comparison method of the
For the second part of the problem we have to check that the student entered the correct (multiple-choice) answer. At the moment this requires that we use an For part 3, we just check that the answer the student submitted compares with the expected String. Finally, note that we can use any formatting that we use in the text section of the problem in the solution. |