LATM_SAGE_INTRO

3699 days ago by bburkman

LATM Sage Intro

Brad Burkman, Louisiana School for Math, Science, and the Arts

Louisiana Association of Teachers of Mathematics

2011 Annual Conference

If you're using this page online from the sagemath.shodor.org:8000 server, start by clicking the "Edit a Copy" link in the upper left corner.

Helpful web resources:  www.sagemath.org, Basic Math Quick Reference Card, Calculus Quick Reference Card

To get a Sage account, go to either www.sagemath.org or to sagemath.shodor.org:8000.

First, a gallery of cool stuff you can do in Sage.

Gallery I:  Solving $3\times 3$ Systems of Equations

[Algebra II, Precalculus, and Linear Algebra]

We have three equations in three variables:

$$5x - 2y+3z=-9 \qquad 4x+3y+5z=4 \qquad x+2y-z=7$$

or 

$$f(x,y)= \frac{5x-2y+9}{-3} \qquad g(x,y) = \frac{4x+3y-4}{-5} \qquad h(x,y) = x+2y-7$$

A.  Graph the system.  

     Click on the cell below, hit "evaluate" underneath, and click and drag the graph to fly the perspective.  

var ( "x, y" ) plot1 = plot3d( (5*x-2*y+9)/(-3),(x,-5,5),(y,-5,5), color='red') plot2 = plot3d( (4*x+3*y-4)/(-5),(x,-5,5),(y,-5,5), color='blue') plot3 = plot3d( (x+2*y-7),(x,-5,5),(y,-5,5), color='green') show ( plot1+plot2+plot3 ) 
       

B.  Solve the system using the "solve" command.

var ( "x, y, z" ) solve( [5*x-2*y+3*z==-9,4*x+3*y+5*z==4,x+2*y-z==7],x,y,z) 
       

C.  Solve using row reduction on an augmented matrix.

A=matrix(QQ,[[5,-2,3,-9],[4,3,5,4],[1,2,-1,7]]) print A 
       
print A.echelon_form() 
       

D.  Solve using inverse matrices.

B=matrix(QQ,[[5,-2,3],[4,3,5],[1,2,-1]]) C=vector(QQ,[-9,4,7]) print B,C 
       
print B^-1 
       
print B^-1*C 
       

Dependent System

What would the picture look like if we had a dependent system?

Here, $h(x,y) = f(x,y)+g(x,y)$.

$$5x - 2y+3z=-9 \qquad 4x+3y+5z=4 \qquad 9x+y+8z=-5$$

or 

$$f(x,y)= \frac{5x-2y+9}{-3} \qquad g(x,y) = \frac{4x+3y-4}{-5} \qquad h(x,y) = \frac{9x+y+5}{-8}$$

var ( "x, y" ) def f(x,y): return (5*x-2*y+9)/(-3) def g(x,y): return (4*x+3*y-4)/(-5) def h(x,y): return (9*x+y+5)/(-8) plot1 = plot3d( f(x,y),(x,-5,5),(y,-5,5), color='red') plot2 = plot3d( g(x,y),(x,-5,5),(y,-5,5), color='blue') plot3 = plot3d( h(x,y),(x,-5,5),(y,-5,5), color='green') show ( plot1+plot2+plot3 ) 
       

Here, $h(x,y)=f(x,y)+g(x,y)+2$$

var ( "x, y" ) def f(x,y): return (5*x-2*y+9)/(-3) def g(x,y): return (4*x+3*y-4)/(-5) def h(x,y): return (9*x+y-11)/(-8) plot1 = plot3d( f(x,y),(x,-5,5),(y,-5,5), color='red') plot2 = plot3d( g(x,y),(x,-5,5),(y,-5,5), color='blue') plot3 = plot3d( h(x,y),(x,-5,5),(y,-5,5), color='green') show ( plot1+plot2+plot3 ) 
       

Gallery II:  Sliders

A.  Graph Transformations  [Algebra II and Trig]

See how changes in $a$, $b$, $c$, and $d$ affect the graph of $y=a[b(x-c)]^2+d$.

To see transformations of $y=a\sin[b(x-c)]+d$, change the f(x)=x^2 in the second line to f(x)=sin(x).  

var ( "a,b,c,d,x" ) f(x)=x^2 @interact def _(a=slider(-4,4,.1,default=1.00), b=slider(-4,4,.1,default=1), c=slider(-5,5,1,default=0), d=slider(-5,5,1,default=0)): p=plot ( a*f(b*(x-c))+d, -10,10, aspect_ratio=1 ) show ( p, ymin=-10, ymax=10 ) 
       

Click to the left again to hide and once more to show the dynamic interactive window

B.  Slope of a secant line:  Sliding $h\to 0$  [Precalculus and Calculus]

For sliding values of $a$ and $h$, and any function $f$,

     find the slope of the secant line through $(a,f(a))$ and $(a+h,f(a+h))$.

var( "a,h,m" ) f(x)=x^2 @interact def _(a=slider(-5,5,.2,default=1),h=slider(0,2,.01,default=1)): p1=plot(f,xmin=-10,xmax=10,ymin=-10,ymax=10,color='black') dot1 = point((a,f(x=a)),pointsize=80,color='blue') dot2 = point((a+h,f(x=a+h)),pointsize=80,color='green') p2=plot( (f(a+h)-f(a))/h*(x-a)+f(a), xmin=-10,xmax=10,ymin=-10,ymax=10,color='red') html('$f(x)\;=\;%s$'%latex(f)) m=(f(a+h)-f(a))/h html('Slope of secant line from ') html(' ${f(a+h)-f(a)\over h}\;=\;%s$'%m) show ( p1+p2+dot1+dot2 ) 
       

Click to the left again to hide and once more to show the dynamic interactive window

B.  Taylor Polynomials [Calc I]

Slide the "order" bar to see the graph of the Taylor polynomial at $x=0$ of that order for $y=\sin x$.

var('x') x0 = 0 f = sin(x) p = plot(f,-1,7, thickness=2) dot = point((x0,f(x=x0)),pointsize=80,rgbcolor=(1,0,0)) @interact def _(order=slider(1,15,2)): ft = f.taylor(x,x0,order) pt = plot(ft,-1, 7, color='green', thickness=2) html('$f(x)\;=\;%s$'%latex(f)) html('$\hat{f}(x;%s)\;=\;%s+\mathcal{O}(x^{%s})$'%(x0,latex(ft),order+1)) show(dot + p + pt, ymin = -2, ymax = 2) 
       

Click to the left again to hide and once more to show the dynamic interactive window

C.  Cool 3-D Graphs

# 3-D plots. After you evaluate, click on the plot, hold the button, and move the mouse to fly the perspective. var ( "x,y" ) plot3d( sin(x) + cos(y),[0,2*pi],[0,2*pi]) 
       

D.  Bracket-and-Halving:  Introduction to Programming

Approximate the root of $f(x)=x^2-2$ in $x \in [1,2]$.  

var ( "a, b, c, x" ) def f(x): return x^2-2 a=1.0 b=2.0 c=0.0 while b-a>0.0000001: c=(a+b)/2 if ( f(c)<0 ): a=c else: b=c print c, f(c) 
       
# Second, some jargon and basic steps: # The boxes, like the ones around this text, are called "cells." # If you click on this cell, an "evaluate" button will appear below. # When you evaluate a cell, a little green vertical line will appear indicating that it's working: # The actual calculation is done in North Carolina, and it may take a second depending on your internet connection # You could evaluate this cell, but it doesn't do anything. # Lines starting with the hash ("#") are comments that have no effect on Sage operations. # Sage is written in Python: #### Python coders will recognize the hash comments and the automatic indentation after colons. #### These quad-hash comments are for programmers and are not essential to using Sage. # To start a new cell, hover just above a cell until a blue line appears, and click on it. # To start a text cell like the "LATM Sage Intro" cell above, Shift-click on the blue line: #### You can put LaTeX code in the text cells. 
       
# Sage can do all of your normal calculations with the conventional order of operations (unlike the basic calculator in Windows) # Click on this cell (the blue border will get thicker) and click "evaluate" below. cos((7-3*2)*pi) 
       
# Sage will print the results of the last line in the cell. # The "print" command gives you more control over what gets printed. # When you evaluate this cell, the result of 3+5 will not be printed. print 2+3 3+5 6+11 
       
# To create a variable, the syntax is very flexible. See the declaration of "x" below. # To create a constant, just give it a value. See the declaration of "y" below. # If you create a variable in one cell, you can use it in subsequent cells. #### Unlike in conventional Python, the variable type will adapt. #### Note the object notation. # For multiplication, "5x" doesn't work. Must be "5*x." # Parentheses do not indicate multiplication. # Brackets are different from parentheses. var ( "x" ) y=5 print factor (x^2 - 5*x + 6) print sqrt(y).n(digits=50) 
       
# To find out what a function does, use the tab key. # function( + Tab will open a description and/or menu. # In the empty line below, type "factor(" [without the quotation marks] and press the Tab key. 
       
# Some interesting math functions. Evaluate and adapt ones you find interesting. factor ( 13^2-1 ) 
       

Solving   $x^2 - 5x - 6=0$   for $x$.  Note the double equals signs and that you have to tell it what to solve for.

solve ( x^2 - 5*x - 6 == 0, x ) 
       

Given the formula for the surface area of a right cylinder, solve for its height.  

$$S = 2\pi rh + 2\pi r^2$$

$$\left[h = -\frac{2 \, \pi r^{2} - S}{2 \, \pi r}\right]$$

var ( "S, r, h" ) print solve ( S == 2*pi*r*h+2*pi*r^2, h ) 
       
#### Print the solution in LaTeX format latex ( solve ( S == 2*pi*r*h+2*pi*r^2, h ) ) 
       

Solving the system  $\qquad  a+b=1 \qquad 2a+3b=4$.

var ("a,b") solve ( [a+b==1,2*a+3*b==4], a,b ) 
       

Sage doesn't have an absolute value solver to solve  $ |2x+3|=1$, but if you use $|x|=\sqrt(x^2)$ you can get the same results.  

f(x)=sqrt((2*x+3)^2) solve( f(x)==1, x ) 
       
# 2-D plots plot ( f(x), -3, 1 ) 
       
# Plotting multiple functions together. plot ( [f(x),1], -3,1 ) 
       
# Plotting multiple functions in different colors. P1 = plot( f(x),-3,1,rgbcolor=(1,0,0)) P2 = plot( 1 ,-3,1,rgbcolor=(0,0,1)) show(P1+P2) 
       
# Plotting in LSU colors P1 = plot( f(x),-3,1,color='purple') P2 = plot( 1 ,-3,1,color='gold') show(P1+P2) 
       
var ( "t" ) polar_plot ( t, 0, 2*pi ) 
       
# Calculus I. var ( "x" ) f(x)=3*x^2+2*x+5 g(x)=diff(f(x),x) h(x)=integral( g(x),x) print f(x), ", ", g(x), ", ", h(x) print integral( f(x), x, 0, 1 ) 
       
# Linear Algebra. Note that we're solving the system above. A = matrix ( [[1,1],[2,3]] ) B = vector ( [1,4] ) print A*B print A^-1*B 
       

Complex numbers.  Here we use   $e^{i\theta} = \cos\theta + i\sin\theta$.  Letting $\theta = \frac{\pi}{6}$, we get $e^{i \pi/6} = \cos \frac{\pi}{6} + i \sin \frac{\pi}{6} = \frac{\sqrt3}{2} + \frac{1}{2}i$

# Complex numbers simplify(e^(i*pi/6)) 
       

Discrete math:  Implement the RSA encryption algorithm using distinct primes $p$ and $q$ of about four digits each.  For a secure RSA algorithm you'd use primes of about a hundred digits, but four digits will give the idea. 

var ( "n, p, q" ) # Seed p and q, and have Sage find the next largest primes. p=1234 q=2345 while is_prime(p)==0: p=p+1 while is_prime(q)==0: q=q+1 n=p*q; print "Primes: p =",p,", q =", q,", n=pq =", n 
       
# Find an integer e such that gcd( e, (p-1)(q-1) )=1. var ( "e" ) # Choose a seed value for e. e=100000 while gcd(e,(p-1)*(q-1))>1: e=e+1 print "e =", e print "Make the values n =", n," and e =", e," public." print "Keep p =", p," and q =", q," private." 
       
# Find an integer d such that de = 1 mod (p-1)(q-1) var ( "d" ) # Choose a seed value for d. d=1959017 while Mod(d*e,(p-1)*(q-1))>1: d=d+1 print d 
       
# Message to be sent to you is m. var ( "c,m" ) m=9209 c=Mod(m^e,n) print "Message sender sends you", c 
       
print "You decode the message as", Mod(c^d,n)