https://dynamics-and-control.readthedocs.io/en/latest/0_Getting_Started/Notebook%20introduction.html
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
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:
Enter \alpha
, then press tab. This will be replaced by α
1.4. SymPy
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
Sympy allows us to do many mathematical operations that would be tedious by hand. For instance, we can expand a polynomial:
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:
1.5. Calculus
Sympy knows how to integrate and differentiate
1.6. Limits
We can evaluate limits using SymPy, even for “interesting” limits where we would need L’Hopital’s rule
1.7. Approximation
SymPy has built-in support for taylor series expansion
To remove the order term use .removeO()
You will also notice that SymPy’s default behaviour is to retain exact representations of certain numbers:
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,
We can also use sympy.Eq
to construct equations
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
This even works with symbolic variables in the equations
No hay comentarios:
Publicar un comentario