## BEGIN_DESCRIPTION
## Sample problem embedding Sage in WW
##
## This problem shows a method of passing the student's
## answer from the work they've done in the SCS
## back to WW for grading transparently to the student.
## It requires hard coding of the input boxes.
##
## This problem asks the student to determine experimentally
## the appropriate coefficients (stored in the vector x) so that
## b lies in the span of the columns of A. That is, Ax = b.
## END_DESCRIPTION
## KEYWORDS('sage', 'sagelet', 'span','linear combination')
## DBsubject('')
## DBchapter('')
## DBsection('')
## Date('June 2012')
## Author('John Travis')
## Institution('Mississippi College')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"problemRandomize.pl",
);
TEXT(beginproblem());
ProblemRandomize(when=>"Correct",onlyAfterDue=>0);
###########################################################
##
## pg initializations and regular WeBWorK code
$a11 = random(2,3,1/2);
$a12 = 1;
$a21 = random(-3,-1,1/2);
$a22 = non_zero_random(-2,5,1/20);
$A = Matrix([[$a11,$a12],[$a21,$a22]]);
$A1 = Vector($a11,$a21);
$x1 = non_zero_random(-2,2,1/20);
$x1ans = Compute("$x1");
$x2 = non_zero_random(-2,2,1/10);
$x2ans = Compute("$x2");
$x = Vector($x1,$x2);
$b1 = $a11*$x1+$a12*$x2;
$b2 = $a21*$x1+$a22*$x2;
$b = Vector($b1,$b2);
####################################################
##
## Upper WeBWorK text
##
## Problem display preceding the Sage cell
##
Context()->texStrings;
BEGIN_TEXT
Solve A x= b by finding coefficients for the columns of A which show b lies in their span.
$PAR
When solving \[Ax=b\]the solution \(x\) can be determined by determining the linear combination of the columns of A which equal b. In the interactive cell below \(A_1\) represents the first column of A while \(A_2\) represents the second column of A. Determine values of \(x_1\) and \(x_2\) so that
\[x_1 A_1 + x_2 A_2 = b \].
$PAR
END_TEXT
Context()->normalStrings;
###########################################################
##
## single cell server code
##
## The Sage code that is sent to the single cell server
## that returns the Sage out put desired.
## It is preprocessed by pg so use pg TeX constructs
## and escape @ to ~~@ etc
##
## TEXT(MODES(TeX=>"", HTML=><
The solution x for Ax=b is given by x1=\{ ans_rule(15) \} and x2=\{ans_rule(15) \}.
REPLACE_WITH_SCRIPT type="application/sage">
b = matrix([[$b1],[$b2]])
bt = b.transpose()
A=matrix([[$a11,$a12],[$a21,$a22]])
At =A.transpose()
# Notice the correct exact answer is given by x = A\b
# Finding when a vector b is in the span of other vectors in 2-space
~~@interact
def _(x1=slider(-3,3,1/20,1), x2=slider(-3,3,1/20,1)):
G = arrow((0,0),x1*At[0],rgbcolor=(0,0,1))
G += arrow(x1*At[0],x1*At[0]+x2*At[1],rgbcolor=(0,1,0))
G += arrow((0,0),($b1,$b2),rgbcolor=(1,0,0),width=5)
G += text("A1",(x1*At[0][0]/2,x1*At[0][1]/2),fontsize=30,color='purple')
G += text("A2",(x1*At[0][0]+x2*At[1][0]/2,x1*At[0][1]+x2*At[1][1]/2),fontsize=30,color='purple')
G += text("b",($b1/2,$b2/2),fontsize=40,color='purple')
G += point(x1*At[0],color='blue',pointsize=40)
G += point(($b1,$b2),color='red',pointsize=30)
G += point(x1*At[0]+x2*At[1],color='green',pointsize=40)
G += point(($b1,$b2),color='red',pointsize=20)
# Add fixed originals and dashed modified version of these
show(G,frame=False)
html('' %str(x1) )
html('' %str(x2) )
END_TEXT
## SAGE_CODE
###########################################################
##
## single cell server script
##
## script that sends the Sage code above to the
## single cell server and writes the return into
## the webpage
##
TEXT(MODES(TeX=>"", HTML=><<'SAGE_SCRIPT'));
REPLACE_WITH_SCRIPT src="http://aleph.sagemath.org/static/jquery.min.js">
REPLACE_WITH_SCRIPT src="http://aleph.sagemath.org/embedded_sagecell.js">
REPLACE_WITH_SCRIPT>
$(function () {
sagecell.makeSagecell({inputLocation: '#sagecell',
template: sagecell.templates.minimal,
autoeval: true,
evalButtonText: 'Reset the interactive display'});
});
SAGE_SCRIPT
####################################################
##
## Lower WeBWorK text
##
## Problem display following the Sage cell
##
Context()->texStrings;
BEGIN_TEXT
When you are comfortable with the coefficients that you have chosen, press
the submit button below.
END_TEXT
Context()->normalStrings;
#######################
# Answer Evaluation
$showPartialCorrectAnswers = 1;
ANS( $x1ans->cmp() );
ANS( $x2ans->cmp() );
###########################################################
##
## Hint(s), delete or comment if not used
##
Context()->texStrings;
$showHint = 2;
BEGIN_HINT
By adjusting the sliders, you are changing the length of the corresponding vector. Remember that a negative
coefficient makes the vector point in the opposite direction.
END_HINT
$showHint = 4;
$x1low = $x1-1/3;
$x1high = $x1+1/5;
BEGIN_HINT
Consider choosing a value for the first coefficient somewhere between $x1low and $x1high.
END_HINT
Context()->normalStrings;
###########################################################
##
## Solution, delete or comment if not used
##
Context()->texStrings;
BEGIN_SOLUTION
Notice that \(($x1) *A_1 + ($x2) *A_2 = $b\)
END_SOLUTION
Context()->normalStrings;
ENDDOCUMENT(); # This should be the last executable line in the problem.