From WeBWorK_wiki
Revision as of 15:34, 14 June 2015 by Paultpearson (talk | contribs) (PGML example link)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Vector Operations

Click to enlarge

This PG code shows how to extract the components of a constant vector, take dot and cross products of vectors, find the length of a vector, construct a unit vector, and check whether the student's answer is parallel to or in the same direction as another vector.

Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:




Initialization: We load to have access to functions on vectors like norm and unit. Since this question has many parts, we use to display it nicely.


$U = non_zero_vector3D(-9,9,1);
$V = non_zero_vector3D(-9,9,1);

# value works only for vectors of constants
@Uarray = $U->value; 
$Ucomp2 = $Uarray[1];

$UdotV = $U . $V;
$UcrossV = $U x $V;
$Vlength = norm( $V );
$Vunit = unit($V);

#  Prevent students from entering the dot and 
#  cross products, and the vector functions
#  norm and unit.

Setup: We use non_zero_vector3D(low,high,increment) to randomly generate some vectors. Calling $U->value returns a (Perl) array of numbers. (Note that ->value does not work on a vector whose components are non-constant formulas.) MathObjects defines the operators . and x to be the dot product and cross product when they occur between two vectors (that is, these operations are overloaded). The functions norm and unit calculate the length of a vector and a unit vector in the same direction. We undefine the dot and cross product as well as the functions norm and unit so that students cannot enter them in their answers.

Suppose \( \vec{u} = $U \) and \( \vec{v} = $V \). 
\{ BeginList('OL', type=>'A') \}

$ITEM The second component of \( \vec{u} \) is 
\{ ans_rule(20) \}

$ITEM \( \vec{u} \cdot \vec{v} = \) 
\{ ans_rule(20) \}

$ITEM \( \vec{u} \times \vec{v} = \) 
\{ ans_rule(20) \}

$ITEM \( \left|\left| \vec{v} \right|\right| = \) 
\{ ans_rule(20) \}

$ITEM Enter a unit vector in the direction of \( \vec{v} \).
\{ ans_rule(20) \}

$ITEM Enter a vector parallel to \( \vec{v} \).
\{ ans_rule(20) \}

$ITEM Enter a vector in the same direction as \( \vec{v} \).
\{ ans_rule(20) \}

\{ EndList('OL') \}

Main Text:

$showPartialCorrectAnswers = 1;

ANS( $Ucomp2->cmp() );
ANS( $UdotV->cmp() );
ANS( $UcrossV->cmp() );
ANS( $Vlength->cmp() );
ANS( $Vunit->cmp() );
ANS( $V->cmp( parallel=>1 ) );
ANS( $V->cmp( parallel=>1, sameDirection=>1 ) );

Answer Evaluation: In the last two answers we set flags for checking whether the student's answer is parallel to or in the same direction as the correct answer. Notice that both flags parallel=>1, sameDirection=>1 must be set in the last answer.

Solution explanation goes here.

COMMENT('MathObject version.');



Templates by Subject Area