Difference between revisions of "Sage in WeBWorK"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 4: Line 4:
   
 
## First Homework Problem File for
 
## First Homework Problem File for
:## Calculus
+
## Calculus
:## Partial Derivatives
+
## Partial Derivatives
:## Unit 1
+
## Unit 1
:##
+
##
+
DOCUMENT();
+
DOCUMENT();
+
loadMacros(
+
loadMacros(
"PGstandard.pl",
+
"PGstandard.pl",
"PGchoicemacros.pl",
+
"PGchoicemacros.pl",
"MathObjects.pl",
+
"MathObjects.pl",
);
+
);
+
Context()->strings->add(none=>{});
+
Context()->strings->add(none=>{});
+
TEXT(beginproblem());
+
TEXT(beginproblem());
+
$x0 = non_zero_random(-2,2,1);
+
$x0 = non_zero_random(-2,2,1);
$y0 = non_zero_random(-2,2,1);
+
$y0 = non_zero_random(-2,2,1);
+
$f0 = ($x0**3-$y0**3)/($x0**2+$y0**2+1);
+
$f0 = ($x0**3-$y0**3)/($x0**2+$y0**2+1);
+
TEXT(<<EOF);
+
TEXT(<<EOF);
<div id="singlecell-test"><script type="text/code">
+
<div id="singlecell-test"><script type="text/code">
+
+
######### Sage code pasted starting here ##########
+
######### Sage code pasted starting here ##########
+
var('x,y,t,s')
+
var('x,y,t,s')
+
#
+
#
+
M=x*y
+
M=x*y
N=-y
+
N=-y
+
@interact(layout=dict(left= [['x0'],['y0'],['delx'],['dely']],
+
@interact(layout=dict(left= [['x0'],['y0'],['delx'],['dely']],
bottom=[['xx'],['yy']]))
+
bottom=[['xx'],['yy']]))
def _( x0 = input_box(0,width=5,label='$x_0$'),
+
def _( x0 = input_box(0,width=5,label='$x_0$'),
y0 = input_box(0,width=5,label='$y_0$'),
+
y0 = input_box(0,width=5,label='$y_0$'),
delx = input_box(1,width=5,label='$\Delta{x}$'),
+
delx = input_box(1,width=5,label='$\Delta{x}$'),
dely = input_box(1,width=5,label='$\Delta{y}$'),
+
dely = input_box(1,width=5,label='$\Delta{y}$'),
xx = range_slider(-5, 5, 1, default=(-2,2), label='x Range'),
+
xx = range_slider(-5, 5, 1, default=(-2,2), label='x Range'),
yy = range_slider(-5, 5, 1, default=(-1,3), label='y Range')):
+
yy = range_slider(-5, 5, 1, default=(-1,3), label='y Range')):
+
G = plot_vector_field((M,N),(x,xx[0],xx[1]),(y,yy[0],yy[1]),aspect_ratio=true)
+
G = plot_vector_field((M,N),(x,xx[0],xx[1]),(y,yy[0],yy[1]),aspect_ratio=true)
G += arrow((x0,y0),(x0+delx,y0+dely))
+
G += arrow((x0,y0),(x0+delx,y0+dely))
show(G)
+
show(G)
+
+
############## End of Sage Code ######################
+
############## End of Sage Code ######################
+
+
</script></div>
+
</script></div>
+
<script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
+
<script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script>
+
<script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script>
+
<script type="text/javascript">
+
<script type="text/javascript">
$(function() { // load only when the page is loaded
+
$(function() { // load only when the page is loaded
var makecells = function() {
+
var makecells = function() {
singlecell.makeSinglecell({
+
singlecell.makeSinglecell({
inputLocation: "#singlecell-test",
+
inputLocation: "#singlecell-test",
editor: "codemirror",
+
editor: "codemirror",
hide: ["editor","computationID","files","messages","sageMode"],
+
hide: ["editor","computationID","files","messages","sageMode"],
evalButtonText: "Start/Restart",
+
evalButtonText: "Start/Restart",
replaceOutput: true});
+
replaceOutput: true});
}
+
}
+
singlecell.init(makecells); // load Single Cell libraries and then
+
singlecell.init(makecells); // load Single Cell libraries and then
// initialize Single Cell instances
+
// initialize Single Cell instances
+
});
+
});
</script>
+
</script>
EOF
+
EOF
+
############### Below is the normal WebWork pg stuff #####################
+
############### Below is the normal WebWork pg stuff #####################
+
Context()->texStrings;
+
Context()->texStrings;
BEGIN_TEXT
+
BEGIN_TEXT
Using the contour plot below, determine the range value of the illustrated function at \( ($x0,$y0) \).
+
Using the contour plot below, determine the range value of the illustrated function at \( ($x0,$y0) \).
$BR $BR
+
$BR $BR
\( f($x0,$y0) = \)\{ ans_rule(15) \}
+
\( f($x0,$y0) = \)\{ ans_rule(15) \}
$PAR
+
$PAR
END_TEXT
+
END_TEXT
Context()->normalStrings;
+
Context()->normalStrings;
+
+
# need to add reasonable approximation error of about 0.1 or so.
# need to add reasonable approximation error of about 0.1 or so.
+
ANS( Compute($f0)->cmp() );
ANS( Compute($f0)->cmp() );
+
+
ENDDOCUMENT(); # This should be the last executable line in the problem.
 
 
ENDDOCUMENT(); # This should be the last executable line in the problem.
 
 
 
   
 
To pass perl variables to the sage block if you need to from the problem initialization use:
 
To pass perl variables to the sage block if you need to from the problem initialization use:

Revision as of 17:30, 30 December 2011

Sage is an open source, online symbolic mathematical system. Details on Sage can be found at http://www.sagemath.org .

For use within WebWork, a special "single-cell" version of Sage is located at

##  First Homework Problem File for
##  Calculus
##  Partial Derivatives
##  Unit 1
## 

DOCUMENT();

loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
"MathObjects.pl",
);

Context()->strings->add(none=>{});

TEXT(beginproblem()); 

$x0 = non_zero_random(-2,2,1);  
$y0 = non_zero_random(-2,2,1);   

$f0 = ($x0**3-$y0**3)/($x0**2+$y0**2+1);

TEXT(<<EOF);
<script type="text/code">


#########  Sage code pasted starting here ##########

var('x,y,t,s')

# 

M=x*y
N=-y
      
@interact(layout=dict(left= [['x0'],['y0'],['delx'],['dely']],
bottom=[['xx'],['yy']]))
def _(  x0 = input_box(0,width=5,label='$x_0$'),
        y0 = input_box(0,width=5,label='$y_0$'),
        delx = input_box(1,width=5,label='$\Delta{x}$'),
        dely = input_box(1,width=5,label='$\Delta{y}$'),
        xx = range_slider(-5, 5, 1, default=(-2,2), label='x Range'),
        yy = range_slider(-5, 5, 1, default=(-1,3), label='y Range')):
            
    G = plot_vector_field((M,N),(x,xx[0],xx[1]),(y,yy[0],yy[1]),aspect_ratio=true)
    G += arrow((x0,y0),(x0+delx,y0+dely))
    show(G)


##############  End of Sage Code ###################### 


</script>
   <script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
   <script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script> 

   <script type="text/javascript">
$(function() { // load only when the page is loaded
  var makecells = function() {
  singlecell.makeSinglecell({
      inputLocation: "#singlecell-test",
      editor: "codemirror",
      hide: ["editor","computationID","files","messages","sageMode"],
      evalButtonText: "Start/Restart",
      replaceOutput: true});
  }

  singlecell.init(makecells); // load Single Cell libraries and then
                              // initialize Single Cell instances

  });
  </script>
EOF

############### Below is the normal WebWork pg stuff #####################

Context()->texStrings;
BEGIN_TEXT
Using the contour plot below, determine the range value of the illustrated function at \( ($x0,$y0) \).
$BR $BR
\( f($x0,$y0) = \)\{ ans_rule(15) \} 
$PAR
END_TEXT
Context()->normalStrings;

#  need to add reasonable approximation error of about 0.1 or so.
ANS( Compute($f0)->cmp() );

ENDDOCUMENT();        # This should be the last executable line in the problem.

To pass perl variables to the sage block if you need to from the problem initialization use:

TEXT(<<EOF);

where <<EOF allows interpolation

otherwise use:

TEXT(<<'EOF');

where 'EOF' tells perl not to interpolate variables