Sage as a Smart Calculator 0.5
system:sage

<h1><span style="font-family: arial,helvetica,sans-serif;">Sage as a Smart Calcultor: A Primer</span></h1>
<p><span style="font-family: arial,helvetica,sans-serif;">Erik Jacobson, Alyson Deines<br /></span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">Adapted in part from Ted Kosen's "Sage for Newbies"</span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">Sage Days 13, March 1, 2009, Version 0.5</span></p>
<h2><span style="font-family: arial,helvetica,sans-serif;">Introduction<br /></span></h2>
<p><span style="font-family: arial,helvetica,sans-serif;">Sage can be used as a smart calculator, performing any task you can do on a handheld scientific or graphing calculator, and more!&nbsp; This primer is not intended to teach Sage but instead to provide examples demonstrating a range of what is possible with Sage, viewed as a calculator.&nbsp; It is aimed at secondary math teachers and undergraduates who want a functional knowledge of Sage without the overhead of deep understanding.&nbsp; The documentation and tutorials at sagemath.org are a good place to look next; those interested in the programming side of things might check out python.org, the website for the language in which much of Sage is written.</span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">This primer is divided into three sections: numeric operations, symbolic operations, and plotting.&nbsp; The numeric operations section provides examples of how Sage can be used to evaluate expressions. The symbolic operations section shows how to use Sage as a computer algebra system, representing and manipulating functions. The third section demonstrates how to plot (graph) functions in Sage and find roots and intersections.</span></p>
<h2><span style="font-family: arial,helvetica,sans-serif;">Numeric Operations</span></h2>
<p><span style="font-family: arial,helvetica,sans-serif;">To use a Sage Notebook, type an expression you want to evaluate into the cell and press <span style="font-family: courier new,courier;">&lt;SHIFT&gt;&lt;ENTER&gt;</span> or click the <span style="font-family: courier new,courier;"><span style="text-decoration: underline;">evaluate</span></span> button.&nbsp; This worksheet is interactive--try evaluating the cell below; you can modify it if you wish.</span></p>

{{{id=4|
2+2
///

4
}}}

<p><br /> <span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">The standard arithmetic operations in Sage are what you might expect:</span> <span style="font-family: courier new,courier;">+, -, *, /, ^</span>. <span style="font-family: arial,helvetica,sans-serif;">The order in which operations are evaluated follows the usual convention and can be forced using parenthesis, as you might expect.</span></span></p>

{{{id=6|
(5-3)^(1/2)+3*(7-2)
///

sqrt(2) + 15
}}}

<p><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">In Sage, the symbol <span style="font-family: courier new,courier;">=</span> is assignment operator, not comparison (which is denoted <span style="font-family: courier new,courier;">==</span> in Sage).&nbsp; The variable x is always defined so you can use x as a variable without first declaring it as a variable.&nbsp; You can assign a numerical value to a variable in Sage using the assignment operator <span style="font-family: courier new,courier;">=.</span> (When you assign a value to a variable, Sage automatically assigns a type to the variable.) This is a good way to store values you want to use or modify later. To display the value of a variable, use the </span><span style="font-family: courier new,courier;">print</span> <span style="font-family: arial,helvetica,sans-serif;">command and type the variable(s) on the same line seperated by commas.&nbsp; Other strings (marked off by quotes) can be included in the list of things you want to display using</span> <span style="font-family: courier new,courier;">print</span>, <span style="font-family: arial,helvetica,sans-serif;">such as the</span> <span style="font-family: courier new,courier;">"x ="</span> <span style="font-family: arial,helvetica,sans-serif;">and</span> <span style="font-family: courier new,courier;">"and y="</span> <span style="font-family: arial,helvetica,sans-serif;">used below.</span></span></p>

{{{id=68|
x = 1/3
y = 3/4
print 1/3==3/4
print x, y
print "x =",x,"and y =",y
///

False
1/3 3/4
x = 1/3 and y = 3/4
}}}

<p><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">If you only want to display the last expression in a list of commands, the</span> </span><span style="font-family: courier new,courier;">print</span> <span style="font-family: arial,helvetica,sans-serif;">function is not necessary. You can end lines with the semicolon character.</span></p>

{{{id=3|
x=5+67*2/3; x
///

149/3
}}}

<p><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">Variables in Sage have methods you can access using the dot operator. Sometimes you might need the numeric approximate of a variable instead of its exact value. One way is to use the dot operator and the</span> </span><span style="font-family: courier new,courier;">.n()<span style="font-family: arial,helvetica,sans-serif;"> </span></span><span style="font-family: arial,helvetica,sans-serif;">method.&nbsp; The number of digits returned by the method can be specified explicitly.&nbsp; The idea here is to tell the method </span><span style="font-family: courier new,courier;">.n()</span> <span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">how many digits you want to see by assigning a value&nbsp; to the paramenter</span> </span><span style="font-family: courier new,courier;">digits</span>.</p>

{{{id=13|
print x
print x.n()
print x.n(digits=30)
///

149/3
49.6666666666667
49.6666666666666666666666666667
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">Sage has reserved words that are defined as common mathematical constants. For example, pi, e, and i </span><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">behave as you expect.&nbsp; Numerical approximations can be obtained using the</span> </span><span style="font-family: courier new,courier;">.n()</span><span style="font-family: arial,helvetica,sans-serif;">method, as before.</span></p>

{{{id=20|
print pi, "~", pi.n()
print e, "~", e.n()
print I, "=", sqrt(-1)
///

pi ~ 3.14159265358979
e ~ 2.71828182845905
I = 
                                       I
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">Many common functions are also available directly in Sage.&nbsp; They can be evaluated using parenthesis.</span></p>

{{{id=22|
print sin(pi)
print cos(1/2).n()
print exp(2*pi*i)
print log(10).n()
///


                                       0
0.877582561890373

                                     18  pi
                                    e
2.30258509299405
}}}

<p><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">Modular division is implemented in Sage in two ways.&nbsp; First, you can use the binary operator</span> </span><span style="font-family: courier new,courier;">%</span> <span style="font-family: arial,helvetica,sans-serif;">with a number followed by the modulus.&nbsp; In addition, you can use the</span><span style="font-family: courier new,courier;"> .mod() </span><span style="font-family: arial,helvetica,sans-serif;">function.</span></p>

{{{id=10|
print 35 % 11

35.mod(3)
///

2
2
}}}

<p><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">Numbers in Sage have other methods in addition to</span> </span><span style="font-family: courier new,courier;">.n()</span> <span style="font-family: arial,helvetica,sans-serif;">and</span> <span style="font-family: courier new,courier;">.mod()</span> <span style="font-family: arial,helvetica,sans-serif;">which follow the same syntax.&nbsp; For example, given a random number less than 100 we might want to know if it is prime. Naturally, chosing a random number that is prime guarantees we get a prime!</span></p>

{{{id=1|
x = random_int_upto(100)
print x
print x.is_prime()

x= random_prime(100)
print x
print x.is_prime()
///

34
False
13
True
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">To get a list of methods available for a given object, type</span> <span style="font-family: courier new,courier;">.&lt;TAB&gt;</span> <span style="font-family: arial,helvetica,sans-serif;">after the object (this works for any Sage function or expression). To try this below, place the cursor after</span> <span style="font-family: courier new,courier;">x.</span> and hit <span style="font-family: courier new,courier;">&lt;TAB&gt;</span>.&nbsp; <span style="font-family: arial,helvetica,sans-serif;">You can then select a method from the list.&nbsp; Try </span><span style="font-family: courier new,courier;">.is_square()</span><span style="font-family: arial,helvetica,sans-serif;"> or anything else of interest.</span></p>

{{{id=0|
x.
///
Syntax Error:
    x.
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">There are many other functions available in Sage.&nbsp; For example, to find the greatest common factor of two or more numbers, use</span> <span style="font-family: courier new,courier;">gcd()</span> <span style="font-family: arial,helvetica,sans-serif;">with the numbers separated by commas.</span></p>

{{{id=83|
gcd(15, 93)
///

3
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">The <span style="font-family: courier new,courier;">@interact </span>command makes the next function interactive.&nbsp; Here you can type in the values <span style="font-family: courier new,courier;">a</span> and <span style="font-family: courier new,courier;">b</span> and the command <span style="font-family: courier new,courier;">gcd(a, b)</span> finds the greatest common factor of these numbers.</span></p>

{{{id=25|
@interact
def gcd_calculator(a=15987987, b = 93543543):
    print gcd(a, b)
///

<html><!--notruncate--><div padding=6 id='div-interact-25'> <table width=800px height=20px bgcolor='#c5c5c5'
                 cellpadding=15><tr><td bgcolor='#f9f9f9' valign=top align=left><table><tr><td align=right><font color="black">a&nbsp;</font></td><td><input type='text' value="15987987" size=80 onchange='interact(25, "sage.server.notebook.interact.update(25, \"a\", 32, sage.server.notebook.interact.standard_b64decode(\""+encode64(this.value)+"\"), globals());sage.server.notebook.interact.recompute(25)")'></input></td></tr>
<tr><td align=right><font color="black">b&nbsp;</font></td><td><input type='text' value="93543543" size=80 onchange='interact(25, "sage.server.notebook.interact.update(25, \"b\", 33, sage.server.notebook.interact.standard_b64decode(\""+encode64(this.value)+"\"), globals());sage.server.notebook.interact.recompute(25)")'></input></td></tr>
</table><div id='cell-interact-25'><?__SAGE__START>
        <table border=0 bgcolor='#white' width=100% height=100%>
        <tr><td bgcolor=white align=left valign=top><pre><?__SAGE__TEXT></pre></td></tr>
        <tr><td  align=left valign=top><?__SAGE__HTML></td></tr>
        </table><?__SAGE__END></div></td>
                 </tr></table></div>
                 </html>
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">Another useful function in Sage is </span><span style="font-family: courier new,courier;">factor()</span>, <span style="font-family: arial,helvetica,sans-serif;">a function that returns the prime factorization of an integer. The function</span> <span style="font-family: courier new,courier;">next_prime()</span> <span style="font-family: arial,helvetica,sans-serif;">returns the next prime larger than the parameter.</span></p>

{{{id=76|
x = next_prime(100)*next_prime(1000)
print x, "=", factor(x)
///

101909 = 101 * 1009
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">Sage includes a large number of functions that can be used for a wide variety of purposes. Some functions are listed below. A more complete list of functions can be found in the Sage Reference Manual. </span></p>
<p style="padding-left: 30px;">
<table style="height: 478px;" border="0" width="741" align="left">
<tbody>
<tr>
<td>abs() <br /></td>
<td>Return the absolute value of the argument.</td>
</tr>
<tr>
<td>binomial()</td>
<td>Return the binomial coefficient.</td>
</tr>
<tr>
<td>ceil()</td>
<td>The ceiling function.</td>
</tr>
<tr>
<td>derivative()</td>
<td>The derivative of function f.</td>
</tr>
<tr>
<td>exp()</td>
<td>The exponential function, exp(x) = e^x.</td>
</tr>
<tr>
<td>factorial()</td>
<td>Compute the factorial of n, which is the product of 1 * 2 * 3 ... (n-1) n.</td>
</tr>
<tr>
<td>floor()</td>
<td>The floor function.</td>
</tr>
<tr>
<td>imaginary()</td>
<td>Return the imaginary part of a complex number.</td>
</tr>
<tr>
<td>integrate()</td>
<td>The integral of a function.</td>
</tr>
<tr>
<td>is_even()</td>
<td>Return whether or not an integer is even.</td>
</tr>
</tbody>
</table>
</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family: arial,helvetica,sans-serif;">One way to obtain additional information on any function is to type its name followed by a question mark</span> <span style="font-family: courier new,courier;">?</span> <span style="font-family: arial,helvetica,sans-serif;">into a worksheet cell then press the </span><span style="font-family: courier new,courier;">&lt;TAB&gt;</span> <span style="font-family: arial,helvetica,sans-serif;">key.&nbsp; You can also find functions by typing the first few letters of a function and hit <span style="font-family: courier new,courier;">&lt;TAB&gt;</span>.<br /></span></p>

{{{id=30|
is_even?
///
101909 = 101 * 1009
}}}

<h2><span style="font-family: arial,helvetica,sans-serif;">Symbolic Operations</span></h2>
<p><span style="font-family: arial,helvetica,sans-serif;">Sage supports symbolic operations.&nbsp; It allows you to create variables and functions in those variables.&nbsp; All operations</span> <span style="font-family: courier new,courier;">+, -, *, /,<span style="font-family: arial,helvetica,sans-serif;"> &amp; </span>^</span> <span style="font-family: arial,helvetica,sans-serif;">can be used with variables exactly as they were used previously with numbers.&nbsp; This allows us to create functions in one or more variables.&nbsp; Since the variables x and y have already been used, we must reset them by using the</span> <span style="font-family: courier new,courier;">var</span> <span style="font-family: arial,helvetica,sans-serif;">function.</span></p>

{{{id=37|
x = var('x')
y = var('y')

f = (x+4)*(x+2)
g = 3*y*x^3/(x+5)
///
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">It is useful to know that the </span><span style="font-family: courier new,courier;">.show()</span><span style="font-family: arial,helvetica,sans-serif;">command displays functions more attractively. The example below contrasts the result of the </span><span style="font-family: courier new,courier;">.show()</span> <span style="font-family: arial,helvetica,sans-serif;">function with the output given by </span><span style="font-family: courier new,courier;">print</span> <span style="font-family: arial,helvetica,sans-serif;">and the Notebook interface. (The sequence</span> <span style="font-family: courier new,courier;">\n</span> <span style="font-family: arial,helvetica,sans-serif;">in a</span> <span style="font-family: courier new,courier;">print</span><span style="font-family: arial,helvetica,sans-serif;"> string inserts a new line.)</span></p>

{{{id=8|
print "show():"
g.show()
print "print:", g, "\n\nnotebook default: \n"
g
///

show():
<html><div class="math">\frac{{{3 {x}^{3} } y}}{x + 5}</div></html>
print: 
                                       3
                                    3 x  y
                                    ------
                                    x + 5 

notebook default: 

3*x^3*y/(x + 5)
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">You can add, subtract, multiply, and divide functions.&nbsp; To do this you use the same syntax as used for adding, subtracting,multiplying, and dividing numbers.</span></p>

{{{id=46|
(f+g/f).show()
///

<html><div class="math">\frac{{{3 {x}^{3} } y}}{{{\left( x + 2 \right) \left( x + 4 \right)} \left( x + 5 \right)}} + {\left( x + 2 \right) \left( x + 4 \right)}</div></html>
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">You can evaluate defined functions at specific values.&nbsp; To evaluate the function f at x = 2, just compute</span> <span style="font-family: courier new,courier;">f(2).</span></p>

{{{id=75|
f(2)
///

24
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">To do a number of computations easily, you can use a</span> <span style="font-family: courier new,courier;">for</span> <span style="font-family: arial,helvetica,sans-serif;">loop.&nbsp; The <span style="font-family: courier new,courier;">for</span> loop uses an index variable (<span style="font-family: courier new,courier;">i</span> in this case) to cycle through values in a list.&nbsp; In the example below,</span> <span style="font-family: courier new,courier;">range(10)</span> <span style="font-family: arial,helvetica,sans-serif;">gives a list of the first 10 integers (indexing starts at 0, so this gives 0,1,2,...,9).&nbsp; The function</span> <span style="font-family: courier new,courier;">Integer</span> <span style="font-family: arial,helvetica,sans-serif;">makes sure the evaluation of function f is a Sage integer so that</span> <span style="font-family: courier new,courier;">is_squarefree()</span> <span style="font-family: arial,helvetica,sans-serif;">is defined.</span></p>
<p><span style="font-family: arial,helvetica,sans-serif;"></span></p>

{{{id=12|
for i in range(10):
    print i, f(i), Integer(f(i)).is_squarefree()
///

0 
                                       8 False
1 
                                      15 True
2 
                                      24 False
3 
                                      35 True
4 
                                      48 False
5 
                                      63 False
6 
                                      80 False
7 
                                      99 False
8 
                                      120 False
9 
                                      143 True
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">You can also take compositions by evaluating one function at the other.</span></p>

{{{id=39|
(f(g)).show()
///

<html><div class="math">{\left( \frac{{{3 {x}^{3} } y}}{x + 5} + 2 \right) \left( \frac{{{3 {x}^{3} } y}}{x + 5} + 4 \right)}</div></html>
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">With compositions and multiplications you need to be careful.&nbsp; If we had defined f = (x+4)(x+2) (instead of f=(x+4)*(x+2)) then f is the composition of (x+4) with (x+2), i.e.</span></p>

{{{id=47|
(x+4)(x+2)
///

x + 6
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">You can also declare several variables at once.</span></p>

{{{id=50|
a,b,c = var('a,b,c')
///
}}}

{{{id=7|
h = a*b*c/y; h.show()
///

<html><div class="math">\frac{{{a b} c}}{y}</div></html>
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">As the function h is a function in several variables, using the syntax</span> <span style="font-family: courier new,courier;">h(4)</span> <span style="font-family: arial,helvetica,sans-serif;">it is not clear which variable should be assigned 4.&nbsp; As</span> <span style="font-family: courier new,courier;">h(4)</span> <span style="font-family: arial,helvetica,sans-serif;">is not specific, the first variable is assigned to be 4.&nbsp; To evaluate h at b = 4, you must specify which variable is equal to 4.</span></p>

{{{id=48|
(h(4)).show()
///

<html><div class="math">\frac{{{4 b} c}}{y}</div></html>
}}}

{{{id=16|
(h(b=4)).show()
///

<html><div class="math">\frac{{{4 a} c}}{y}</div></html>
}}}

{{{id=15|
(h(a=3,b=4,c=1,y=5)).show()
///

<html><div class="math">\frac{12}{5}</div></html>
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">Defined functions can also be manipulated in other ways, they can be expanded, symplified, and factored.&nbsp; If you function is a fraction of two functions, you can take the denominator or numerator.&nbsp; There are many other functions available for manipulating the functions you create.&nbsp; To see a list functions available to f,&nbsp; type</span> <span style="font-family: courier new,courier;">f.&lt;tab&gt;</span> .</p>

{{{id=53|
(f.expand()).show()
///

<html><div class="math">{x}^{2}  + {6 x} + 8</div></html>
}}}

{{{id=41|
F = f(g)

F = F.full_simplify(); (F).show()
///

<html><div class="math">\frac{{{9 {x}^{6} } {y}^{2} } + {\left( {18 {x}^{4} } + {90 {x}^{3} } \right) y} + {8 {x}^{2} } + {80 x} + 200}{{x}^{2}  + {10 x} + 25}</div></html>
}}}

{{{id=21|
G = F.numerator()

(G.factor()).show()
///

<html><div class="math">{\left( {{3 {x}^{3} } y} + {2 x} + 10 \right) \left( {{3 {x}^{3} } y} + {4 x} + 20 \right)}</div></html>
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">Other functions are also defined.&nbsp; Trigonometric functions, exponential functions, logarithms can all be used symbolically.&nbsp; These functions can be manipulated just as the previous functions could.</span></p>

{{{id=42|
s = sin(x)*cos(x)*e^x*F; s.show()
///

<html><div class="math">\frac{{{{{e}^{x}  \cos \left( x \right)} \sin \left( x \right)} \left( {{9 {x}^{6} } {y}^{2} } + {\left( {18 {x}^{4} } + {90 {x}^{3} } \right) y} + {8 {x}^{2} } + {80 x} + 200 \right)}}{{x}^{2}  + {10 x} + 25}</div></html>
}}}

{{{id=23|
(s.factor()).show()
///

<html><div class="math">\frac{{{{{{e}^{x}  \cos \left( x \right)} \sin \left( x \right)} \left( {{3 {x}^{3} } y} + {2 x} + 10 \right)} \left( {{3 {x}^{3} } y} + {4 x} + 20 \right)}}{{\left( x + 5 \right)}^{2} }</div></html>
}}}

{{{id=26|
t = (sin(x))^2+(cos(x))^2; t.show()
///

<html><div class="math">{\sin \left( x \right)}^{2}  + {\cos \left( x \right)}^{2} </div></html>
}}}

{{{id=55|
t.full_simplify()
///

1
}}}

<h2><span style="font-family: arial,helvetica,sans-serif;"><span style="font-size: xx-large;">Plotting in Sage</span></span></h2>
<p><span style="font-family: arial,helvetica,sans-serif;">Sage also provides plotting capabilities so you can plot the functions you have created.&nbsp; Sage has both 2D and 3D plotting capabilities.</span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">To plot the function <span style="font-family: courier new,courier;">f</span> over the range [-10,10] do the following:</span></p>

{{{id=56|
f = (x-3)*(x+2)
p =plot(f,-10,10)
show(p)
///
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">To find the roots of this polynomial between two bounds, use the</span> <span style="font-family: courier new,courier;">.find_root()</span> <span style="font-family: arial,helvetica,sans-serif;">method giving the bounds as a first and second parameter.&nbsp; In this example, the graph shows one root is between 0 and -5 and the other is between 0 and 5.</span></p>

{{{id=72|
print f.find_root(-5,0)
print f.find_root(0,5)
///

-2.0
3.0
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">You can also plot two lines at once, say f and g.&nbsp; This is done by creating to plots, say p and q, and then adding them together in the show functions, show(p+q).&nbsp; To change the color of one of the plots you can set the color when defining the plot using</span> <span style="font-family: courier new,courier;">rgbcolor = (a,b,c)</span><span style="font-family: arial,helvetica,sans-serif;">, where a,b,c take values in the range 0 to 1.</span></p>
<p>I<span style="font-family: arial,helvetica,sans-serif;">f you want to set the color of g to be red, you need to use</span> <span style="font-family: courier new,courier;">rgbcolor = (1,0,0)</span>.&nbsp; <span style="font-family: arial,helvetica,sans-serif;">RGB stands for red, green, and blue.&nbsp; The vector (1,0,0) represents 100% red, 0% green, and 0% blue.&nbsp; So to change the color of the line you change the percentages in the vector.</span></p>

{{{id=59|
g = (x+4)*(x+20)
q = plot(g,-10,10, rgbcolor=(1,0,0))
show(p+q)
///
}}}

<p><span style="font-family: arial,helvetica,sans-serif;"><br /></span><span style="font-family: arial,helvetica,sans-serif;"></span></p>

<p><span style="font-family: arial,helvetica,sans-serif;"><br /></span><span style="font-family: arial,helvetica,sans-serif;"></span></p>

<p>This code allows you to zoom in on the intersection interactively using a slider.&nbsp; The function wraps the commands from the last two cells in a function called zoom_on_graph that takes the variable N, a slider-specified value that sets the width of the plots, <span style="font-family: courier new,courier;">p</span> and <span style="font-family: courier new,courier;">q</span>.</p>

{{{id=79|
x,y = var('x'),var('y')
f = (x-3)*(x+2)
g = (x+4)*(x+20)

@interact
def zoom_on_graph(N=(10, (1..100))):
    intersection_point = solve((y==f,y==g),x,y,solution_dict=True)
    p =plot(f,intersection_point[0][x]-N,intersection_point[0][x]+N)
    q = plot(g,intersection_point[0][x]-N,intersection_point[0][x]+N, rgbcolor=(1,0,0))
    show(p+q)
///

<html><!--notruncate--><div padding=6 id='div-interact-79'> <table width=800px height=20px bgcolor='#c5c5c5'
                 cellpadding=15><tr><td bgcolor='#f9f9f9' valign=top align=left><table><tr><td align=right><font color="black">N&nbsp;</font></td><td><table><tr><td>
    	<div id='slider-N-79' class='ui-slider ui-slider-3' style='margin:0px;'><span class='ui-slider-handle'></span></div>
    	</td><td><font color='black' id='slider-N-79-lbl'></font></td></tr></table><script>(function(){ var values = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100"]; setTimeout(function() {
    $('#slider-N-79').slider({
    	stepping: 1, min: 0, max: 99, startValue: 9,
    	change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-N-79-lbl').text(values[position]); interact(79, "sage.server.notebook.interact.update(79, \"N\", 34, sage.server.notebook.interact.standard_b64decode(\""+encode64(position)+"\"), globals());sage.server.notebook.interact.recompute(79)"); },
    	slide: function(e,ui) { if(values!=null) $('#slider-N-79-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-N-79-lbl').text(values[$('#slider-N-79').slider('value')]);
    }, 1); })();</script></td></tr>
</table><div id='cell-interact-79'><?__SAGE__START>
        <table border=0 bgcolor='#white' width=100% height=100%>
        <tr><td bgcolor=white align=left valign=top><pre><?__SAGE__TEXT></pre></td></tr>
        <tr><td  align=left valign=top><?__SAGE__HTML></td></tr>
        </table><?__SAGE__END></div></td>
                 </tr></table></div>
                 </html>
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">Suppose you are interested in the intersection of these two functions. For example, to zoom in on the intersection point in the previous example we need to know where </span><span style="font-family: arial,helvetica,sans-serif;">the functions <span style="font-family: courier new,courier;">f</span> and <span style="font-family: courier new,courier;">g </span></span><span style="font-family: arial,helvetica,sans-serif;">intersect<span style="font-family: courier new,courier;"></span>.&nbsp; The</span> <span style="font-family: courier new,courier;">solve()</span> <span style="font-family: arial,helvetica,sans-serif;">function takes a system of equations and a list of variables and returns the solutions of the equation or system of equations.&nbsp; In the next example, there is a system of two quadratic functions and we show how to retrieve the coordinates of the intersections from the <span style="font-family: courier new,courier;">solve()</span> function.<br /></span></p>

{{{id=70|
x,y = var('x'),var('y')
f = -(x-3)*(x+2)
g = (x+4)*(x-3)

solutions = solve((y==f,y==g),x,y,solution_dict=True)

print "The functions ", f, " and ", g, "\nhave two intersections: ", solutions
print "\nThe x-coordinate of the first intersection is:", solutions[0][x]
print "The y-coordinate of the first intersection is:", solutions[0][y]
///

The functions  
                                (3 - x) (x + 2)  and  
                                (x - 3) (x + 4) 
have two intersections:  [{y: 0, x: 3}, {y: -6, x: -3}]

The x-coordinate of the first intersection is: 
                                       3
The y-coordinate of the first intersection is: 
                                       0
}}}

<p><span style="font-family: arial,helvetica,sans-serif;">You can also create 3D plots in sage by calling plot3d.&nbsp; Here we will plot the sin(x*y).&nbsp; If you drag your mouse over the image, right-clicking will give you options for viewing the 3D plot. The option rgbcolor lets you change the color.&nbsp; Here 'red' is a preset rgb value.&nbsp; The first version is a static image that will load quickly on almost any platform (even cell phones!).&nbsp; To draw the static image, set the option viewer to 'tachyon'. The second plot is an interactive model that you can rotate and zoom. (Note: This takes some time to load.)</span></p>

{{{id=36|
var('x,y')
show(plot3d(sin(x*y), (x,-3,3), (y,-3,3), rgbcolor='red', viewer = 'tachyon'))
show(plot3d(sin(x*y), (x,-3,3), (y,-3,3), rgbcolor='blue'))
///
}}}

<p>Sage has much more plotting functionality and can also be used to plot points, specific objects such as sphere and cones, parametric plots, and even animations.&nbsp; For more information on plotting see section 6 of "Sage for Newbies" found at http://sagemath.org/library/books.html&nbsp; or see the Sage documentation http://sagemath.org/doc/ref/ref.html.</p>

{{{id=66|

///
}}}

{{{id=33|

///
}}}

{{{id=62|

///
}}}

{{{id=35|

///
}}}

{{{id=38|

///
}}}

{{{id=29|

///
}}}

{{{id=67|

///
}}}

{{{id=85|

///
}}}