Unit Conversion

Answer is a number or formula with units

Complete Code

Download file: UnitConversion.pg

POD for Macro Files

PG problem file

Explanation

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'contextUnits.pl', 'PGcourse.pl');

Preamble

We load contextUnits.pl in order to use the Units context.

Context('Units')->withUnitsFor('volume');

$v        = random(25, 200, 25);
$vol      = Compute("$v ml");
$vol_cups = $vol->toUnits('cup');

Context('Units')->withUnitsFor('length', 'time');

$s         = random(22, 110, 11);
$speed     = Compute("$s ft/s");
$speed_mph = $speed->toUnits('mi/h');

Setup

The Units context is from the contextUnits.pl macro. This is related to the ‘Numeric’ context with the ability to handle units. The first question involves volume units so we set that.

The ->toUnits method converts the given number with units to the requested unit.

The second part below is a different conversion question, so the line Context('Units')->withUnitsFor('length', 'time'); resets the contxt with a new set of units.

BEGIN_PGML
Perform the following unit conversions:

a) Convert [$vol] to cups.  You may use [`1\;\text{l} = 1000\;\text{ml}`],
[`1\;\text{qt} =  0.9464\;\text{l}`] and [`1\;\text{qt}=4\;\text{cups}`].

    Answer: [_]{$vol_cups->with(sameUnits => 1)}

b) Convert [$speed] to miles per hour.  Recall that
[`1\;\text{mile}=5280\;\text{feet}`].

    Answer: [_]{$speed_mph->with(sameUnits => 1)}

Note: use units in all answers. [@ helpLink('units') @]*
END_PGML

Statement

The with(sameUnits =>1) requires the student answer to be in the same units. Use helpLink('units') for students to have access to the complete list of units that WeBWorK understands.

BEGIN_PGML_SOLUTION
Using dimensional analysis:

a) [```
[$vol] \cdot \frac{1\;\text{l}}{1000\;\text{ml}}
\cdot \frac{1\;\text{qt}}{0.9464\;\text{l}}
\cdot \frac{4\;\text{cups}}{1\;\text{qt}} = [$vol_cups]
```]

b) [```
[$speed] \cdot \frac{1\;\text{mile}}{5280\;\text{ft}}
\cdot \frac{3600\;\text{sec}}{1\;\text{hr}} = [$speed_mph]
```]
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.