Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

08 - Equations to Solve Problems

Solving systems of equations is a common task in engineering. In this section, we’ll review how to solve systems of equations using Python with multiple different approaches. The exampels will come from evaluating mass balances on a processing system.

Solving Linear Systems of Equations

Separation process principles are critical to many industries. Lets say you have a flashing unit that is separating a mixture of benzene and toluene. The feed to the unit is 40 mol% benzene and 60 mol% toluene. The vapor leaving the unit is 80 mol% benzene and 20 mol% toluene. The liquid leaving the unit is 10 mol% benzene and 90 mol% toluene. The feed rate is 100 mol/hr. The vapor and liquid leaving the unit are in equilibrium. What are the flow rates of the vapor and liquid leaving the unit?

This problem can be solved by writing a system of equations and solving for the unknowns. In this case, we have two unknowns, the vapor and liquid flow rates. We can write two equations, one for the benzene balance and one for the total flows. One of the equations would be a species balance and the other equation a total mass balance. A picture of the system is shown below.

The inlet enters at the left and the vapor exists the top and the liquid exists the bottom. This is equivalent to a single stage of a distillation column. The equations are:

F=L+VziF=xiL+yiV\begin{align} F &= L + V \\ z_i F &= x_i L + y_i V \end{align}

which for this case ziz_i = 0.4, FF = 100 mol/hr, xix_i = 0.1, yiy_i = 0.8. We can solve this in many different ways. We’ll solve this system of equations multiple ways:

  • first we’ll do it using NumPy (linear algebra),

  • then we’ll do it using SciPy (fsolve),

  • then we’ll do it using SymPy (symbolic algebra),

  • then we’ll do it using Gauss Elimination (a numerical method),

  • and with Gekko (a software package for dynamic optimization).

Solve Linear System with Numpy

The above equations can be solved numerically as shown below using the numpy package. First you’ll adjust the equations algebraically to be in the form Ax = b. Then you’ll use the numpy.linalg.solve function to solve the system of equations.

The above equation rearranged into the form Ax = b is:

L+V=1000.1L+0.8V=40\begin{align} L + V &= 100 \\ 0.1 L + 0.8 V &= 40 \end{align}

The A matrix is:

[110.10.8]\begin{bmatrix} 1 & 1 \\ 0.1 & 0.8 \end{bmatrix}

and the b vector is:

[10040]\begin{bmatrix} 100 \\ 40 \end{bmatrix}
The solution is [57.14285714 42.85714286] for the system of equations with the first value equal to L and the second value equal to V

Solve Linear System with Scipy’s Fsolve

The above equations can be solved numerically as shown below using the scipy package. Fsolve can be used to solve for multiple variables in both a non-linear and linear scenario given there are continuous functions and the number of equations = number of unknowns. Method used to find roots is in the documentation: fsolve is a wrapper around MINPACK’s hybrd and hybrj algorithms.

L  = 57.14 and V = 42.86

Remember thqt the notation for fsolve is:

fsolve(function name(with input only related to the guess value(s)), guess value(s)). Further examples in the next lecture.

Solve Linear System with Sympy

The above equations can be solved symbolically as shown below using the sympy package. Typically used for symbolic solutions to equations. The sympy package can also be used to solve differential equations symbolically. The solver is for linear equations or nonlinear equations, see the documentation for further details: https://docs.sympy.org/latest/modules/solvers/solvers.html.

Loading...

Solve Linear System with Gaussian Elimination

Linear algebra techniques can be used to take the inverse of the A matrix and multiply it by the b vector to solve for the unknowns. One way to get the inverse of the A matrix is to use the Gauss Elimination method. This method is shown below. Similar to the linalg solution above, the system of equations are arranged in the form Ax = b.

Solution:
[57.14285714 42.85714286]

Solve Linear System with Gekko

Gekko is a software package for dynamic optimization. It can also be used to solve systems of equations. Developed here at Brigham Young University by John Hedengren. Further information here: https://gekko.readthedocs.io/en/latest/ and here: https://www.mdpi.com/2227-9717/6/8/106.

The solution is 57.14 for L and 42.86 for V

The above answers are consistent with the answers from the other methods.

Solving Non-Linear Systems of Equations

Similar tools can be used to solve non-linear systems of equations. Scipy’s fsolve is commonly used. There are other tools including Gekko, and Newton’s method.

One application of solving a non-linear equatoin in chemical engineering is calculating the pressure loss from flow down a pipe using the Colbrook equation to determine the Darcy friction factor. The Darcy friction factor is used to calculate the pressure drop in a pipe. The Darcy friction factor is a function of the Reynolds number and the relative roughness of the pipe. The Reynolds number is a function of the fluid velocity, density, viscosity, and pipe diameter.

The Colbrook equation is:

1f=−2.0log⁡10(ϵ/D3.71+2.51Ref)\frac{1}{\sqrt{f}} = -2.0 \log_{10} \left( \frac{\epsilon/D}{3.71} + \frac{2.51}{\text{Re} \sqrt{f}} \right)

where ff is the darcy friction factor, ϵ\epsilon is the pipe roughness, DD is the pipe diameter, and Re is the Reynolds number equal to the product of the fluid density, velocity, and diameter divided by the fluid viscosity. This is valid with a relative roughness of ϵ/D<0.05\epsilon/D < 0.05 and 104<Re<10810^4 < \text{Re} < 10^8.

The pressure loss down the length of pipe is calculated by:

ΔP=fLDρv22\Delta P = f \frac{L}{D} \frac{\rho v^2}{2}

where ΔP\Delta P is the pressure drop, ff is the Darcy friction factor, LL is the length of the pipe, DD is the pipe diameter, ρ\rho is the fluid density, and vv is the fluid velocity.

Example problem: calculate the pressure drop down a 100 m long pipe with a 2 inch diameter with a fluid velocity of 1 m/s. The fluid has a density of 1000 kg/m3^3 and a viscosity of 0.001 kg/m-s. The pipe roughness is 0.0001 m. What is the pressure drop?

Non-linear solution with scipy fsolve

The Reynolds number is 50800
The friction factor is 0.0264
The pressure drop is 25.97 Pa (or 3.76 psi)

Non-linear solution with Gekko

Solve for the friction factor using the GEKKO package.

The friction factor is 0.0264