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)
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:
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:
\alpha, then press tab. This will be replaced by α
α = 1
We need to import the SymPy module to get symbolic math capabilities.
We need to start the pretty-printer to get nicely typeset math
Note that this changes somewhat based on the version of sympy
In order to do symbolic calculations, we need to create a symbol
x = sympy.Symbol('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()
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:
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:
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()
Sympy knows how to integrate and differentiate
eq = sympy.tan(sympy.log(x**2 + 1))
polynomial.diff(x) # First derivative
polynomial.diff(x, 2) # Second derivative
polynomial.integrate(x) # indefinite integral - note no constant of integration is added
polynomial.integrate((x, 1, 2)) # Note that integrate takes one argument which is a tuple for the definite integral
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))
a = sympy.symbols('a')
lim = sympy.limit(badeq, x, a)
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.
To remove the order term use
temp = sympy.series(nonlinear_expression, x, 2, 2) temp.removeO()
You will also notice that SymPy’s default behaviour is to retain exact representations of certain numbers:
number = sympy.sqrt(2)*sympy.pi number
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.
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,
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
We can also use
sympy.Eq to construct equations
equation = sympy.Eq(2*x**2 + 2, 4) equation
The roots function will give us the multiplicity of the roots as well.
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])
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