Sympy is a Python package used to work with symbolic math. Symbolic math refers to calculations that use symbolic variables rather than variables that contain numerical values. Let's take a simple example:

$$ sin(\frac{\pi}{2}) $$

If we define

$$ \pi = 3.1459 $$

Then calculate the arcsine of pi/2, we should get 1. But the value doesn't exactly equal 1:


In [2]:
import math
pi = 3.1459
print(math.sin(pi/2))


0.9999976808467592

Instead, if we define pi symbolically

$$ \pi = \pi $$

Then when we calculate the sine of pi/2, we get an exact value, 1:

pi = symbolic math symbol version of pi
print(sin(pi/2))
1

Now let's take a concrete problem and solve it with symbolic math


In [3]:
import sympy

In [4]:
a, T1, T2, L1, L2 = sympy.symbols('a T1 T2 L1 L2')

In [5]:
CTE_Eq = sympy.Eq(a*L1*(T2-T1), L2)

In [6]:
CTE_Eq.subs(T1, 20)
CTE_Eq.subs(L1, 80.00)
CTE_Eq.subs(L2, 80.10)
CTE_Eq.subs(a, 24e-6)


Out[6]:
Eq(2.4e-5*L1*(-T1 + T2), L2)

In [7]:
CTE_Eq


Out[7]:
Eq(L1*a*(-T1 + T2), L2)

In [8]:
expr = sympy.solveset(CTE_Eq, T2)

In [9]:
expr.subs(T1, 20)
expr.subs(L1, 80.00)
expr.subs(L2, 80.10)
expr.subs(a, 24e-6)


Out[9]:
{T1 + 41666.6666666667*L2/L1}

In [10]:
expr


Out[10]:
{T1 + L2/(L1*a)}

In [11]:
T1 = 20
#T2 = ?
a = 24e-6
L1 = 80
L2 = 80.10

In [12]:
T2 = T1 + (L2)/(a*L1)
T2


Out[12]:
41738.74999999999

In [13]:
a*L1*(72-20)


Out[13]:
0.09984

In [14]:
T1+ (L2-L1)/(a*L1)


Out[14]:
72.08333333333037

In [15]:
def clearance(cl):
    T1 = 20
    #T2 = ?
    a = 24e-6
    L1 = 80
    L2 = 80.10 + cl
    return T1+ (L2-L1)/(a*L1)

In [19]:
clearance(0.10)


Out[19]:
124.16666666666075

In [ ]: