Here's a short slide I prepared. It summarizes how python can be used to solve algebraic expressions ,as well as many other mathematical expression using the sympy library.
2. Symbolic Mathematics Python programming
Many applications in the field of mathematics , Science and Engineering demands the use
of symbolic operations, that is, mathematical operations which involves the use of
expressions that contain symbolic variables.
Evaluating or simplifying these mathematical expression often result to another
mathematical expression in terms of the symbolic variable
2𝑡3
+ 5𝑡 − 8 6𝑡2
+ 5For Instance, the derivative of: With respect to t is:
3. Symbolic Mathematics Python programming
Python has the capability of handling many types of symbolic operations with the support
of a scientific library called
SYMPY Library
SymPy is short for Symbolic Python
SymPy is included
with many distributions of Python that are focused on scientists or engineers
including the Anaconda Python distribution and Pythonxy. Installation on
Linux systems is also straightforward. Further information related to downloading and installing SymPy as well as
comprehensive documentation is
available on the SymPy website: www.sympy.org.
4. Symbolic Mathematics Python programming
Sympy Library is imported into Python using the Command: import sympy
This enables access to all the available method in the sympy library using sympy.method() format.
The next step is to declare the symbolic variables or parameters that will be present in the equations
that we plan to manipulate or solve symbolically.
To transforms a string that lists the variables or parameters into instances of the SymPy Symbol class, use the
command: sympy.symbols()
1.
2.
5. Symbolic Mathematics Python programming
To Find roots of a polynomial
𝑎𝑥2
+ 𝑏𝑥 + 𝑐
output
−𝑏 ± 𝑏2 − 4𝑎𝑐
2𝑎
Solve() function
is used for
solving algebraic
expressions
6. Symbolic Mathematics Python programming
Example Problem
The van der Waals equation of state is a common equation for relating the temperature (T), pressure (P), and specific
volume (V̂ ) of a nonideal gas .
𝑷 =
𝑹𝑻
𝑽 − 𝒃
−
𝒂
𝑽 𝟐
Our goal is to calculate the specific volume of ammonia (Tc = 405.5 K and Pc = 111.3 atm) at T = 420 K and P = 43.4
atm.
Before using SymPy to solve for the specific volume, we need to rewrite our equation(s) so that all terms are on one
side of the “=” sign. Thus, we will write the van der Waals equation as
𝟎 = 𝑷 −
𝑹𝑻
𝑽 − 𝒃
+
𝒂
𝑽 𝟐
7. Symbolic Mathematics Python programming
Solution
output
The output from this example is
[0.70088,
0.06531 - 0.02985*I,
0.06531 + 0.02985*I]
The equation is cubic with respect to V so we should not be surprised by getting
three solutions (i.e., three roots). In this case, it is simple to determine the
correct solution as two of the solutions are complex and obviously not physical
8. Symbolic Mathematics Python programming
Sympy has lots of methods used for Solving Mathematical
Expressions.
Enter:
>>> help(sympy)
In your python Interpreter Shell to view more available Functions associated to Sympy class
Email: franklinobasy@gmail.com , LinkedIn : www.linkedin.com/in/franklinobasy