Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168759
Image: ubuntu2004
""" To get started, put the cursor in a box and click "evaluate", or press Shift-Enter ""
""" Introduction to SAGE SAGE is an open source CAS, or "Computer Algebra System". It is written mostly in the Python language, and serves to unite many, many other free mathematics programs while serving as an interface for non-free software like Maple, Mathematica, and MATLAB. SAGE means "Software for Algebra and Geometry Exploration". It was first released in February, 2005 by William Stein at the University of Washington in Seattle, WA. Most of the development of SAGE has been funded by grants, from sources including the National Science Foundation, Google, and even Microsoft Corp. SAGE is released under the GNU Public License, which means it is freely distributable and the source code is open to anyone and everyone in the world. You can modify the code however you wish, and even distribute your version as a new software package... the only caveat is that you must follow through by making your new source code freely available to the world, too. """
""" Numeric Capabilities To begin, we will use Sage as a simple calculator. The lesson is constructed to flow through the Sage commands in the order they are written. The Sage commands are easy to spot since they are shown in a evaluation box all of their own, while comments (such as this one) are enclosed by a pair of triple quotes, or a leading number sign, #. """
3+5
8
1/5 + 3/7
22/35
2^100
1267650600228229401496703205376
# Use the float() function to get a decimal approximation float(1/5 + 3/7)
0.62857142857142856
# Can use the n() function to do the same thing n(2^100)
1.26765060022823e30
# default number of digits after decimal is 15 # can change this with the digits argument n(1/5 + 3/7, digits=5)
0.62857
""" Sage uses the traditional order of operations: 1. exponentiation (written either as ^ or as **) first, 2. then multiplication (*) and division (/), 3. then addition (+) and subtraction (-) This order will be used unless you use parentheses to indicate a particular order. It doesn't hurt to put in extra parentheses, except for the pain of unnecessary typing, and it often makes the expression easier to read. This can be a great advantage if an error has been made in writing the expression. Try each of the following, and make sure you understand Sage's response to each one. If any of it seems surprising to you, ask about it. """
1/2^5
1/32
3/2^5
3/32
(3/2)^5
243/32
1.5^5
7.59375000000000
""" Aack! What happened in that last example? Since the input used a floating point number (1.5, not 3/2), Sage completed the computations in floating point. Now consider these calculations: """
type(_)
<type 'sage.rings.real_mpfr.RealNumber'>
(1.5)^2
2.25000000000000
(1.5)^6
11.3906250000000
(1.5)^8
25.6289062500000
""" This example also shows that there are three ways to force floating point evaluation: through use of the n() command, or use of the float() command, or by specifying the problem using floating point numbers. We can coerce a decimal representation to a fractional one (if possible) with the QQ() function """
QQ(_)
6561/256
""" The other number types are the integers (represented by ZZ), the Real numbers (RR), and the complex numbers (CC) """
1.5^15
437.893890380859
((3.0)/(2.0))^15
437.893890380859
(3./2)^15
437.893890380859
n((3/2)^15);
437.893890380859
""" Of course, when you are using parentheses, you should make sure that they match up correctly (two of the next three input commands SHOULD generate an error message): """
""" Getting Help with Sage One of the real advantages of using Sage is that it has a very useful and extensive Help system. If you want to learn more about a command that is introduced in one of these lessons, you can read the relevant help file. For example, execute the next input line to read more about the 'Returning a numerical approximation of x with at least prec bits of precision'. """
n?
""" Symbolic Capabilities While those numeric capabilities are useful, they are certainly not the only reasons this instruction file has been created. Sage can operate with symbols, as well as numbers, and it is this application that we will find to be most useful. """
# let x be a 'symbolic variable' x = var('x') factor( x^2 - 5*x + 6 )
(x - 3)*(x - 2)
solve( x^2 - 5*x + 6 == 0 )
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_31.py", line 5, in <module> exec compile(ur'solve( x**_sage_const_2 - _sage_const_5 *x + _sage_const_6 == _sage_const_0 )' + '\n', '', 'single') File "", line 1, in <module> File "/home/sage/sage_install/sage/local/lib/python2.6/site-packages/sage/symbolic/relation.py", line 491, in solve return f.solve(*args,**kwds) File "expression.pyx", line 5531, in sage.symbolic.expression.Expression.solve (sage/symbolic/expression.cpp:21935) TypeError: solve() takes at least 1 positional argument (0 given)
""" Some of Sage's symbolic commands have default values that can save you some typing. In the case of solve(), if there's only one variable, Sage will solve for that one. If you just give an expression, it will solve for when that is equal to zero (but then the variable must be specified). The general form is solve( f(x) == g(x) ) or solve( f(x), x) Thus, the last command could have been written as follows: """
solve( x^2 - 5*x + 6, x )
[x == 3, x == 2]
""" We can also solve an equation that has extra symbolic variables included. For instance, Sage can find the traditional quadratic formula. """
# define additional symbolic variables a,b,c a, b, c = var('a b c') solve(a*x^2 + b*x + c == 0, x)
[x == -1/2*(b + sqrt(-4*a*c + b^2))/a, x == -1/2*(b - sqrt(-4*a*c + b^2))/a]
# make the output look pretty show(_)
\left[x = -\frac{1}{2} \, \frac{{(b + \sqrt{-4 \, a c + b^{2}})}}{a}, x = -\frac{1}{2} \, \frac{{(b - \sqrt{-4 \, a c + b^{2}})}}{a}\right]
""" Sage can also solve systems of equations. To set up a system of three equations in three unknowns, you first assign each equation a name. A name must begin with a letter, and it can consist of from 1 to 499 characters. After the first character, you may use additional letters or numbers. ( Sage is sensitive to case, that is, a lower-case x and an upper-case X are treated as two distinct characters. This can be a little disconcerning at first. Always check the case of the letters used if an error message occurs or the answer seems to be incorrect.) """
# two new variables, y and z y, z = var('y z') eq1 = -2*x + y - 3*z == 1 eq2 = 2*x - 2*y + z == -3 eq3 = x + y - z == -3
""" Now that the equations have been defined, we can again use the solve command. Notice that the list of equations is enclosed in a pair of brackets: """
solve([eq1,eq2,eq3], x,y,z)
[[x == -2, y == 0, z == 1]]
""" The answers may not have come out in the order you expected (they may not be listed in the order x =, y = , z = ) but they should all be there. Notice the difference between that problem and the situation where you use fewer equations than you have unknowns: """
show(solve([eq1, eq2], x, y, z))
\left[\left[x = -\frac{5}{2} \, r_{1} + \frac{1}{2}, y = -2 \, r_{1} + 2, z = r_{1}\right]\right]
""" A solution of the form z = r1 means that the value of z is arbitrary (it can be assigned any value). Then the other variables, such as x and y, are specified in terms of the chosen z. """
""" Limits Sage will also do many varieties of limits. The following are a few examples. Verify that they are correct. """
limit((x^2 - 4)/(x-2), x=2)
4
limit(sin(x)/x, x=0)
1
show( limit(1/(x-1), x=1, dir='below') )
-\infty
show( limit(1/(x-1), x=1) )
+\infty
show( limit(1/(x-1), x=1, dir='above') )
+\infty
limit((2*x^3 - 5*x^2 + x -7)/(3*x^3 + 10*x -1), x=oo);
2/3
""" You should try some more examples. If you have questions about what statement to make, check the Sage help menu on limits. """
""" Differentiation The algebraic capabilities of Sage permits the system to perform exact evaluation of most of the techniques in calculus. These include both ordinary and partial derivatives. Because of the ability to perform partial derivatives, it is necessary to tell Sage for which variable the differentiation is being performed. Here are a few examples. """
# if no ambiguity, variable not needed diff(x^2 + 2*x - 3)
2*x + 2
# in this case, need to specify which variable to differentiate diff(x^2*y^3 + 2*x*y^2 - 3, x)
2*x*y^3 + 2*y^2
diff(x^2*y^3 + 2*x*y^2 - 3, y)
3*x^2*y^2 + 4*x*y
""" More difficult examples can be constructed but the format is identical to these. """
show( diff(exp(x^2 + y^3)*sin(ln(x^3*tan(y))), x) )
{{{2 x} {e}^{{y}^{3} + {x}^{2} } } \sin \left( \log \left( {{x}^{3} \tan \left( y \right)} \right) \right)} + \frac{{{3 {e}^{{y}^{3} + {x}^{2} } } \cos \left( \log \left( {{x}^{3} \tan \left( y \right)} \right) \right)}}{x}
diff(exp(x^2 + y^3)*sin(ln(x^3*tan(y))), y)
{{{3 {y}^{2} } {e}^{{y}^{3} + {x}^{2} } } \sin \left( \log \left( {{x}^{3} \tan \left( y \right)} \right) \right)} + \frac{{{{e}^{{y}^{3} + {x}^{2} } {\sec \left( y \right)}^{2} } \cos \left( \log \left( {{x}^{3} \tan \left( y \right)} \right) \right)}}{\tan \left( y \right)}
""" Notice that Sage may not produce the predictible results in every instance. For example, if we differentiated the function f(x) = (x^2+1)/(x^2 -1) using the quotient rule we would expect to have f'(x) = ((2x)(x^2 - 1) - (x^2 + 1)(2x))/(x^2 + 1)^2 = -(4x)/(x^2 + 1)^2 Sage gives a different, but equivalent version. Can you determine why? """
show( diff( (x^2+1)/(x^2-1) ) )
\frac{{2 x}}{{x}^{2} - 1} - \frac{{{2 x} \left( {x}^{2} + 1 \right)}}{{\left( {x}^{2} - 1 \right)}^{2} }
""" Integration Sage will also usually perform integration, provided that the integral can be determined exactly by algebraic means. However, certain integration techniques are not well recognized by Sage so varying forms of the equation are sometimes needed to give the system some assistance. Also, the form of the result may differ from what you would get using standard integration techniques. The general form is integrate( f(x) ) and note that 'integral'='integrate'. Here are some examples. Notice that, again, that Sage will figure out the variable of integration if there is no ambiguity. If there is more than one variable (say, with multiple integration) then the variable of integration must be specified. """
show( integrate(x/(x^2-2*x+2)) )
\frac{\log \left( {x}^{2} - {2 x} + 2 \right)}{2} + \tan^{-1} \left( \frac{{2 x} - 2}{2} \right)
integrate( x^2*arctan(x) )
x^3*arctan(x)/3 - (x^2/2 - log(x^2 + 1)/2)/3
show(_)
\frac{{{x}^{3} \tan^{-1} \left( x \right)}}{3} - \frac{\frac{{x}^{2} }{2} - \frac{\log \left( {x}^{2} + 1 \right)}{2}}{3}
integrate( x/((x^2 + 1)*(x^2 + 4)) )
log(x^2 + 1)/6 - log(x^2 + 4)/6
integrate( (2*x + 5)/(x - 3) )
11*log(x - 3) + 2*x
integrate( (x^3 + x + 1)/(x^4 + 2*x^2 + 4*x) )
log(x^3 + 2*x + 4)/4 + log(x)/4
integrate( (sin(x))^2*(cos(x))^4 )
((2*x - sin(4*x)/2)/4 + sin(2*x)^3/6)/8
integrate( (x^3 + 1)/(x^3 - x^2) )
-log(x) + 2*log(x - 1) + x + 1/x
integrate( x^4/(x^10 + 16) )
arctan(x^5/4)/20
integrate( (x - 2)^(1/2)/(x + 2) )
2*(sqrt(x - 2) - 2*arctan(sqrt(x - 2)/2))
integrate( cos(3*x)*cos(5*x) )
(3*sin(8*x)/16 + 3*sin(2*x)/4)/3
integrate( (x + sin(x))^2 )
(x - sin(2*x)/2)/2 + 2*(sin(x) - x*cos(x)) + x^3/3
integrate( (x^3)*exp(-2*x) )
-(4*x^3 + 6*x^2 + 6*x + 3)*e^(-(2*x))/8
integrate( exp(arctan(x))/(1 + x^2) )
e^arctan(x)
integrate( (1 + exp(x))/(1 - exp(x)) )
x - 2*log(e^x - 1)
integrate( sin(x)*sin(2*x)*sin(3*x) )
cos(6*x)/24 - cos(4*x)/16 - cos(2*x)/8
integrate( sin(2*x)/sqrt(9 - (cos(x))^4) )
-arcsin((2 - 2*sin(x)^2)/6)
integrate( x/((x^4) + 2*x^2 + 10) )
arctan((2*x^2 + 2)/6)/6
integrate( exp(x^(1/3)) )
3*(x^(2/3) - 2*x^(1/3) + 2)*e^x^(1/3)
integrate( (ln(1 + x^2)) )
x*log(x^2 + 1) - 2*(x - arctan(x))
# can't do this one integrate( x^(2*ln(1 + x)) )
integrate(x^(2*log(x + 1)), x)
""" Sage is telling us that it cannot do this one, nor can it do the next one. But in the second case there is a standard function that is used for its representation. """
# no closed form solution integrate( exp(-x^2) );
sqrt(pi)*erf(x)/2
""" The preceding integrals were all indefinite integrals. Sage can also perform definite integration. The general form is integrate( f(x), x, xmin, xmax) Note that with a definite integral the variable must be specified, even if there is no chance of ambiguity. """
integrate( (cos(x))^5, x, 0, pi/4);
43/120*sqrt(2)
integrate((tan(x))^3*(sec(x))^4, x, 0, pi/4)
5/12
""" As mentioned above, Sage will also perform multiple integration. I will give but one example of a triple integral for those who have had multivariable calculus. To integrate the the function f(x, y, z) = e^z sin(x) y^2 on the region D={(x, y, z) | 0 < x < 1, 0 < y < 1-x, 0 < z < 1 - x - y} we could do the following. """
var('x y z') integrate( exp(z)*sin(x)*y^2, z, 0, 1 - x - y)
sin(x)*y^2*(e^(-y - x + 1) - 1)
assume( x < 1 ) integrate(_, y, 0, 1-x)
(2*e^(1 - x) + (x^3 - 6*x^2 + 15*x - 16)/3)*sin(x)
# note that we saved the 'show' until the last evaluation show( integrate(_, x, 0, 1) )
-\sin \left( 1 \right) - \cos \left( 1 \right) + \frac{{3 e} - 4}{3}
# Do all three at once show( integrate(integrate(integrate(exp(z)*sin(x)*y^2, z, 0, 1 - x - y), y, 0, 1-x), x, 0, 1) )
-\sin \left( 1 \right) - \cos \left( 1 \right) + \frac{{3 e} - 4}{3}
""" Graphical Capabilities Sketching the graph of an equation or a function is one of the primary applications of calculus, and a graph can often lead to valuable insights to solving a problem. From a problem-solving point of view it is important to be able to sketch graphs by analyzing the important behavior of the graph, and in complicated situations, computer algebra systems such as Sage can be a powerful tool in this regard. The simplest form of the plot command uses Sage's defaults for everything, as illustrated with the following input. The first plot we will produce is, couldn't you guess, y = x^2, and we will graph it for x between -1 and 1. The general format is: plot( f(x), xmin, xmax, ... ) """
plot( x^2, -1, 1)
""" This one should certainly be familiar, on Sage surely it is unnecessary, but it is good to see that it gives the predicted result. Now we will try one that would be a little more difficult. Keep in mind that Sage, while producing sophisticated plot is simply a point plotter. """
plot(x * sin(x), -10, 10)
""" Notice that the horizontal axis is set to go from -10 to 10, but the vertical axis is scaled to whatever is appropriate for the graph. Since we are going to plot this same expression, x*sin(x), a number of times, let's give it a name and then save ourselves some typing by just using that name from now on. """
f = x*sin(x)
plot(f,-3*pi, 3*pi);
# save the plot as an object for use P = plot(f, -3*pi, 3*pi);
# can change the window with the 4 optional arguments below show(P, xmin=-5, xmax=10, ymin=-5, ymax=9 )
show(P, figsize=[3,3])
# Add text to the plot show(P + text("My first plot", (4,8)) )
""" 3-Dimensional Plotting Sage can do 3-dimensional plots as well. The graphs are made with an open source Java applet package called Jmol. To maximize your chances for Jmol to work correctly, you should have the most recently released official Sun Java version. Be sure to define the symbolic variables in the plot, or you will get an error. """
plot3d(x^2 + y^2, (x,-3,3), (y,-3,3))
plot3d(y^2 - x^2, (x, -2, 2), (y, -1, 2))
""" Graphing Parametric Equations The general form is parametric_plot( (f(t), g(t)), tmin, tmax, ...) """
# need to define t as a symbolic variable t = var('t') parametric_plot((3*sin(t), 3*cos(t)), (t, 0, 2*pi))
parametric_plot((3*sin(t), -3*cos(t)), (t, 0, 2*pi))
parametric_plot((3*cos(t), -3*sin(t)), 0, 2*pi)
""" For the cycloid using a circle of radius 1 we have x(t) = t - sin(t) and y(t) = 1 - cos(t) . To plot the first two "loops" of this curve we could use """
parametric_plot( (t - sin(t), 1 - cos(t)), (t, 0, 4*pi))
""" To plot polar curves, we can use the fact that x = r* cos(t) and y = r*sin(t) (where I am using t instead of theta, for rather obvious reasons) and treat this as a set of parametic equations: x(t) = r(t)*cos(t) and y(t) = r(t)*sin(t) For example, to sketch r = cos (2t): """
parametric_plot( (cos(2*t)*cos(t), cos(2*t)*sin(t)), (t,0, 2*pi))
""" Alternatively, we could sketch this graph using the polarplot command as """
polar_plot(cos(2*t), 0, 2*pi)
""" You can also use Sage to experiment with series. For example, we have seen that the series whose terms are 1/2^k converges: """
maxima("sum(1/2^k, k, 1, inf), simpsum;")
1
maxima("sum(1/k^2, k, 1, inf), simpsum;")
%pi^2/6
float(_)
1.644934066848226
QQ(1.5)
3/2
RR(pi)
3.14159265358979