Difference between revisions of "Vectors"
m |
m |
||
Line 15: | Line 15: | ||
<tr valign="top"> |
<tr valign="top"> |
||
− | <td style="background-color:# |
+ | <td style="background-color:#ddffdd;border:black 1px dashed;"> |
<pre> |
<pre> |
||
loadMacros( |
loadMacros( |
||
Line 25: | Line 25: | ||
</pre> |
</pre> |
||
</td> |
</td> |
||
− | <td style="background-color:# |
+ | <td style="background-color:#ccffcc;padding:7px;"> |
<p> |
<p> |
||
Be sure to load <code>MathObjects.pl</code> and <code>parserVectorUtils.pl</code> |
Be sure to load <code>MathObjects.pl</code> and <code>parserVectorUtils.pl</code> |
||
Line 111: | Line 111: | ||
ANS( $v2->cmp( checker=>sub { |
ANS( $v2->cmp( checker=>sub { |
||
my ($correct, $student, $ansHash) = @_; |
my ($correct, $student, $ansHash) = @_; |
||
− | return $ |
+ | return $correct->isParallel($student); |
}, showCoordinateHints => 0 ) ); |
}, showCoordinateHints => 0 ) ); |
||
## or: |
## or: |
||
# ANS( $v1->cmp( checker=>sub { |
# ANS( $v1->cmp( checker=>sub { |
||
# my ($correct, $student, $ansHash) = @_; |
# my ($correct, $student, $ansHash) = @_; |
||
− | # return $ |
+ | # return $correct.$student == 0; } ) ); |
# make a custom answer checker as a subroutine |
# make a custom answer checker as a subroutine |
||
sub parallel_vector_cmp { |
sub parallel_vector_cmp { |
||
my ($correct, $student, $ansHash) = @_; |
my ($correct, $student, $ansHash) = @_; |
||
− | return $ |
+ | return $correct->isParallel($student); |
} |
} |
||
ANS( $v3->cmp( checker=>~~¶llel_vector_cmp, |
ANS( $v3->cmp( checker=>~~¶llel_vector_cmp, |
Revision as of 13:01, 4 December 2009
Vectors in Problems: PG Code Snippet
This code snippet shows the essential PG code to use vectors in WeBWorK problems. 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.
PG problem file | Explanation |
---|---|
loadMacros( "PGstandard.pl", "PGcourse.pl", "MathObjects.pl", "parserVectorUtils.pl", ); |
Be sure to load |
Context('Vector'); ## display vectors in ijk format # Context()->flags->set( ijk=>1 ); ## set the appearance of the ijk vectors ## this sets them to be overset with ## vector arrows, instead of boldface # Context()->constants->set( # i => {TeX => "\mathit{\vec i}"}, # j => {TeX => "\mathit{\vec j}"}, # k => {TeX => "\mathit{\vec k}"}, # ); $v1 = Vector("<1,3>"); $v2 = Compute("<-3,1>"); $v3 = 3*i + 2*j - 4*k; $v4 = Vector(1,1,0); # create an array of the components of $v3 @v3comp = $v3->value; $a = 3*i + j; $b = $a + $v1; $c = norm($v3); # vector length $v5 = unit($v3); # unit vector in same direction $d = $v1 . $v2; # dot product $v6 = $v3 x $v4; # cross product $v3->isParallel($v4); # =1 if parallel, =0 if skew |
We indicate that we are working in a vector context by setting the
Then, we can define vectors as we might expect: either with the
Note that if we define the vector using the constants i, j and k, as in the definition of
To explicitly require that the vectors be two-dimensional rather than three-dimensional, we would use
The components of the vectors are available as an array from |
BEGIN_TEXT Enter the vector pointing from \($a\) to \($b\): \{ ans_rule(25) \} $PAR Enter a vector perpendicular to this: \{ ans_rule(25) \} $PAR Enter a vector parallel to \($v3\): \{ ans_rule(25) \} |
We can then use the vectors that we created in the text section of the problem. |
ANS( $v1->cmp() ); ANS( $v2->cmp( checker=>sub { my ($correct, $student, $ansHash) = @_; return $correct->isParallel($student); }, showCoordinateHints => 0 ) ); ## or: # ANS( $v1->cmp( checker=>sub { # my ($correct, $student, $ansHash) = @_; # return $correct.$student == 0; } ) ); # make a custom answer checker as a subroutine sub parallel_vector_cmp { my ($correct, $student, $ansHash) = @_; return $correct->isParallel($student); } ANS( $v3->cmp( checker=>~~¶llel_vector_cmp, showCoordinateHints => 0 ) ); |
We can then use the vectors to check the answers that are given. Note that we have used custom answer checkers for the latter answer evaluators here, taking advantage of the built in dot product and Other properties of MathObjects vectors are given in the MathObjects reference table. |