Difference between revisions of "VectorParametric2"

From WeBWorK_wiki
Jump to navigation Jump to search
(Update custom answer checker.)
Line 66: Line 66:
 
<pre>
 
<pre>
 
Context("Vector2D");
 
Context("Vector2D");
#Context("Vector"); # for 3D vectors
+
#Context("Vector"); # for 3d vectors
 
Context()->variables->are(t=>"Real");
 
Context()->variables->are(t=>"Real");
 
Context()->variables->set(t=>{limits=>[0,5]});
 
Context()->variables->set(t=>{limits=>[0,5]});
Context()->flags->set( ijk=>0 );
+
Context()->flags->set( ijk=>0, ijkAnyDimension => 1 );
   
 
$answer = Vector("<2t,(2t)^2>");
 
$answer = Vector("<2t,(2t)^2>");
Line 77: Line 77:
 
<p>
 
<p>
 
<b>Setup:</b>
 
<b>Setup:</b>
We choose not to display the answer using ijk notation.
 
  +
We choose not to display the answer using ijk notation. Also, use <code>ijkAnyDimension => 1</code> to require a dimension match between i,j,k vectors and either the student or the correct answer when doing vector operations.
 
</p>
 
</p>
 
</td>
 
</td>
Line 114: Line 114:
 
<pre>
 
<pre>
 
$showPartialCorrectAnswers = 1;
 
$showPartialCorrectAnswers = 1;
 
sub components {
 
 
my $V = shift;
 
$V = $V->perl;
 
 
if ( $V =~ m/Value/ ) {
 
 
$V =~ s/Value::Vector->new~~(//g;
 
$V = substr($V, 0, -1);
 
$V =~ s/Value::Real->new//g;
 
$V =~ s/~~$//g;
 
$V =~ s/ //g;
 
return split(',',$V);
 
 
} else {
 
 
$V =~ s/~~* i~~)/~~),/g;
 
$V =~ s/~~* j~~)/~~),/g;
 
$V =~ s/~~* k~~)/~~),/g;
 
$V =~ s/~~$//g;
 
$V =~ s/ //g;
 
$V = substr($V, 0, -1);
 
return split(',',$V);
 
 
}
 
 
}
 
 
   
 
sub mycheck {
 
sub mycheck {
 
my ($correct, $student, $ansHash) = @_;
 
my ($correct, $student, $ansHash) = @_;
my @r = components($student);
+
my $xstu = $student . Vector(1,0);
my $xstu = Formula("$r[0]");
+
my $ystu = $student . Vector(0,1);
my $ystu = Formula("$r[1]");
 
 
if ( ($xstu->D('t')==Formula("2")) &&
 
if ( ($xstu->D('t')==Formula("2")) &&
 
($ystu->D('t')==Formula("8t")) )
 
($ystu->D('t')==Formula("8t")) )
Line 158: Line 129:
 
<p>
 
<p>
 
<b>Answer Evaluation:</b>
 
<b>Answer Evaluation:</b>
The subroutine components <code>components($student)</code> extracts the components of a vector and returns an array of (Perl) strings, which we assign to the array <code>@r</code> inside the custom answer checker <code>mycheck</code>. Each of these (Perl) strings <code>$r[0]</code> and <code>$r[1]</code> is then made into a MathObject formula and assigned a name. Since <code>$xstu</code> and <code>$ystu</code> are MathObjects formulas representing the components of the student's answer, we can differentiate them just like any MathObject formula. Notice that the argument to <code>cmp( checker => ~~&mycheck )</code> is our subroutine that is a custom answer checker.
+
Use dot products of the student answer with the vectors <code>Vector(1,0)</code> and <code>Vector(0,1)</code> to get the components <code>$xstu</code> and <code>$ystu</code> of the student answer. Then, we can differentiate the components just like any MathObject formula. Notice that the argument to <code>cmp( checker => ~~&mycheck )</code> is our subroutine that is a custom answer checker.
 
</p>
 
</p>
 
</td>
 
</td>

Revision as of 16:51, 7 June 2015

Motion and Velocity with a Parametric Curve

Click to enlarge

This PG code shows how to construct a custom answer checker that extracts the component functions from the student's answer and makes some derivative calculations with them.


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT();

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

TEXT(beginproblem());

Initialization: Although not necessary for the code below, we load parserVectorUtils.pl because you may want to use some of its methods when you use this template file.

Context("Vector2D");
#Context("Vector"); # for 3d vectors
Context()->variables->are(t=>"Real");
Context()->variables->set(t=>{limits=>[0,5]});
Context()->flags->set( ijk=>0, ijkAnyDimension => 1 );

$answer = Vector("<2t,(2t)^2>");

Setup: We choose not to display the answer using ijk notation. Also, use ijkAnyDimension => 1 to require a dimension match between i,j,k vectors and either the student or the correct answer when doing vector operations.

Context()->texStrings;
BEGIN_TEXT
Find a vector parametric function \( \vec{r}(t) \) 
for a bug that moves along the parabola \( y = x^2 \) 
with velocity \( \vec{v}(t) = \langle 2, 8t \rangle \) 
for all \( t \).
$BR
$BR
\( \vec{r}(t) = \) 
\{ ans_rule(20) \}
\{ AnswerFormatHelp("vectors") \} 
END_TEXT
Context()->normalStrings;

Main Text:

$showPartialCorrectAnswers = 1;

sub mycheck {
  my ($correct, $student, $ansHash) = @_;
  my $xstu = $student . Vector(1,0);
  my $ystu = $student . Vector(0,1);
  if ( ($xstu->D('t')==Formula("2")) &&
       ($ystu->D('t')==Formula("8t")) )
  { return 1; } else { return 0; } 
}

ANS( $answer->cmp( checker=>~~&mycheck ) );

Answer Evaluation: Use dot products of the student answer with the vectors Vector(1,0) and Vector(0,1) to get the components $xstu and $ystu of the student answer. Then, we can differentiate the components just like any MathObject formula. Notice that the argument to cmp( checker => ~~&mycheck ) is our subroutine that is a custom answer checker.

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

COMMENT('MathObject version.');

ENDDOCUMENT();

Solution:

Templates by Subject Area