<

Preface

This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0340 (Methods of Applied Mathematics - II) . It is primarily for students who have some experience using Sage. If you have never used Sage before and would like to learn more of the basics for this computer algebra system, it is strongly recommended looking at the APMA 0330 tutorial. The tutorial accompanies the textbook Applied Differential Equations. The Primary Course by Vladimir Dobrushkin, CRC Press, 2015.

This tutorial contains many scripts. You, as the user, are free to use all codes for your needs, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. Any comments and/or contributions for this tutorial are welcome; you can send your remarks to <Vladimir_Dobrushkin@brown.edu>

 

Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to Sage tutorial for the first course APMA0330
Return to Sage tutorial for the second course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Introduction to Linear Algebra with Sage

Introduction

Sage was initially created by William Stein in 2004-2005, using open source programs released under the GPL or a
GPL-compatible license. The main goal of the project was to create a viable open source alternative to proprietary mathematical software to
be used for research and teaching. The rst release of the program was in February 2005. By the end of the year, Sage included Pari, GAP
and Singular libraries as standard and worked as a common interface to other mathematical programs like Mathematica and Magma.

 

3D PLotting

 

Sage can also be used to create three-dimensional plots. In both the notebook and the REPL, these plots will be displayed by default using the open source package [Jmol], which supports interactively rotating and zooming the figure with the mouse.

Use plot3d to graph a function of the form \( z = f(x,y) . \)

sage: x, y = var('x,y')
sage: plot3d(x^2 + 2*y^2, (x,-2,2), (y,-2,2))
Alternatively, you can use parametric_plot3d to graph a parametric surface where each of \( x, y, z \) is determined by a function of one or two variables (the parameters, typically \( u \ mbox{and } v \) ). The previous plot can be expressed parametrically as follows:
sage: u, v = var('u,v')
sage: f_x(u, v) = u
sage: f_y(u, v) = v
sage: f_z(u, v) = u^2 + 2 * v^2
sage: parametric_plot3d([f_x, f_y, f_z], (u, -2, 2), (v, -2, 2))
The third way to plot a 3D surface in Sage is implicit_plot3d, which graphs a contour of a function like \( f(x,y,z)=0 \) (this defines a set of points). We graph a sphere using the classical formula:
sage: x, y, z = var('x,y,z')
sage: implicit_plot3d(x^2 + 2*y^2 + z^2 - 4, (x,-2, 2), (y,-2, 2), (z,-2, 2))




 

More Examples

Yellow Whitney's umbrella:

sage: u, v = var('u,v')
sage:  fx = u*v
sage: fy = u
sage: fz = v^2
sage: parametric_plot3d([fx, fy, fz], (u, -1, 1), (v, -1, 1),
....:   frame=False, color="yellow")


Cross cap:

sage: u, v = var('u,v')
sage:  fx = (1+cos(v))*cos(u)
sage:  fy = (1+cos(v))*sin(u)
sage:  fz = -tanh((2/3)*(u-pi))*sin(v)
sage: parametric_plot3d([fx, fy, fz], (u, 0, 2*pi), (v, 0, 2*pi),
....:   frame=False, color="red")

Twisted Torus:

sage: u, v = var('u,v')
sage:  fx = (3+sin(v)+cos(u))*cos(2*v)
sage:  fy = (3+sin(v)+cos(u))*sin(2*v)
sage:  fz = sin(u)+2*cos(v)
sage: parametric_plot3d([fx, fy, fz], (u, 0, 2*pi), (v, 0, 2*pi),
....:   frame=False, color="red")
Lemniscate:
sage: x, y, z = var('x,y,z')
sage:  f(x, y, z) = 4*x^2 * (x^2 + y^2 + z^2 + z) + y^2 * (y^2 + z^2 - 1)
sage:   implicit_plot3d(f, (x, -0.5, 0.5), (y, -1, 1), (z, -1, 1))

 

 

 

 

Basic Arithmetic Operations

The goal of this document is not to teach you numerical analysis, but
to explain how to express your ideas in Sage and Python. By numerical
computation we essentially mean machine precision floating point
computations.

Type in the first cell, hold the shift key down, and press the enter key.

sage: a=1
You may be surprised that nothing appeared to happen. In fact, the value 1 was
assigned to the variable a, but this result was not echoed. Repeat on the cell
below; click on the cell, then use "shift-enter" or click on "evaluate". So if you type
sage: a
Sage will provide you the numerical value: 1 Similarly,
sage: b=2; b

This time the value 2 was assigned to b and then the value of b was echoed. The
semicolon is used to separate commands appearing on the same line. The same thing
could be done by typing "b=2", pressing enter, typing "b".

Feel free to use whichever of these approaches is most comfortable to you.

To echo a floating point number, you have to use the float() function with the
variable you assigned the expression or fraction.

sage: b=2/3
sage: float(b)
0.6666666666666666

Basic functions which normally are built-in are also available in Sage. Such as:

gcd()
sqrt()

sage: q = cos(2*pi/50)
sage: numerical_approx(q))
0.992114701314478
sage: q.n()
0.992114701314478
sage: q.n(prec=100) # bits
0.99211470131447783104979304279
sage: q.n(digits=50) # decimal digits
sage: n(q)
0.992114701314478
sage: N(q)
0.992114701314478 sage: RealField(100) # Real Field with 100 bits of precision
sage: RealField(100)(q)
0.99211470131447783104979304279
sage: RR # Real Field with 53 bits of precisio
sage: RR(q)
0.992114701314478
sage: RealField(100) # Real Field with 100 bits of precision
0.99211470131447783104979304279
Sage is using simple arithmetic expressions using “+”, “−”, “∗”, and “∕” as for instance,
sage: a=2/pi*sqrt(3)
sage: RealField(100)(a)
1.1026577908435840990226529966
You may type more than one command in a window by placing semicolons between them.
sage: 2+3/5; 34*18-33
3/5
579
The underscore character refers to the previous result.
sage: factor(_) 
3 * 193
Sage understands π, e, and i or j (the square of j is −1).
sage: 3*pi; n(_); n(e); I^2
3*pi
9.42477796076938
2.71828182845905
-1
The function exp(x) is an alternate form of e^x
sage: e^2; exp(2) 
e^2
The solve function solves equations. To use it, first specify some variables; then the arguments to solve are an equation (or a system of equations), together with the variables for which to solve:
sage: u = var('u')
sage: diff(sin(u), u)
cos(u)
To compute the fourth derivative of sin(x^2):
sage: diff(sin(x^2), x, 4)
16*x^4*sin(x^2) - 48*x^2*cos(x^2) - 12*sin(x^2)

 

Complex Numbers


sage: z = complex(-3,4);
sage: z
(-3+4j)
sage: real(z)
-3.0
sage: imag(z)
4.0
sage: abs(z)
5.0
sage: arg(z)
(2.214297435588181+0j)
sage: conjugate(z)
(-3-4j)
sage: z1 = complex(-3,4);
sage: z1
(-3+4j)
sage: z2=z1*i;
sage: z2
(-4-3j)
sage: a = arrow((0,0),(real(z1),imag(z1)))
sage: a
sage: plot(a)
sage: b = arrow((0,0),(real(z2),imag(z2)))
sage: b
sage: plot(b)
sage: plot(a+b)
sage: z = 1+i
sage: a = arg(z)
sage: a
1/4*pi
sage: b = abs(z)
sage: b
sqrt(2)
sage: c = abs(z)*e^(I*(arg(z)));
sage: c
sqrt(2)*e^(1/4*I*pi)
sage: simplify(c)
I + 1
sage: sqrt(-8,all=true)
[2*sqrt(-2), -2*sqrt(-2)]
sage: solve( x^3 == -8, x )
[x == I*sqrt(3)*(-1)^(1/3) - (-1)^(1/3), x == -I*sqrt(3)*(-1)^(1/3) - (-1)^(1/3), x == 2*(-1)^(1/3)]

 

I. How to define functions

To define a function, just type in the formula. We need to use a special form for the left hand side, which includes an underscore after

sage: f(x)=(cos(x)-1)/x^2
# f(x) is now defined and values can be passed
sage: f(3)
1/9*cos(3) - 1/9
sage: f(pi)
-2/pi^2
sage: f(pi).n()
-0.202642367284676
Symbolic variables must be defined as such
sage: var('a b')
(a, b)
sage: f(a+b)
(cos(a + b) - 1)/(a + b)^2
Simple example of constructing useful 2D function plot:
sage: f(x).plot()
# plot(function, (starting x pass to function, ending x pass to function), ymin = graphical range min, ymax = graphical range max
sage:
plot(f(x), (-2*pi, 2*pi),ymin = -1, ymax = 1)

To define a discontinous function, intervals must be continuos and individually discrete i.e., intervals must connect to next but must be over a defined interval; variable definition is optional.

sage: f1 = x^2
sage: f2 = 4-x
sage: f = piecewise([[(0,2),f1],[(2,4),f2]], x)
# examples
sage: f(1)
1
sage: f(2.5)
1.50000000000000
sage: f(1.5)
2.25000000000000
#important note!!!!!
sage: f(2)
3
#function at the interval junction returns (f1(2)+f2(2))/2, true for all junctions

Plot of piecewise function

sage: f.plot()
A derivative of a peicewise continuous function returns derivative of pieces
sage: f.derivative()
Piecewise defined function with 2 parts, [[(0, 2), x |--> 2*x], [(2, 4), x |--> -1]]

Usefule attributes

# Critical points only defined over individual functions, does not include interval points

sage: f.critical_points()
[]
sage: f.domain()
(0, 4)
sage: f.intervals()
[(0, 2), (2, 4)]
sage: f.end_points()
[0, 2, 4]
sage: f.which_function(1)
x |--> x^2
sage: f.which_function(3)
x |--> -x + 4

Calculus


Sage knows how to differentiate and integrate many functions. For example, to
differentiate \( \sin (u) \) with respect to u, do the following:

sage: u = var('u')
sage: diff(sin(u), u)
cos(u)

To compute the fourth derivative of \( \sin(x^2) \):

sage: diff(sin(x^2), x, 4)
16*x^4*sin(x^2) - 48*x^2*cos(x^2) - 12*sin(x^2)
We move on to integrals, both indefinite and definite. To compute \( \int x\sin(x^2)\,{\text d}x \) and \( \int_0^1 \frac{x}{x^2+1}\, {\text d}x: \)
sage: integral(x*sin(x^2), x)
-1/2*cos(x^2)
sage: integral(x/(x^2+1), x, 0, 1)
1/2*log(2)

To compute the partial fraction decomposition of \( \frac{1}{x^2-1}: \)

sage: f = 1/((1+x)*(x-1))
sage: f.partial_fraction(x)
1/2/(x - 1) - 1/2/(x + 1)

Numerical integration:

sage:  numerical_integral(sin(1/x)*sin(x),0,pi)
(1.1839699090568376, 7.755356113813495e-07)
sage: a=2/pi
sage: RealField(100)(a)
0.63661977236758134307553505349
sage: (_)*1.1839699090568376
0.7537386539938299

 


 

 

2. Part 1: Matrix Algebra

Part I. Plotting

Plotting functions
Implicit plot
Vertical and horizontal lines
Lables and texts
Arrows
Pollar plot
Direction fields

 

 

2. Part 2: Linear Systems of Ordinary Differential Equations

Solving ODEs
Direction fields
Separable equations
Equations reducible to separable equations.
Exact equations
Integrating Factors
Linear and Bernoulli equations
Riccati equation

Existence and Uniqueness of solutions
Qualitative analysis
Applications

 

 

2. Part 3: Non-linear Systems of Ordinary Differential Equations

Recurrences
Numerical solutions

a) Euler methods
b) Polynomial approximations
c) Runge-Kutta methods
d) Multistep methods
4) Numerov's method

Applications

 

2. Part 4: Numerical Methods

Second order differential equations

Fundamental set of solutions. Wronskian
General solution
Reduction of order
Non-homogeneous equations.
Lagrange's method
Method of undetermined coefficients

Operator methods (not sure yet)
Applications

 

2. Part 5: Fourier Series

Laplace transform
Heaviside function
Laplace Transform of Discontinuous Functions
Inverse Laplace transformation
Laplace transformation in differential equations

Mechanical and Electrical Vibrations
Other applications

2. Part 6: Partial Differential Equations

Laplace transform
Heaviside function
Laplace Transform of Discontinuous Functions
Inverse Laplace transformation
Laplace transformation in differential equations

Mechanical and Electrical Vibrations
Other applications

 


Return to Sage page

Return to the main page (APMA0340)
Return to the Part 1 (Matrix Algebra)
Return to the Part 2 (Linear Systems of Ordinary Differential Equations)
Return to the Part 3 (Non-linear Systems of Ordinary Differential Equations)
Return to the Part 4 (Numerical Methods)
Return to the Part 5 (Fourier Series)
Return to the Part 6 (Partial Differential Equations)