Math 300 - demo

250 days ago by jimlb

This is a short demonstration of some nice features of SAGE.  You'll get a notebook that looks like this if you use SAGE through the Clemson server as in the link I sent you.  If you download free SAGE on your laptop, you won't get this nice of an interface.  You'll still be able to do all the math, but not some of the other nice features.  If you still want the nicer features, then you have to use CoCalc, which is free if you want crappy service. Otherwise is costs money.  So basically use the Clemson service as long as it is available and complain to Oxy that you want ITS to actually install SAGE like Clemson has. 

The first thing to note above is the Typeset button.  If you click that, all the outputs will be done in LaTeX and look nice.  

If you want help, you type what you're trying to do with a question mark after it.  If that is a command in SAGE, it will bring up the documentation. For example, if we want to differentiate a function:

derivative? 
       

File: /usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/calculus/functional.py

Type: <type ‘function’>

Definition: derivative(f, *args, **kwds)

Docstring:

The derivative of f.

Repeated differentiation is supported by the syntax given in the examples below.

ALIAS: diff

EXAMPLES: We differentiate a callable symbolic function:

sage: f(x,y) = x*y + sin(x^2) + e^(-x)
sage: f
(x, y) |--> x*y + e^(-x) + sin(x^2)
sage: derivative(f, x)
(x, y) |--> 2*x*cos(x^2) + y - e^(-x)
sage: derivative(f, y)
(x, y) |--> x

We differentiate a polynomial:

sage: t = polygen(QQ, 't')
sage: f = (1-t)^5; f
-t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1
sage: derivative(f)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t, t)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, t, 2)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, 2)
-20*t^3 + 60*t^2 - 60*t + 20

We differentiate a symbolic expression:

sage: var('a x')
(a, x)
sage: f = exp(sin(a - x^2))/x
sage: derivative(f, x)
-2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2
sage: derivative(f, a)
cos(-x^2 + a)*e^(sin(-x^2 + a))/x

Syntax for repeated differentiation:

sage: R.<u, v> = PolynomialRing(QQ)
sage: f = u^4*v^5
sage: derivative(f, u)
4*u^3*v^5
sage: f.derivative(u)   # can always use method notation too
4*u^3*v^5
sage: derivative(f, u, u)
12*u^2*v^5
sage: derivative(f, u, u, u)
24*u*v^5
sage: derivative(f, u, 3)
24*u*v^5
sage: derivative(f, u, v)
20*u^3*v^4
sage: derivative(f, u, 2, v)
60*u^2*v^4
sage: derivative(f, u, v, 2)
80*u^3*v^3
sage: derivative(f, [u, v, v])
80*u^3*v^3

File: /usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/calculus/functional.py

Type: <type ‘function’>

Definition: derivative(f, *args, **kwds)

Docstring:

The derivative of f.

Repeated differentiation is supported by the syntax given in the examples below.

ALIAS: diff

EXAMPLES: We differentiate a callable symbolic function:

sage: f(x,y) = x*y + sin(x^2) + e^(-x)
sage: f
(x, y) |--> x*y + e^(-x) + sin(x^2)
sage: derivative(f, x)
(x, y) |--> 2*x*cos(x^2) + y - e^(-x)
sage: derivative(f, y)
(x, y) |--> x

We differentiate a polynomial:

sage: t = polygen(QQ, 't')
sage: f = (1-t)^5; f
-t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1
sage: derivative(f)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t, t)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, t, 2)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, 2)
-20*t^3 + 60*t^2 - 60*t + 20

We differentiate a symbolic expression:

sage: var('a x')
(a, x)
sage: f = exp(sin(a - x^2))/x
sage: derivative(f, x)
-2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2
sage: derivative(f, a)
cos(-x^2 + a)*e^(sin(-x^2 + a))/x

Syntax for repeated differentiation:

sage: R.<u, v> = PolynomialRing(QQ)
sage: f = u^4*v^5
sage: derivative(f, u)
4*u^3*v^5
sage: f.derivative(u)   # can always use method notation too
4*u^3*v^5
sage: derivative(f, u, u)
12*u^2*v^5
sage: derivative(f, u, u, u)
24*u*v^5
sage: derivative(f, u, 3)
24*u*v^5
sage: derivative(f, u, v)
20*u^3*v^4
sage: derivative(f, u, 2, v)
60*u^2*v^4
sage: derivative(f, u, v, 2)
80*u^3*v^3
sage: derivative(f, [u, v, v])
80*u^3*v^3

Calculus 1:

2+2 
       
f(x) = cos(x);f 
       
f(pi/4) 
       
RR(f(pi/4)) 
       
plot(f(x), (x,-3*pi,3*pi), color='purple'); 
       
g(x) = f.derivative();g 
       
P1=plot(f(x), (x,-3*pi,3*pi), color='blue'); P2=plot(g(x), (x,-3*pi,3*pi), color='red'); 
       
(P1+P2).show() 
       

Of course, you'd like your graphs to be better labeled than this if you are going to use them in a project.  

P1=plot(f(x), (x,-3*pi,3*pi), color='blue', axes_labels=['$x$','$y$']); P2=plot(g(x), (x,-3*pi,3*pi), color='red'); (P1+P2).show() 
       

We can also put labels next to the functions, but that requires a little more work.  We have to actually create the text on the plot and tell SAGE where to position it. 

P=plot(f(x), (x,-3*pi,3*pi), color='blue', axes_labels=['$x$','$y$']); 
       
P+=plot(g(x), (x,-3*pi,3*pi), color='red') 
       
P+=text("$y=f(x)$", (8.1,1), fontsize=15, color='black');P+=text("$y=f'(x)$", (3,1),fontsize=15, color='black'); 
       
P.show() 
       
f(x) = 1/x;f 
       
x |--> 1/x
x |--> 1/x

Just plotting this isn't going to be so useful because of the vertical asymptote.  We will want to limit the $y$-values. 

plot(f,(x,-2,2),color='green') 
       
P=plot(f,(x,-2,2),ymax=10,ymin=-10,color='blue');P.show() 
       

We can also make simple things like lines, circles, etc.  We can remove axes if that is helpful, or put in a grid. 

P=circle((0,0),2, thickness=1);P.show() 
       
P=circle((0,0),8^(1/6), thickness=2, axes=false);P.show() 
       
P=circle((1,1), 2, hue=.9, alpha=.1, fill=True);P.show() 
       
P=line([(-1,-1),(1,1)], color='purple');P.show() 
       

You can right click on any of these graphics and save them and then import them into LaTeX as a graphic. 

We can also use the command latex(expression) to have SAGE spit out the latex code.  This can be super useful.  You must turn Typeset off for this though!

f(x) = sqrt(x-5) + 1/(x^5+5*x +3)^(1/3);f 
       
f.derivative().derivative() 
       
derivative(f,x,2); 
       
g=derivative(f,x,5);g 
       
latex(derivative(f,x,5)); 
       
x \ {\mapsto}\ -\frac{280000 \, {\left(x^{4} + 1\right)} x^{6}}{9 \,
{\left(x^{5} + 5 \, x + 3\right)}^{\frac{10}{3}}} + \frac{16000 \,
x^{5}}{3 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{7}{3}}} +
\frac{7000000 \, {\left(x^{4} + 1\right)}^{3} x^{3}}{81 \, {\left(x^{5}
+ 5 \, x + 3\right)}^{\frac{13}{3}}} - \frac{140000 \, {\left(x^{4} +
1\right)}^{2} x^{2}}{9 \, {\left(x^{5} + 5 \, x +
3\right)}^{\frac{10}{3}}} - \frac{11375000 \, {\left(x^{4} +
1\right)}^{5}}{243 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{16}{3}}}
+ \frac{4000 \, {\left(x^{4} + 1\right)} x}{3 \, {\left(x^{5} + 5 \, x +
3\right)}^{\frac{7}{3}}} - \frac{40}{{\left(x^{5} + 5 \, x +
3\right)}^{\frac{4}{3}}} + \frac{105}{32 \, {\left(x -
5\right)}^{\frac{9}{2}}}
x \ {\mapsto}\ -\frac{280000 \, {\left(x^{4} + 1\right)} x^{6}}{9 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{10}{3}}} + \frac{16000 \, x^{5}}{3 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{7}{3}}} + \frac{7000000 \, {\left(x^{4} + 1\right)}^{3} x^{3}}{81 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{13}{3}}} - \frac{140000 \, {\left(x^{4} + 1\right)}^{2} x^{2}}{9 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{10}{3}}} - \frac{11375000 \, {\left(x^{4} + 1\right)}^{5}}{243 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{16}{3}}} + \frac{4000 \, {\left(x^{4} + 1\right)} x}{3 \, {\left(x^{5} + 5 \, x + 3\right)}^{\frac{7}{3}}} - \frac{40}{{\left(x^{5} + 5 \, x + 3\right)}^{\frac{4}{3}}} + \frac{105}{32 \, {\left(x - 5\right)}^{\frac{9}{2}}}
f = x*(x-1)*(x-2)+1;f; 
       
(x - 1)*(x - 2)*x + 1
(x - 1)*(x - 2)*x + 1
P=plot(f,(x,0,2));P.show() 
       
f.find_local_maximum(0,1) 
       
(1.3849001794597504, 0.42264973107517745)
(1.3849001794597504, 0.42264973107517745)
P+=point((0.42264973107517745,1.3849001794597504),color='black', pointsize=20, zorder=50);P.show() 
       

Calculus 2:

f = x*(x-1)*(x-2)+1;f 
       
p = plot(f, 0.5, 1.5, fill=True) + plot(f, 0, 2);p.show() 
       
integrate(f,(x,0.5,1.5)); 
       
integrate(x,(x,0,1)); 
       
integral(x) 
       
integral(f) 
       
integral(arctan(x)) 
       

Some integrals just don't have nice closed form and you'll get some mess that you won't recognize.  

integral(sin(x)/x); 
       
Ei? 
       

File: /usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/functions/exp_integral.py

Type: <class ‘sage.functions.exp_integral.Function_exp_integral’>

Definition: Ei(coerce=True, hold=False, dont_call_method_on_arg=False, *args)

Docstring:

The generalized complex exponential integral Ei(z) defined by

\operatorname{Ei}(x) = \int_{-\infty}^x \frac{e^t}{t}\; dt

for x > 0 and for complex arguments by analytic continuation, see [AS] 5.1.2.

EXAMPLES:

sage: Ei(10)
Ei(10)
sage: Ei(I)
Ei(I)
sage: Ei(3+I)
Ei(I + 3)
sage: Ei(1.3)
2.72139888023202
sage: Ei(10r)
Ei(10)
sage: Ei(1.3r)
2.7213988802320235

The branch cut for this function is along the negative real axis:

sage: Ei(-3 + 0.1*I)
-0.0129379427181693 + 3.13993830250942*I
sage: Ei(-3 - 0.1*I)
-0.0129379427181693 - 3.13993830250942*I

The precision for the result is deduced from the precision of the input. Convert the input to a higher precision explicitly if a result with higher precision is desired:

sage: Ei(RealField(300)(1.1))
2.16737827956340282358378734233807621497112737591639704719499002090327541763352339357795426

ALGORITHM: Uses mpmath.

File: /usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/functions/exp_integral.py

Type: <class ‘sage.functions.exp_integral.Function_exp_integral’>

Definition: Ei(coerce=True, hold=False, dont_call_method_on_arg=False, *args)

Docstring:

The generalized complex exponential integral Ei(z) defined by

\operatorname{Ei}(x) = \int_{-\infty}^x \frac{e^t}{t}\; dt

for x > 0 and for complex arguments by analytic continuation, see [AS] 5.1.2.

EXAMPLES:

sage: Ei(10)
Ei(10)
sage: Ei(I)
Ei(I)
sage: Ei(3+I)
Ei(I + 3)
sage: Ei(1.3)
2.72139888023202
sage: Ei(10r)
Ei(10)
sage: Ei(1.3r)
2.7213988802320235

The branch cut for this function is along the negative real axis:

sage: Ei(-3 + 0.1*I)
-0.0129379427181693 + 3.13993830250942*I
sage: Ei(-3 - 0.1*I)
-0.0129379427181693 - 3.13993830250942*I

The precision for the result is deduced from the precision of the input. Convert the input to a higher precision explicitly if a result with higher precision is desired:

sage: Ei(RealField(300)(1.1))
2.16737827956340282358378734233807621497112737591639704719499002090327541763352339357795426

ALGORITHM: Uses mpmath.

SAGE can do improper integrals as well. 

integrate(1/x,(x,0,1)); 
       
Traceback (click to the left of this block for traceback)
...
ValueError: Integral is divergent.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_141.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW50ZWdyYXRlKDEveCwoeCwwLDEpKTs="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpRSzujK/___code___.py", line 3, in <module>
    exec compile(u'integrate(_sage_const_1 /x,(x,_sage_const_0 ,_sage_const_1 ));
  File "", line 1, in <module>
    
  File "/usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/misc/functional.py", line 664, in integral
    return x.integral(*args, **kwds)
  File "sage/symbolic/expression.pyx", line 11352, in sage.symbolic.expression.Expression.integral (/usr/local/sage-6.10/src/build/cythonized/sage/symbolic/expression.cpp:60289)
  File "/usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/symbolic/integration/integral.py", line 761, in integrate
    return definite_integral(expression, v, a, b, hold=hold)
  File "sage/symbolic/function.pyx", line 988, in sage.symbolic.function.BuiltinFunction.__call__ (/usr/local/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:11344)
  File "sage/symbolic/function.pyx", line 503, in sage.symbolic.function.Function.__call__ (/usr/local/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:7111)
  File "sage/symbolic/function.pyx", line 1059, in sage.symbolic.function.BuiltinFunction._evalf_or_eval_ (/usr/local/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:12073)
  File "/usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/symbolic/integration/integral.py", line 176, in _eval_
    return integrator(*args)
  File "/usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/symbolic/integration/external.py", line 24, in maxima_integrator
    result = maxima.sr_integral(expression, v, a, b)
  File "/usr/local/sage-6.10/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 782, in sr_integral
    raise ValueError("Integral is divergent.")
ValueError: Integral is divergent.
integrate(1/x^3,(x,1,+infinity)); 
       

SAGE can quickly calculate Taylor polynomials for you as well. 

P5=taylor(sin(x),x,0,5);P5 
       
P15=taylor(sin(x),x,0,15);P15 
       
P50=taylor(sin(x),x,0,50); 
       
P=plot(sin(x),(x,-10*pi,10*pi),color='blue')+plot(P5,(x,-10*pi,10*pi),ymax=2, ymin=-2,color='cyan')+plot(P15,(x,-10*pi,10*pi),ymax=2, ymin=-2,color='pink')+plot(P50,(x,-10*pi,10*pi),color='red');P.show() 
       

Multivariable Calculus: Pretty much the same as calculus. You can do a bunch of the stuff here.  I'll just highlight a couple. 

v=vector([1,2,3]);v 
       
w=vector([-1,2,4]);w 
       
v.cross_product(w) 
       
v.dot_product(w) 
       
v.norm() 
       
var('x,y,z'); 
       
f(x,y) = sin(x^2 *y);f 
       
plot3d(f,(x,-5,5),(y,-5,5)); 
       
f(x,y) = 1 + x^2 + y^2;f 
       
plot3d(f,(x,-2,2), (y,-2,2), color='orange'); 
       

We can use SAGE to solve Lagrange multiplier problems.

var('x y t l'); f(x, y) = 8*x^2 - 2*y; g(x, y) = x^2 + y^2 ; 
       
S = solve([diff(f(x, y), x) == l * diff(g(x, y), x), diff(f(x, y), y) == l * diff(g(x, y), y), g(x, y) == 1], [x, y, l], solution_dict = True) S 
       

We can also plot vector fields. 

plot_vector_field((-y,x), (x,-3,3), (y,-3,3), color='red') 
       
plot_vector_field3d((-y,x,z), (x,-3,3), (y,-3,3),(z,-3,3),colors='blue') 
       

Linear Algebra:

A=matrix([[1,2,3],[4,5,6],[7,8,9]]);A 
       
A.eigenvalues() 
       
B=matrix([[3,0,0],[0,6,0],[0,0,9]]);B 
       
A*B 
       
B.eigenvalues() 
       
B.eigenspaces_right() 
       
A.eigenspaces_right() 
       
B.nullity() 
       
C=random_matrix(ZZ,7,7);C 
       
C.rank() 
       
D=random_matrix(ZZ,10,10);D.rank() 
       
T=zero_matrix(ZZ,7,7);T 
       
E=block_matrix([[C,T],[T,T]]);E 
       
E.rank() 
       
E.column_space() 
       
E.rref() 
       
E.transpose() 
       
E.is_invertible() 
       
C.det() 
       
C=C.change_ring(QQ); 
       
C.is_invertible() 
       
C.inverse()