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.

09 - Solving More Equations

Systems of equations, whether linear or non-linear, coupled or non-coupled come up in many different chemical engineering applications. In this notebook, we will practice more solving systems of equations using Python and SymPy.

Definitions

Linear Equations

Linear equations have variables that are not raised to a power and are not functions of variables. For example, y=2x+3y = 2x + 3 is a linear equation.

Non-linear Equations

Non-linear equations have terms with exponents other than 1 or functions of variables like sin⁡(x)\sin(x) or exp⁡(x)\exp(x). y=2x2+3y = 2x^2 + 3 or y=sin(x)y=sin(x) are both non-linear equations. Most of the equations you’ll encounter in chemical engineering are non-linear.

Coupled Equations

Couple equations are equations that have variables in common. For example, y=2x+3y = 2x + 3 and y=3x+2y = 3x + 2 are coupled equations because they both have xx and yy in common.

Non-coupled Equations

Non-coupled equations do not have variables in common. For example, y=2x+3y = 2x + 3 and z=3p+2z = 3p + 2 are non-coupled equations because they do not have any variables in common.

Review: Linear coupled equations

Linear coupled equations are often seen in when solving material and energy balances during steady state conditions.

Remember, using linear algebra, we can write a system of linear equations as a matrix equation Ax=b:

  • First define the matrix AA and the array bb.

  • Use x = np.linalg.solve(A,b)

An example problem with multiple equations and variables is:

−2x0+x1=1x0−2x1+x2=2x1−2x2+x3=3x2−2x3+x4=4x3−2x4=5\begin{align} -2x_0 + x_1 = 1 \\ x_0 - 2x_1 + x_2 = 2 \\ x_1 - 2x_2 + x_3 = 3 \\ x_2 - 2x_3 + x_4 = 4 \\ x_3 - 2x_4 = 5 \end{align}

$$ \left[ \begin{array}{ccccc} -2 & 1 & 0 & 0 & 0 \ 1 & -2 & 1 & 0 & 0 \ 0 & 1 & -2 & 1 & 0 \ 0 & 0 & 1 & -2 & 1 \ 0 & 0 & 0 & 1 & -2 \ \end{array}\right]\cdot

\left[ \begin{array}{c} x_0 \ x_1 \ x_2 \ x_3 \ x_4 \ \end{array}\right] =

\left[ \begin{array}{c} 1 \ 2 \ 3 \ 4 \ 5 \ \end{array}\right] $$

x = [ -5.83333333 -10.66666667 -13.5        -13.33333333  -9.16666667]

Here’s a link to more linear algebra operations.

Nonlinear equations

1-D equations (1 equation in 1 unknown).

Solving a nonlinear equation involves first putting it in the form f(x)=0f(x)=0. We know the function ff and we want to find the value of xx that gives f(x)=0f(x)=0.

To solve this, do the following:

  • Define the function f(x)

  • Set an initial guess for x.

  • We need to import a library: from scipy.optimize import fsolve

  • Call the fsolve function: x = fsolve(f, x0)

    • f is the name of the function you are solving, and x0 is your initial guess.

Exercise

Solve for both roots of f(x)=x2−5.f(x) = x^2-5.

[2.23606798] [-2.23606798]

Multiple equations in multiple unknowns

  • We call this a multi-dimensional problem.

  • Instead of f(x)=0f(x)=0, we solve

  • f⃗(x⃗)=0⃗\vec{f}(\vec{x})=\vec{0}.

    • That is, x⃗\vec{x} is a vector of unknowns, and f⃗\vec{f} is a vector of functions.

    • f⃗(x⃗)\vec{f}(\vec{x}) takes a vector x⃗\vec{x}, and returns a vector f⃗\vec{f}.

Consider solving the two functions for the two unknowns:

h(y,z)=0=y+2zh(y,z) = 0 = y+2z, and

g(y,z)=0=sin⁡(y)/zg(y,z)=0=\sin(y)/z.

We typically want all solvers to look like f(x)=0f(x)=0

  • We would write these as

    f0(x0,x1)=x0+2x1,f_0(x_0,x_1) = x_0+2x_1,

    f1(x0,x1)=sin⁡(x0)/x1,f_1(x_0,x_1) = \sin(x_0)/x_1,

    which becomes:

    [f0(x0,x1)f1(x0,x1)]=[x0+2x1sin⁡(x0)/x1]=[00],\left[\begin{matrix} f_0(x_0,x_1) \\ f_1(x_0,x_1)\end{matrix}\right] = \left[\begin{matrix}x_0+2x_1 \\ \sin(x_0)/x_1\end{matrix}\right] = \left[\begin{matrix}0 \\ 0\end{matrix}\right],

    or

    f⃗(x⃗)=[x0+2x1sin⁡(x0)/x1]=0⃗,\vec{f}(\vec{x}) = \left[\begin{matrix}x_0+2x_1 \\ \sin(x_0)/x_1\end{matrix}\right] = \vec{0},

    that is,

    f⃗(x⃗)=0⃗.\vec{f}(\vec{x})=\vec{0}.
  • We solve the system just like before:

    • Just define the vector function. It takes an array of unknowns x⃗\vec{x}, and returns an array of function values f⃗\vec{f}.

    • We use the same solver, but we give an array of initial guesses x⃗0\vec{x}_0 of [y=1 and z=2].

Note Here is a link to more information about the fsolve function with extra arguments that can give more control over the solution process.

y=3.0π, z=-1.5π

There can be multiple solutions to non-linear equations. The solver will only find one of them. In some cases you may need to find all of the solutions. For example, in the above scenario, if the guess is changed to 100,2 (see below), I git a completely different answer.

y=38.0π, z=-19.0π

One way to do find many of the solutions is to loop through multiple guesses and if the solution is different, you could collect that solution into a list or array. For example, see the below code where the solver is called for an array of guess values and combinations thereof.

The number of unique solutions is at least 224
<Figure size 640x480 with 1 Axes>
0 -0.001592 16 -0.001592 26 0.000000 27 0.000000 48 0.000000 ... 9249 0.001592 9549 0.000000 9751 0.000000 9752 0.000000 9778 -0.001592 Length: 224, dtype: float64

It looks like the solutions lie in a line where z=−1/2∗yz = -1/2*y.

Sympy Solution

[(-pi, pi/2)]

Another Exercise

Solve the following system of three equations in three unknowns:

x2+y2=1,x^2 + y^2 = 1,
xy+yz=−1.1,xy + yz = -1.1,
y2+z2=2.y^2 + z^2 = 2.
x=0.10056, y=-0.99493, z=1.00504
/Users/clintguymon/opt/anaconda3/envs/jupiterbook/lib/python3.9/site-packages/scipy/optimize/_minpack_py.py:177: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)
The number of unique solutions is at least 20
Loading...

args and tuple expansion

  • Functions like fsolve, quad and others have an argument called args that you can set to have those functions pass extra arguments to the function provide.

  • But fsolve and quad (etc.) don’t know how many arguments there are, and there could be an arbitrary number.

  • Tuple expansion to the rescue!

Example 1

  • Below, function f can take any number of arguments.

  • args is a tuple, and the * part of *args can be thought of as collapsing the provided list of arguments into the tuple args.

    • The name args doesn’t matter. It can be anything.

  • You can then loop over the elements of args.

(1.1, 2.2, 3.3, 'hello', {'a': [1, 3, 5, 7], 'b': [4, 3]})
1.1
2.2
3.3
hello
{'a': [1, 3, 5, 7], 'b': [4, 3]}
a is [1, 3, 5, 7]

You can also refer to the keys in the dictionary within the function such as:

Example 2

  • Below, function f takes three arguments.

  • When calling the function, we first make a tuple with three elements,

  • Then we expand the tuple into values using *xyz.

    • The * part of *xyz acts to expand the tuple into elements that are then placed in the individual arguments that the function takes.

1.1
2.2
3.3

Example 3

  • Write my own version of quad that uses the trapazoid method, and passes in extra arguments that my function will need.

  • See the line fx = f(x, *args) where the tuple of args I pass in to myQuad is expanded into the coefficients c0, c1, and c2 of my quadratic function, defined below.

15.40044893378227
6.666802707206749
(15.4, 1.709743457922741e-13)

kwargs and dictionaries

a = 1
b = 2
c = 3
The product of a and b is 2