The purpose of this tutorial is to introduce students in APMA 0340 (Methods of Applied Mathematics - I) to a Python library for symbolic mathematics, called SymPy (Symbolic Python). It aims to become a full-featured computer algebra system while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy 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.

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 <> The tutorial accompanies the textbook Applied Differential Equations. The Primary Course by Vladimir Dobrushkin, CRC Press, 2015;

Return to computing page for the second course APMA0340
Return to computing page for the first course APMA0330
Return to SymPy tutorial page for the first course
Return to the main page for the first course APMA0330
Return to the main page for the second course APMA0340


Case Sensitivity
Basic Arithmetic Operations
Complex numbers
Functions (explicit and implicit)

Part I: Plotting

Plotting functions (Cartesian and polar coordinates)
(a) explicitly
(b) implicitly
vertical and horizontal lines
lables and texts
polar plot

Part II: First Order ODEs

Solving ODEs
(a) Using DSolve
(b) Verification
(c) Plotting
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

Part III: Numerical Methods and Applications

Numerical solutions

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


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

Operator methods (not sure yet)

Part V: Fourier Series

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

Part VI: Partial Differential 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



SymPy is a Python library for symbolic mathematics. It aims become a full featured computer algebra system. SymPy is written entirely in Python and does not require any external libraries. Ondřej Čertík started the project in 2006; on Jan 4, 2011, he passed the project leadership to Aaron Meurer.



The SymPy CAS can be installed on virtually any computer with Python 2.6 or above. SymPy does require mpmath Python library to be installed first. The current recommended method of installation is through Anaconda, which includes mpmath, as well as several other useful libraries. Alternatively, executables are available for Windows, and some Linux distributions have SymPy packages available.


Anaconda is a free Python distribution from Continuum Analytics that includes SymPy, Matplotlib, IPython, NumPy, and many more useful packages for scientific computing. This is recommended because many nice features of SymPy are only enabled when certain libraries are installed. For example, without Matplotlib, only simple text-based plotting is enabled. With the IPython notebook or qtconsole, you can get nicer \(\LaTeX\) printing by running init_printing().

If you already have Anaconda and want to update SymPy to the latest version, use:

conda update sympy


If you wish to contribute to SymPy or like to get the latest updates as they come, install SymPy from git. To download the repository, execute the following from the command line:

git clone git://

To update to the latest version, go into your repository and execute:

git pull origin master

If you want to install SymPy, but still want to use the git version, you can run from your repository: develop

This will cause the installed version to always point to the version in the git directory.

Other Methods

An installation executable (.exe) is available for Windows users at the downloads site. In addition, various Linux distributions have SymPy available as a package. You may also install SymPy from source or using pip.

Run SymPy

After installation, it is best to verify that your freshly-installed SymPy works. To do this, start up Python and import the SymPy libraries:

$ python
>>> from sympy import *

From here, execute some simple SymPy statements like the ones below:

>>> x = Symbol('x')
>>> limit(sin(x)/x, x, 0)
>>> integrate(1/x, x)

For a starter guide on using SymPy effectively, refer to the SymPy Tutorial.


Versions of SymPy prior to 1.0 included mpmath, but it now depends on it as an external dependency. If you installed SymPy with Anaconda, it will already include mpmath. Use:

conda install mpmath

to ensure that it is installed.

If you do not wish to use Anaconda, you can use pip install mpmath.

If you use mpmath via sympy.mpmath in your code, you will need to change this to use just mpmath. If you depend on code that does this that you cannot easily change, you can work around it by doing:

import sys
import mpmath
sys.modules['sympy.mpmath'] = mpmath

before the code that imports sympy.mpmath. It is recommended to change code that uses sympy.mpmath to use mpmath directly wherever possible.

Case Sensitivity

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 enter.


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. 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 se "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
The underscore character refers to the previous result.
sage: factor(_) 
3 * 193


Numerical Evaluations

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)

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


sage: q = cos(2*pi/50)
sage: numerical_approx(q))
sage: q.n()
sage: q.n(prec=100) # bits
sage: q.n(digits=50) # decimal digits
sage: n(q)
sage: N(q)
0.992114701314478 sage: RealField(100) # Real Field with 100 bits of precision
sage: RealField(100)(q)
sage: RR # Real Field with 53 bits of precisio
sage: RR(q)
sage: RealField(100) # Real Field with 100 bits of precision
Sage is using simple arithmetic expressions using “+”, “−”, “∗”, and “∕” as for instance,
sage: a=2/pi*sqrt(3)
sage: RealField(100)(a)
The function exp(x) is an alternate form of e^x
sage: e^2; exp(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)


Some Numbers

Sage understands π, e, and i or j (the square of j is −1).
sage: 3*pi; n(_); n(e); I^2

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)
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 divisible by 2^(14):

sage: (factorial(213)/(factorial(14)*factorial(26)*factorial(43)*factorial(53)*factorial(77)))/(2^13)


Complex Numbers

sage: z = complex(-3,4);
sage: z
sage: real(z)
sage: imag(z)
sage: abs(z)
sage: arg(z)
sage: conjugate(z)
sage: z1 = complex(-3,4);
sage: z1
sage: z2=z1*i;
sage: z2
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
sage: b = abs(z)
sage: b
sage: c = abs(z)*e^(I*(arg(z)));
sage: c
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. 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)
sage: f(pi).n()
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
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)
sage: f(2.5)
sage: f(1.5)
#important note!!!!!
sage: f(2)
#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


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)

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)
sage: integral(x/(x^2+1), x, 0, 1)

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)
sage: (_)*1.1839699090568376

Probablistic problems

Example. Consider the following probabalistic problem.
The distribution of IQ scores can be modeled by a normal distribution with mean 95 and standard deviation 10. Estimate the fraction of population with IQ between 100 and 110.

sage:  mu=95 
sage: s=10
sage: var('x')
sage: p(x)=(1/s/sqrt(2*pi))*exp(-(x-mu)^2 /2/s^2)
sage: integral(p(x),x,100,110)
sage: n(_)


Example. Consider the following probabalistic problem.
The probability density function of a certain random variable \( X \) is \( p(x) =2k\,e^{-kx} \) The range of \( X \) is from 0 to 4.

What is the value of \( k \) ?

sage:  var('x')
sage: var('k')
sage: p(x)=2*k*exp(-k*x)
sage: integral(p(x),x,0,4)
-2*k*(e^(-4*k)/k - 1/k)
# if you want to find antiderivative first, type:
sage: p.integral(x) x |--> -2*e^(-k*x)
sage: solve(-2*k*(e^(-4*k)/k - 1/k) ==1,k)
[k == log(I*2^(1/4)), k == log(-2^(1/4)), k == log(-I*2^(1/4)), k == 1/4*log(2)]

Another way:

sage: A=integral(p(x),x,0,4)
sage: A.full_simplify()
2*(e^(4*k) - 1)*e^(-4*k)
sage: k = 1/4*log(2)

Derive the formula for cumulative distribution function \( P(x) \)

sage: var('t')
sage: PP(x)=integral(p(t),t,0,x)
sage: PP(x)
-2*k*(e^(-4*k)/k - 1/k)


Find the mean.


sage: integral(t*p(t),t,0,4)
-2*k*((4*k + 1)*e^(-4*k)/k^2 - 1/k^2)
sage: mu(k)=-2*k*((4*k + 1)*e^(-4*k)/k^2 - 1/k^2)
sage: mu(1/4*log(2))
sage: n(_)

What is the median ?

sage: solve(PP(t)==1/2,t)
[t == log(4/3)/k]
sage: n(log(4/3)*4/log(2))
# another way:
sage: PPP(x,k)=-2*k*(e^(-k*x)/k - 1/k)
sage: PPP(x,1/4*log(2))
-2*(e^(-1/4*x*log(2))/log(2) - 1/log(2))*log(2)
sage: find_root(-2*(e^(-1/4*x*log(2))/log(2) - 1/log(2))*log(2) ==1/2,0,4)


sage: f = PP(x)==.5
sage: f.roots(x)
sage: n(_)
[(log(4/3)/k, 1)]

Estimate of the fraction of the population between 2 and 4.

sage: pp(k)=integral(p(t),t,2,4)
sage: pp(1/4*log(2))
sage: n(_)

Critical points

You can find critical points of a piecewise defined function:

sage: x = PolynomialRing(RationalField(), 'x').gen()
sage: f1 = x^0
sage: f2 = 1-x
sage: f3 = 2*x
sage: f4 = 10*x-x^2
sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: f.critical_points()



1. Part 1: Plotting

Part I. Plotting

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



1. Part 2: First Order ODEs

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



1. Part 3: Numerical Methods and Applications

Numerical solutions

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



1. Part 4: Second and Higher Order ODEs

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)


1. Part 5: Series and Recurrences

First order recurrences
Second order recurrences
Generating functions
Series solutions for the first order equations
Series solutions for the second order equations
Generalized series solutions.
Bessel equation
Airy equation
Chebyshev equations
Legendre equation
Hermite equation
Laguerre equation


1. Part 6: Laplace Transform

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