VectorOperations1

From WeBWorK
Jump to: navigation, 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:

DOCUMENT(); 

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"parserVectorUtils.pl",
"unionLists.pl",
);

TEXT(beginproblem());

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

Context("Vector");

$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.
#
Context()->operators->undefine(".","><");
Context()->functions->disable("Vector");

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.

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

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

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

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

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

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

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

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

\{ EndList('OL') \}
END_TEXT
Context()->normalStrings;

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.

Context()->texStrings;
BEGIN_SOLUTION
Solution explanation goes here.
END_SOLUTION
Context()->normalStrings;

COMMENT('MathObject version.');

ENDDOCUMENT();

Solution:

Templates by Subject Area

follow us