The purpose of this tutorial is to introduce students in APMA 0330 (Methods of Applied Mathematics - I) to the computer algebra system Sage. Sage is built out of nearly 100 open-source packages, and features a unified interface. Sage can be used to study elementary and advanced, pure and applied mathematics. This includes a huge range of mathematics including basic algebra, calculus, elementary to very advanced number theory, cryptography, numerical computation, commutative algebra, group theory, combinatorics, graph theory, exact linear algebra and much more. It utilizes various software packages and seamlessly integrates their functionality into a common experience.

The goal of this tutorial is two-fold. On one side, it provides tools to assist in solving problems based on an alternate software, that is free of charge---Sage. Therefore, this tutorial contains many scripts written is Sage. 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. On the other side, this tutorial supplements and accompanies the textbookApplied Differential Equations. The Primary Course by Vladimir Dobrushkin, CRC Press, 2015; http://www.crcpress.com/product/isbn/9781439851043. It contains many examples from real world problems that are used for demonstration and clarification of material presented in the textbook.

In this tutorial, the user learns the essential steps in mathematical modeling, including the identification of state variables, assumptions, the invocation of relevant science or empirical laws, the derivation of the governing differential equations, the avalable methods of solutions, the design of a computational algorithm, the generation of computer graphics, and the effect of the system's parameters. Therefore, the tutorial is an introduction to mathematical modeling with differential equations. 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 second course APMA0340
Return to the main page for the course APMA0330
Return to the main page for the course APMA0340

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

Applications

Part IV: Second and Higher Order Differential Equations

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

Recurrences
Power series solutions to ODEs
Bessel functions
Airy functions
Orthogonal polynomials
a. Chebyshev
b. Legendre
c. Hermite
d. Laguerre
Euler Systems of equations

Laplace transform
Heaviside function and discontinuous functions
Inverse Laplace transformation
Laplace transformation in differential equations
ODE with discontinuous functions

Mechanical and Electrical Vibrations
Other applications

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 first 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 Maple.

There are two ways in which Sage can be distributed. One is as a "regular" OS X,
Windows and/or Linux application named something like Sage-VERSION. This version
needs to be downloaded and installed in your machine. Although it's a Desktop
application, the software opens in your primary browser acting as a native
webapp. The binaries can be downloaded in the following links:

For MAC OS X - http://www.sagemath.org/download-mac.html
For LINUX - http://www.sagemath.org/download-windows.html
For WINDOWS - http://www.sagemath.org/download-linux.html

Although the desktop version has the advantage of working offline, the best
choice is to use the online version, which is more user friendly and can store
work in the cloud. The cloud version can be accessed in the following link:

https://cloud.sagemath.com/

1) Access the link:
https://cloud.sagemath.com/

2) Will be presented to you a login page in which you have to create an account
with login and password.

3) Create your account.

4) Once you created your account, log in with your username and password.

Both desktop and cloud versions will open in your default browser. Although, with the
cloud version you can use Sage, R, Octave, Python, Cython, GAP, Macaulay2,
Singular, and much more. You can edit documents with inverse and forward search,
and Sage mode. Collaboratively edit IPython notebooks, Sage worksheets, and all
other document types Write, compile, and run code in most programming languages
and also use command line terminals.

One of the strengths of Sage is that it can be used over the network without requiring any installation of the application.
The Sage Notebook is a web browser-based graphical user interface for Sage. It allows writing and running code, displaying
embedded two and three dimensional plots, and organizing and sharing data with other users. The Sage Notebook works with
most web browsers without the need for additional add-ons or extensions. However, Java is required to run Jmol, the Java
applet used in Sage to plot 3D objects.

In addition to these main options, Sage can be applied in alternative ways. For instance, a single cell containing Sage code can be embedded
in any webpage and evaluated using a public single cell server. There is also support to embed Sage code and graphics in LaTeX documents
using Sagetex.

1) For you to use Sage, first you have to create a new project.

2) Once you've created the new project and given a name and description, you
will create a new Sage file. Once you created the .sagews file, your're all
set! Just start using Sage.

3) You execute commands by pressing Shif+Enter (simultaneously).

Sage is case sensitive for both variables can be defined and for commands. The variable "a" (lower case) is different from "A" (upper case). You will know if the command that you enter is correct because the color of the command will change to black. The irartional constant Pi (=3.141926...) can be entered either as pi or, its numerical value float(pi), which gives output: 3.141592653589793.
One of the key aspects of using Sage is knowing how to enter a command. Once you have typed the command, then hit shift-enter.

The goal of this document is not to teach you numerical analysis, but
to explain how to express your ideas in Sage. 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.

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.

Familiar from calculus functions such as exponential and power functions as well as many others are built-in into sage. For example, the square root and greatest common division are among them:

sage: sqrt(a) sage: gcd(b)

The function exp(x) is an alternate form of e^x

sage: e^2; exp(2) e^2

However, to use any function, one need first to define an intependent variable or variables. It can ve done as follows:

sage: x1 = var('x1'); x1 x1 sage: a, b, c = var('a, b, c ')

or just

sage: var('a, b, c ')

Logarithm function

The absolute value function is denote by abs. Some examples:

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 knows some famous numbers. For instance, the golden ratio \( \phi = \dfrac{1 + \sqrt{5}}{2} \approx 1.618\) can be invoked as follows

sage: R = RealField(n) #n is the number of decimal places you want sage: R sage: R(golden_ratio)

For example,

sage: R = RealField(16) sage: R Real Field with 16 bits of precision sage: R(golden_ratio) 1.618 sage: R = RealField(50) sage: R(golden_ratio)
1.6180339887499

Multinomial coefficients \( \binom{n}{a,b,c,\ldots} = \dfrac{n!}{a! \,b! \,c!\, \cdots} , \quad n=a+b+c+\cdots ,\) (and in particular, binomial coefficients
\( \binom{n}{a} = \dfrac{n!}{a! \,(n-a)!} \) ) can be evaluated using the factorial operator. For instance,
multinomial coefficient \( \binom{213}{14\,26\,43\, 53\, 77} \) is divisible by 2^(13), but not divisible by 2^(14):

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)]

As you see, this function can be plotted, but not differentiated or integrated because z is a dummy variable. It can be fixed by typing:

sage: var('z') # define z to be a variable z sage: f(z) z^2 sage: plot(f(z),0 2) Graphics object consisting of 1 graphic primitive

Another way is to define a “callable symbolic expression”. These can be plotted, differentiated, and integrated. To define a function, just type in the formula. There are some examples.

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() # numerically evaluate -0.202642367284676
sage: type(f) type 'sage.symbolic.expression.Expression'

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]]

Return to Sage page for the second course (APMA0340)

Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)
Return to the Part 7 (Boundary Balue Problems)