Sympy is a Python package used for solving equations using symbolic math.

Let's solve the following problem with SymPy.

Given:

The density of two different polymer samples $\rho_1$ and $\rho_2$ are measured.

$$ \rho_1 = 1.408 \ g/cm^3 $$$$ \rho_2 = 1.343 \ g/cm^3 $$

The percent crystalinity of the two samples ($\%c_1 $ and $\%c_2$) is known.

$$ \%c_1 = 74.3 \% $$$$ \%c_2 = 31.2 \% $$

The percent crystalinity of a polymer sample is related to the density of 100% amorphus regions ($\rho_a$) and 100% crystaline regions ($\rho_c$) according to:

$$ \%crystallinity = \frac{ \rho_c(\rho_s - \rho_a) }{\rho_s(\rho_c - \rho_a) } \times 100 \% $$

Find:

Find the density of 100% amorphus regions ($\rho_a$) and the density of 100% crystaline regions ($\rho_c$) for this polymer.

Solution:

There are a couple functions we need from Sympy. We'll need the symbols function to create our symbolic math variables and we need the nonlinsolve function to solve a system of non-linear equations.


In [19]:
from sympy import symbols, nonlinsolve

We need to define six different symbols: $$\rho_c, \rho_a, \rho_1, \rho_2, c_1, c_2$$


In [20]:
pc, pa, p1, p2, c1, c2 = symbols('pc pa p1 p2 c1 c2')

Next we'll create two expressions for our two equations. We can subtract the %crystallinity from the left side of the equation to set the equation to zero.

$$ \%crystallinity = \frac{ \rho_c(\rho_s - \rho_a) }{\rho_s(\rho_c - \rho_a) } \times 100 \% $$$$ \frac{ \rho_c(\rho_s - \rho_a) }{\rho_s(\rho_c - \rho_a) } \times 100 \% - \%crystallinity = 0 $$

Sub in $\rho_s = \rho_1$ and $\rho_s = \rho_2$ to each of the expressions.


In [21]:
expr1 = ( (pc*(p1-pa)   ) / (p1*(pc-pa)) - c1)
expr2 = ( (pc*(p2-pa)   ) / (p2*(pc-pa)) - c2)

Now we'll substitue in the values of $\rho_1 = 1.408$ and $c_1 = 0.743$ into our first expression.


In [28]:
expr1 = expr1.subs(p1, 1.408)

In [29]:
expr1 = expr1.subs(c1, 0.743)

In [30]:
expr1


Out[30]:
0.710227272727273*pc*(-pa + 1.408)/(-pa + pc) - 0.743

Now we'll substitue our the values of $\rho_2 = 1.343$ and $c_2 = 0.312$ into our second expression.


In [31]:
expr2 = expr2.subs(p2, 1.343)

In [32]:
expr2 = expr2.subs(c2, 0.312)

In [33]:
expr2


Out[33]:
0.744601638123604*pc*(-pa + 1.343)/(-pa + pc) - 0.312

To solve the two equations for the to unknows $\rho_a$ and $\rho_b$, use SymPy's nonlinsolve() function. Pass in a list of the two expressions and followed by a list of the two variables to solve for.


In [35]:
nonlinsolve([expr1,expr2],[pa,pc])


Out[35]:
{(1.29957020576342, 1.44984214271483)}

We see that the value of $\rho_a = 1.29957$ and $\rho_c = 1.44984$.

The solution is a SymPy FiniteSet object. To pull the values of $\rho_a$ and $\rho_c$ out of the FiniteSet, use the syntax sol.args[0][<var num>].


In [36]:
sol = nonlinsolve([expr1,expr2],[pa,pc])

In [37]:
type(sol)


Out[37]:
sympy.sets.sets.FiniteSet

In [38]:
sol.args


Out[38]:
((1.29957020576342, 1.44984214271483),)

In [39]:
sol.args[0]


Out[39]:
(1.29957020576342, 1.44984214271483)

In [40]:
sol.args[0][0]


Out[40]:
1.29957020576342

In [43]:
pa = sol.args[0][0]
pc = sol.args[0][1]
print(f' Density of 100% amorphous polymer, pa = {round(pa,2)} g/cm3')
print(f' Density of 100% crystaline polymer, pc = {round(pc,2)} g/cm3')


 Density of 100% amorphous polymer, pa = 1.3 g/cm3
 Density of 100% crystaline polymer, pc = 1.45 g/cm3

In [ ]: