WeBWorK Main Forum

Matrix entries disappearing in gateway exams

Matrix entries disappearing in gateway exams

by Jason Aubrey -
Number of replies: 3
Hi All,

We have encountered a problem with the webwork problem below when it is part of a gateway. If a student enters answers into the matrix boxes, then leaves the problem and comes back, those entries disappear. Any ideas? The source code for the problem is below.

Thanks,
Jason

## DESCRIPTION
## Eigenvalues and systems of differential equations
## ENDDESCRIPTION


## DBsubject(Differential equations)
## DBchapter(Systems of differential equations)
## DBsection(Complex eigenvalues)
## Date(01/31/2012)
## Institution(Rochester and Fort Lewis College)
## Author(Paul Pearson)
## Level(4)
## MO(1)
## TitleText1('Elementary Differential Equations')
## AuthorText1('Kohler and Johnson')
## EditionText1('2006')
## Section1('4.6')
## Problem1('20')


##################################
# Initialization

DOCUMENT();

loadMacros(
"PGstandard.pl",
"PGmatrixmacros.pl",
"MathObjects.pl",
"parserMultiAnswer.pl",
"unionLists.pl",
"PGcourse.pl"
);


TEXT(beginproblem());



#####################################
# Setup 1

Context("Complex");

# dilation
$a = random(-1,1,2);

$A11 = 3 * $a; $A12 = 2 * $a;
$A21 = -5 * $a; $A22 = -3 * $a;

$lambda1 = Complex("$a i");
$v1top = Complex("3 + i");
$v1bot = Complex("-5");

$lambda2 = Complex("- $a i");
$v2top = Complex("3 - i");
$v2bot = Complex("-5");

$multians1 = MultiAnswer($lambda1, $v1top, $v1bot, $lambda2, $v2top, $v2bot)->with(
singleResult => 1,
checkTypes => 0,
allowBlankAnswers => 1,
format =>
"<table border='0' cellspacing='0'>
<tr>
<td> %s, </td>
<td><table border='0' cellspacing='5'><tr><td> %s </td></tr><tr><td> %s </td></tr></table></td>
<td>&nbsp;&nbsp; %s,</td>
<td><table border='0' cellspacing='5'><tr><td> %s </td></tr><tr><td> %s </td></tr></table></td>
</tr>
</table>",
tex_format =>
"%s, \left\lbrack \begin{array}{r} %s \\ %s \end{array}\right\rbrack,
%s, \left\lbrack \begin{array}{r} %s \\ %s \end{array}\right\rbrack",
checker => sub {
my ($correct, $student, $answerHash ) = @_;
my @c = @{$correct};
my @s = @{$student};

Value::Error("Your vectors are parallel") if
$c[1]->typeMatch($s[1])
&& $c[2]->typeMatch($s[2])
&& $c[4]->typeMatch($s[4])
&& $c[5]->typeMatch($s[5])
&& ($s[1] * $s[5] == $s[2] * $s[4]);

if ( $c[0]->typeMatch($s[0])
&& $c[1]->typeMatch($s[1])
&& $c[2]->typeMatch($s[2])
&& ($s[1] != 0 || $s[2] != 0)
&& ($A11 * $s[1] + $A12 * $s[2] == $s[0] * $s[1])
&& ($A21 * $s[1] + $A22 * $s[2] == $s[0] * $s[2])
)
{ $flag1 = 1; }

if ( $c[3]->typeMatch($s[3])
&& $c[4]->typeMatch($s[4])
&& $c[5]->typeMatch($s[5])
&& ($s[4] != 0 || $s[5] != 0)
&& ($A11 * $s[4] + $A12 * $s[5] == $s[3] * $s[4])
&& ($A21 * $s[4] + $A22 * $s[5] == $s[3] * $s[5])
)
{ $flag2 = 1; }

if ($flag1==1 && $flag2 ==1) {
return 1;
} elsif ($flag1 == 1) {
Value::Error("The eigenpair { lambda1, v1 } is correct");
} elsif ($flag2 == 1) {
Value::Error("The eigenpair { lambda2, v2 } is correct");
}else{
Value::Error("Neither of the eigenpairs is correct");
}
 
}
);





#####################################
# Main text 1

Context()->texStrings;
BEGIN_TEXT
Consider the linear system
$PAR
\[
\displaystyle
\boldsymbol{ \vec{y}^{\,\prime} }
=
\left\lbrack
\begin{array}{rr}
$A11 & $A12 \\
$A21 & $A22
\end{array}
\right\rbrack
\boldsymbol{\vec{y}}.
\]
$BR

\{ BeginList("OL",type=>"a") \}

$ITEM
Find the eigenvalues and eigenvectors for the coefficient matrix.
$BCENTER
\{
mbox(
[
'\( \lambda_1 = \) ',
$multians1->ans_rule(5),
', \( \boldsymbol{\vec{v}_1} = \) ',
display_matrix([[$multians1->ans_rule(5)],[$multians1->ans_rule(5)]]),
', and \( \lambda_2 = \) ',
$multians1->ans_rule(5),
', \( \boldsymbol{\vec{v}_2} = \) ',
display_matrix([[$multians1->ans_rule(5)],[$multians1->ans_rule(5)]]),
],
allowbreaks=>'yes'
)
\}
$ECENTER
END_TEXT
Context()->normalStrings;


##################################
# Answer evaluation 1

$showPartialCorrectAnswers = 1;

ANS( $multians1->cmp() );





##################################
# Setup 2

Context("Numeric")->variables->are(t=>"Real");

$c1 = non_zero_random(-3,3,1);
do { $c2 = non_zero_random(-3,3,1); } until ($c2 != $c1);

$y0top = 3 * $c1 + $c2;
$y0bot = -5 * $c1;

$y0top = 3 * $c1 + $c2;
$y0bot = -5 * $c1;

$ytop = Formula("$c1 (3 cos($a t) - sin($a t)) + $c2 (cos($a t) + 3 sin($a t))");
$ybot = Formula("$c1 (-5 cos($a t)) + $c2 (-5 sin($a t))");


$multians2 = MultiAnswer($ytop, $ybot)->with(
singleResult => 1,
checkTypes => 0,
allowBlankAnswers => 1,
format =>
"<table border='0' cellspacing='5'><tr><td> %s </td></tr><tr><td> %s </td></tr></table>",
tex_format =>
"\begin{array}{l} y_1(t) = %s \\ y_2(t) = %s \end{array}",
checker => sub {
my ($correct, $student, $self ) = @_;
my @c = @{$correct};
my @s = @{$student};
 
$flag1 = 0;
$flag2 = 0;

if ( $c[0]->typeMatch($s[0]) && ($c[0] == $s[0]) ) { $flag1 = 1; }
if ( $c[1]->typeMatch($s[1]) && ($c[1] == $s[1]) ) { $flag2 = 1; }

if ($flag1==1 && $flag2 ==1) {
return 1;
} elsif ($flag1 == 1) {
Value::Error("Solution y<sub>1</sub> is correct");
} elsif ($flag2 == 1) {
Value::Error("Solution y<sub>2</sub> is correct");
}else{
Value::Error("Neither of the solutions is correct");
}
 
}
);




Context()->texStrings;
BEGIN_TEXT
$ITEMSEP
$ITEM
Find the real-valued solution to the initial value problem
$PAR
\[
\left\lbrace
\begin{array}{rcrcl}
y_1^{\,\prime}
& = &
$A11 y_1 + $A12 y_2,
& &
y_1(0) = $y0top,
\\
y_2^{\,\prime}
& = &
$A21 y_1 + $A22 y_2,
& &
y_2(0) = $y0bot.
\end{array}
\right.
\]
$BR
Use \( t \) as the independent variable in your answers.
$BR
$BR
\( y_1(t) = \) \{ $multians2->ans_rule(50) \}
$BR
\( y_2(t) = \) \{ $multians2->ans_rule(50) \}

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

#####################################
# Answer evaluation


ANS( $multians2->cmp() );


COMMENT('Complex eigenvalues +/- i and complex eigenvectors.');
BEGIN_SOLUTION
We first find the eigenvalues of the coefficient matrix, to do this, we find the solutions of \(det(A-rI) = 0\), that is
\[
det(\begin{bmatrix}
$A11-r & $A12 \\
$A21 & $A22-r
\end{bmatrix}) = ($A11-r)($A22-r)-$A12*$A21 = r^2-\{$A11+$A22\}r+\{$A11*$A22-$A12*$A21\} = 0
\]
From the quadratic formula we find that our eigenvalues are \(r = \pm $a i\)

To find an eigenvector for our first eigenvalue \($a i\), we solve
\[
A-$a i I = \begin{bmatrix}
$A11-$a i & $A12 \\
$A21 & $A22-$a i
\end{bmatrix} = \begin{bmatrix}
0\\
0
\end{bmatrix}
\]
After some row reduction, we find our first eigenvector to be
\[
\begin{bmatrix}
3+i\\
-5
\end{bmatrix}
\]
To find an eigenvector for our second eigenvalue \($lambda2\), we solve
\[
A+$a i I = \begin{bmatrix}
$A11+$a i & $A12 \\
$A21 & $A22+$a i
\end{bmatrix} = \begin{bmatrix}
0\\
0
\end{bmatrix}
\]
After some row reduction, we find our second eigenvector to be
\[
\begin{bmatrix}
3-i\\
-5
\end{bmatrix}
\]
Now that we have our eigenvectors, we can find the general solution, which is
\[
\begin{bmatrix}
y_1\\
y_2
\end{bmatrix} =
c_1(\cos(t)\begin{bmatrix}
\{3\}\\
\{-5\}
\end{bmatrix}-\sin(t)
\begin{bmatrix}
-1\\
0
\end{bmatrix})
+ c_2(\sin(t) \begin{bmatrix}
\{3\}\\
\{-5\}
\end{bmatrix}+\cos(t)
\begin{bmatrix}
1\\
0
\end{bmatrix})
\]
To find the specific solution, we plug in our initial conditions and solve the system for \(c_1,c_2\), plugging in we get
\[
\begin{bmatrix}
$y0top\\
$y0bot
\end{bmatrix}
= c_1 \begin{bmatrix}
\{3\}\\
\{-5\}
\end{bmatrix}+c_2\begin{bmatrix}
\{1\}\\
\{0\}
\end{bmatrix}
\]
After row reduction we find \(c_1 = $c1, c_2 = $c2\), giving us a specific solution of
\[
\begin{bmatrix}
y_1\\
y_2
\end{bmatrix} =
$c1(\cos(t)\begin{bmatrix}
\{3\}\\
\{-5\}
\end{bmatrix}-\sin(t)
\begin{bmatrix}
-1\\
0
\end{bmatrix})
+ $c2(\sin(t) \begin{bmatrix}
\{3\}\\
\{-5\}
\end{bmatrix}+\cos(t)
\begin{bmatrix}
1\\
0
\end{bmatrix})
\]
END_SOLUTION
ENDDOCUMENT();

In reply to Jason Aubrey

Re: Matrix entries disappearing in gateway exams

by Michael Gage -
I've added this to issue #924
because I believe they are related.

You can also view the task board
for similar bug fixes which are being worked on and/or need a volunteer.


for the general task board that Danny Glin has set up.

-- Mike
In reply to Jason Aubrey

Re: Matrix entries disappearing in gateway exams

by Glenn Rice -
This is fixed by my pending pull request https://github.com/openwebwork/webwork2/pull/935.
In reply to Glenn Rice

Re: Matrix entries disappearing in gateway exams

by Michael Gage -
I was about to report Glenn's patch. I have just pulled it into master and submitted and pulled a duplicate into develop.

Danny Glin's comment about trying to refactor the way past answers and sticky answers are stored so that Problem.pm and GatewayQuiz.pm can use exactly the same subroutines is still relevant.

Refactoring will make maintenance easier and might possibly clean up some of the squirrelier aspects of the code. Is anyone interested?

Also see https://github.com/orgs/openwebwork/projects/6 where this issue has now been moved to "Done". :-)