# 1. Introduction to Sympy and the Jupyter Notebook for engineering calculations¶

Sympy is a computer algebra module for Python. You are looking at the convenient Jupyter Notebook interface. This notebook aims to show some of the useful features of the Sympy system as well as the notebook interface.

This notebook will use Python as the programming language. This means that most of what you learned in MPR can be applied in the notebook. The notebook interface provides “cells” where one can input code. To run the code, click on a cell and press Shift+Enter.

## 1.1. A quick tour¶

Take a second to go through the tour of the notebook interface by clicking on “Help, User Interface Tour”. Also note that there is help available for a number of other things under that menu.

Now that you are familiar with the nomenclature, let’s run some code!

Evaluate the cell below to print out a message by clicking inside the cell and then pressing Shift + Enter

:

for word in ['Hello', 'World']:
print(word)

Hello
World

:

a = 1


## 1.2. Math in text boxes¶

The text editor supports math in :math:LaTeX <>__ notation. You can double-click on a text box to see the codes used to enter it:

$f(a)=\int_\infty^0 \frac{1}{a+2} \mathrm{d}a$

Double-click on the formula above to see the code that produced it.

## 1.3. Special symbols in variable names¶

The notebook supports easy entry of special symbols in variable names. Simply type a backslash with the name of the symbol, then press tab to have it replaced by the symbol. For example:

Enter \alpha, then press tab. This will be replaced by α

:

α = 1


## 1.4. SymPy¶

We need to import the SymPy module to get symbolic math capabilities.

:

import sympy


We need to start the pretty-printer to get nicely typeset math

Note that this changes somewhat based on the version of sympy

:

sympy.init_printing()


In order to do symbolic calculations, we need to create a symbol

:

x = sympy.Symbol('x')

:

x

:

$$x$$

Sympy allows us to do many mathematical operations that would be tedious by hand. For instance, we can expand a polynomial:

:

polynomial = (2*x + 3)**4
polynomial.expand()

:

$$16 x^{4} + 96 x^{3} + 216 x^{2} + 216 x + 81$$

Notice what happened - we defined a new name called “polynomial” and then used the .expand() method to expand the polynomial. We can see all the methods associated with an object by typing its name and a dot then pressing “tab”.

Call up the list of methods for the polynomial variable by entering “.” and pressing tab at the end of the line in the cell below:

:

polynomial

:

$$\left(2 x + 3\right)^{4}$$

To get help about any method, we can type its name and append a ? at the end, then evaluate the cell

Obtain help about the .expand() method by evaluating the cell below:

:

polynomial.expand()

:

$$16 x^{4} + 96 x^{3} + 216 x^{2} + 216 x + 81$$

It is also possible to obtain help for a function by placing the cursor between the parentheses and pressing Shift+Tab

Of course, we can also factor polynomials:

:

(x**2 + 2*x + 1).factor()

:

$$\left(x + 1\right)^{2}$$

## 1.5. Calculus¶

Sympy knows how to integrate and differentiate

:

eq = sympy.tan(sympy.log(x**2 + 1))

:

eq.diff(x)

:

$$\frac{2 x \left(\tan^{2}{\left (\log{\left (x^{2} + 1 \right )} \right )} + 1\right)}{x^{2} + 1}$$
:

polynomial.diff(x) # First derivative

:

$$8 \left(2 x + 3\right)^{3}$$
:

polynomial.diff(x, 2) # Second derivative

:

$$48 \left(2 x + 3\right)^{2}$$
:

polynomial.integrate(x) # indefinite integral - note no constant of integration is added

:

$$\frac{16 x^{5}}{5} + 24 x^{4} + 72 x^{3} + 108 x^{2} + 81 x$$
:

polynomial.integrate((x, 1, 2)) # Note that integrate takes one argument which is a tuple for the definite integral

:

$$\frac{6841}{5}$$

## 1.6. Limits¶

We can evaluate limits using SymPy, even for “interesting” limits where we would need L’Hopital’s rule

:

badeq = (2*sympy.sin(x) - sympy.sin(2*x))/(x - sympy.sin(x))

:

badeq

:

$$\frac{2 \sin{\left (x \right )} - \sin{\left (2 x \right )}}{x - \sin{\left (x \right )}}$$
:

a = sympy.symbols('a')

:

lim = sympy.limit(badeq, x, a)

:

lim

:

$$- \frac{- 2 \sin{\left (a \right )} + \sin{\left (2 a \right )}}{a - \sin{\left (a \right )}}$$
:

lim.subs(sympy.sin(a), 1)

:

$$- \frac{\sin{\left (2 a \right )} - 2}{a - 1}$$

## 1.7. Approximation¶

SymPy has built-in support for taylor series expansion

:

nonlinear_expression = sympy.sin(x)
sympy.series(nonlinear_expression, x, 2, 7) # taylor expansion in terms of the x variable, around x=2, first order.

:

$$\sin{\left (2 \right )} + \left(x - 2\right) \cos{\left (2 \right )} - \frac{\left(x - 2\right)^{2} \sin{\left (2 \right )}}{2} - \frac{\left(x - 2\right)^{3} \cos{\left (2 \right )}}{6} + \frac{\left(x - 2\right)^{4} \sin{\left (2 \right )}}{24} + \frac{\left(x - 2\right)^{5} \cos{\left (2 \right )}}{120} - \frac{\left(x - 2\right)^{6} \sin{\left (2 \right )}}{720} + O\left(\left(x - 2\right)^{7}; x\rightarrow 2\right)$$

To remove the order term use .removeO()

:

temp = sympy.series(nonlinear_expression, x, 2, 2)
temp.removeO()

:

$$\left(x - 2\right) \cos{\left (2 \right )} + \sin{\left (2 \right )}$$

You will also notice that SymPy’s default behaviour is to retain exact representations of certain numbers:

:

number = sympy.sqrt(2)*sympy.pi
number

:

$$\sqrt{2} \pi$$

To convert the exact representations above to an approximate floating point representations, use one of these methods. sympy.N works with complicated expressions containing variables as well. float will return a normal Python float and is useful when interacting with non-sympy programs.

:

sympy.N(number*x)

:

$$4.44288293815837 x$$
:

float(number)

:

$$4.442882938158366$$

## 1.8. Solving equations¶

Sympy can help us solve manipulate equations using the solve function. Like many solving functions, it finds zeros of a function, so we have to rewrite equalities to be equal to zero,

\begin{align} 2x^2 + 2 &= 4 \\ 2x^2 + 2 - 4 &= 0\end{align}
:

sympy.plot(x**2 + 1)

<Figure size 640x480 with 1 Axes>

:

<sympy.plotting.plot.Plot at 0x10b531438>

:

solutions = sympy.solve(2*x**2 + 2 - 4)
solutions

:

$$\left [ -1, \quad 1\right ]$$
:

solutions

:

$$-1$$

We can also use sympy.Eq to construct equations

:

equation = sympy.Eq(2*x**2 + 2, 4)
equation

:

$$2 x^{2} + 2 = 4$$

The roots function will give us the multiplicity of the roots as well.

:

sympy.roots(equation)

:

$$\left \{ -1 : 1, \quad 1 : 1\right \}$$

The results are given as a dictionary. If this is not familiar to you, have a look in the Extra Python notebook.

We can also solve systems of equations by passing a list of equations to solve and asking for a list of variables to solve for

:

x, y = sympy.symbols('x, y')
sympy.solve([x + y - 2,
x - y - 0], [x, y])

:

$$\left \{ x : 1, \quad y : 1\right \}$$

This even works with symbolic variables in the equations

:

a, b, c = sympy.var('a, b, c')
solution = sympy.solve([a*x + b*y - 2,
a*x - b*y - c], [x, y])
solution

:

$$\left \{ x : \frac{c + 2}{2 a}, \quad y : \frac{- c + 2}{2 b}\right \}$$
[ ]: