Sage in WeBWorK: Difference between revisions

From WeBWorK_wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 17: Line 17:
);
);
   
   
Context()->strings->add(none=>{});
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);
 
 
######### Here is where special coding for Sage starts  ########
TEXT(<<'EOF');
 
 
<div id="singlecell-test">
<script type="text/code">
   
   
  TEXT(beginproblem());
######### Actual Sage code pasted starting here ##########
#########  This code should work in regular Sage ##########
$x0 = non_zero_random(-2,2,1)
 
$y0 = non_zero_random(-2,2,1)
 
var('x,y,z')
$f0 = ($x0**3-$y0**3)/($x0**2+$y0**2+1);
 
   
@interact(layout=dict(top=[['x0'],['y0']],
  TEXT(<<'EOF');
bottom=[['N'],['zoom_in']]))
def _(N=slider(5,100,1,10,label='Number of Contours'),
        zoom_in=checkbox(false,label='Zoom in'),
        x0=input_box(0,width=10,label='x coordinate of center'),
        y0=input_box(0,width=10,label='y coordinate of center')):
 
 
    f=(x^3-y^3)/(x^2+y^2+1)
    offset = floor(10*random())/20
 
    if zoom_in:
        surface = contour_plot(f,(x,x0-offset-1/10,x0+1/10),(y,y0-1/10,y0+offset+1/10), cmap=True,colorbar=True,fill=False,contours=N)   
    else:
        surface = contour_plot(f,(x,-3,3),(y,-3,3),cmap=True,colorbar=True,fill=False,contours=N)   
    limit_point = point((x0,y0),color='red',size=30)
 
    html.table([[surface+limit_point]])
    html('Contour Plot of $f(x,y)$ around $(%s'%str(x0)+',%s'%str(y0)+')$')
 
 
##############  End of Sage Code ######################
 
 
##############  More stuff to make sage work ##########
 
</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/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});
}


<div id="singlecell-test">
  singlecell.init(makecells); // load Single Cell libraries and then
<script type="text/code">
                              // initialize Single Cell instances
 
#########  Sage code pasted starting here ##########


var('x,y,z')
  });
@interact(layout=dict(top=[['x0'],['y0']],
bottom=[['N'],['zoom_in']]))
def _(N=slider(5,100,1,10,label='Number of Contours'),
        zoom_in=checkbox(false,label='Zoom in'),
        x0=input_box(0,width=10,label='x coordinate of center'),
        y0=input_box(0,width=10,label='y coordinate of center')):
    f=(x^3-y^3)/(x^2+y^2+1)
    offset = floor(10*random())/20
    if zoom_in:
        surface = contour_plot(f,(x,x0-offset-1/10,x0+1/10),(y,y0-1/10,y0+offset+1/10), cmap=True,colorbar=True,fill=False,contours=N)   
    else:
        surface = contour_plot(f,(x,-3,3),(y,-3,3),cmap=True,colorbar=True,fill=False,contours=N)   
    limit_point = point((x0,y0),color='red',size=30)
    html.table([[surface+limit_point]])
    html('Contour Plot of $f(x,y)$ around $(%s'%str(x0)+',%s'%str(y0)+')$')
##############  End of Sage Code ######################
   </script>
   </script>
  </div>
EOF
 
 
############### End of the Sage specific code #######################
 
############### Below is the normal WebWork pg stuff ################
   
   
  <script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
Context()->texStrings;
  <script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script>
BEGIN_TEXT
Using the contour plot below, determine the range value of the illustrated function at \( ($x0,$y0) \).
  <script type="text/javascript">
$BR $BR
$(function() { // load only when the page is loaded
\( f($x0,$y0) = \)\{ ans_rule(15) \}  
  var makecells = function() {
$PAR
  singlecell.makeSinglecell({
END_TEXT
      inputLocation: "#singlecell-test",
Context()->normalStrings;
      editor: "codemirror",
 
      hide: ["editor","computationID","files","messages","sageMode"],
#  need to add reasonable approximation error of about 0.1 or so.
      evalButtonText: "Start/Restart",
ANS( Compute($f0)->cmp() );
      replaceOutput: true});
 
  }
ENDDOCUMENT();        # This should be the last executable line in the problem.
  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.
  </nowiki>
  </nowiki>



Revision as of 22:36, 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 http://sagemath.org:5467

##  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);


######### Here is where special coding for Sage starts  ########
TEXT(<<'EOF');


<div id="singlecell-test">
<script type="text/code">
 
#########  Actual Sage code pasted starting here ##########
#########  This code should work in regular Sage ##########


var('x,y,z')

@interact(layout=dict(top=[['x0'],['y0']], 
bottom=[['N'],['zoom_in']]))
def _(N=slider(5,100,1,10,label='Number of Contours'),
        zoom_in=checkbox(false,label='Zoom in'),
        x0=input_box(0,width=10,label='x coordinate of center'),
        y0=input_box(0,width=10,label='y coordinate of center')):


    f=(x^3-y^3)/(x^2+y^2+1)
    offset = floor(10*random())/20

    if zoom_in:
        surface = contour_plot(f,(x,x0-offset-1/10,x0+1/10),(y,y0-1/10,y0+offset+1/10), cmap=True,colorbar=True,fill=False,contours=N)    
    else:
        surface = contour_plot(f,(x,-3,3),(y,-3,3),cmap=True,colorbar=True,fill=False,contours=N)    
    limit_point = point((x0,y0),color='red',size=30) 

    html.table([[surface+limit_point]])
    html('Contour Plot of $f(x,y)$ around $(%s'%str(x0)+',%s'%str(y0)+')$')


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


##############  More stuff to make sage work ##########

 </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/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


############### End of the Sage specific code ####################### 

############### 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