4.1. Polymer Crystallinity#
Sympy is a Python package used for solving equations using symbolic math.
Sympy can be installed with the Anaconda Prompt or a terminal using the command below:
conda install sympy
Let’s solve the following problem with SymPy.
Given:#
The density of two different polymer samples \(\rho_1\) and \(\rho_2\) are measured.
The percent crystalinity of the two samples (\(\%c_1 \) and \(\%c_2\)) is known.
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:
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.
from sympy import symbols, nonlinsolve
We need to define six different symbols: $\(\rho_c, \rho_a, \rho_1, \rho_2, c_1, c_2\)$
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.
Sub in \(\rho_s = \rho_1\) and \(\rho_s = \rho_2\) to each of the expressions.
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 = 0.904\) and \(c_1 = 0.628\) into our first expression.
expr1 = expr1.subs(p1, 0.904)
expr1 = expr1.subs(c1, 0.628)
expr1
Now we’ll substitue our the values of \(\rho_2 = 0.895\) and \(c_2 = 0.544\) into our second expression.
expr2 = expr2.subs(p2, 0.895)
expr2 = expr2.subs(c2, 0.544)
expr2
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.
nonlinsolve([expr1,expr2],[pa,pc])
We see that the value of \(\rho_a = 0.840789\) and \(\rho_c = 0.94613431\).
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>]
.
sol = nonlinsolve([expr1,expr2],[pa,pc])
type(sol)
sympy.sets.sets.FiniteSet
sol.args
((0.840789786223278, 0.946134313397929), (0, 0))
sol.args[0]
sol.args[0][0]
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 = 0.84 g/cm3
Density of 100% crystaline polymer, pc = 0.95 g/cm3