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.

HW08: Solve Equations

Systems of equations are solved by finding variable values that satisfy all of the equations. With linear systems of equations, there is typically a unique solution that is easy to solve, even for large-scale systems. Nonlinear equations can be more challenging to solve and may have multiple solutions. Variable bounds and a good initial guess are sometimes required for nonlinear systems of equations. This section is to solve systems of:

  • Linear systems of equations

  • Nonlinear systems of equations

Problem #1

Solve the following systems of linear algebraic equations (A x=b)\left(A\,x = b\right) using matrix calculations such as:

[121.5−1][x0x1]=[00.5]\left[ {\begin{array}{cc} 1 & 2 \\ 1.5 & -1 \\ \end{array} } \right] \left[ {\begin{array}{c} x_0 \\ x_1 \end{array} } \right] = \left[ {\begin{array}{c} 0 \\ 0.5 \end{array} } \right]

An array is defined with square brackets as a list or numpy array.

Vector: A vector (1D array) is:

b=[00.5]b = \left[ {\begin{array}{c} 0 \\ 0.5 \end{array} } \right]

and is defined as a list:

b = [0,0.5]

or as a numpy array:

b = np.array([0,0.5])

Matrix: A matrix (2D array) is:

A=[121.5−1]A = \left[ {\begin{array}{cc} 1 & 2 \\ 1.5 & -1 \\ \end{array} } \right]

and is defined as a list:

A = [[1.0, 2.0],[1.5,-1.0]]

or as a numpy array:

A = np.array([[1.0, 2.0],[1.5,-1.0]])

Solution Method #1: The solution to this set of equations can be found by inverting the matrix AA and multiplying by bb as x=A−1 bx=A^{-1}\,b.

[x0x1]=[121.5−1]−1[00.5]\left[ {\begin{array}{c} x_0 \\ x_1 \end{array} } \right] = \left[ {\begin{array}{cc} 1 & 2 \\ 1.5 & -1 \\ \end{array} } \right]^{-1} \left[ {\begin{array}{c} 0 \\ 0.5 \end{array} } \right]

The inverse of a matrix can be found with the function np.linalg.inv. Two matrices are multiplied together with the dot product with function np.dot.

[ 0.25  -0.125]

Solution Method #2: The function np.linalg.solve can also be used to find x for linear systems of equations.

[ 0.25  -0.125]

Parse Solution: Individual values can be accessed by referencing the specific location in the array. An array index starts with 0.

0.25

Last Value in Array: The last value of this array can be accessed with x[1] or with x[-1].

-0.125
-0.125

Array References: The second to last value of any array can be accessed with x[-2].

0.25

Action: Find the values of x that satisfy the following system of linear equations (A x=bA \, x = b):

[1170120520132710404101251314][x0x1x2x3x4]=[4530152092]\left[ {\begin{array}{ccccc} 11 & 7 & 0 & 1 & 2 \\ 0 & 5 & 2 & 0 & 1 \\ 3 & 2 & 7 & 1 & 0 \\ 4 & 0 & 4 & 10 & 1 \\ 2 & 5& 1 & 3 & 14 \end{array} } \right] \left[ {\begin{array}{c} x_0 \\ x_1 \\ x_2 \\ x_3 \\ x_4 \end{array} } \right] = \left[ {\begin{array}{c} 45 \\ 30 \\ 15 \\ 20 \\ 92 \end{array} } \right]

Action: Solve the equations with a different method and compare the solution to the one above.

Action: Print the last value of x.

Problem #2

A nonlinear equation solver such as scipy.optimize.fsolve can also solve linear equations. Returning to the introductory example:

[121.5−1][x0x1]=[00.5]\left[ {\begin{array}{cc} 1 & 2 \\ 1.5 & -1 \\ \end{array} } \right] \left[ {\begin{array}{c} x_0 \\ x_1 \end{array} } \right] = \left[ {\begin{array}{c} 0 \\ 0.5 \end{array} } \right]

This is solved with fsolve by placing all of the equations to one side as:

[121.5−1][x0x1]−[00.5]=[f0f1]\left[ {\begin{array}{cc} 1 & 2 \\ 1.5 & -1 \\ \end{array} } \right] \left[ {\begin{array}{c} x_0 \\ x_1 \end{array} } \right] - \left[ {\begin{array}{c} 0 \\ 0.5 \end{array} } \right] = \left[ {\begin{array}{c} f_0 \\ f_1 \end{array} } \right]

The equation is solved when f0f_0 and f1f_1 are equal to zero. The fsolve function requires a function that returns the residuals (f1f_1 and f2f_2).

The function solver fsolve also needs an initial guess. A few guess values are evaluated below.

[0.0, -0.5]
[1.5, -0.25]
[1.0, 1.0]

Guess values for x0x_0 and x1x_1 are selected as [1,1] and fsolve finds the solution.

[ 0.25  -0.125]
[ 0.25  -0.125]

Action: Determine one solution for xx and yy to the following equations (there are 4 possible solutions).

2x2+y2=12 x^2 + y^2 = 1

(0.5x−0.5)2+2(y−0.25)2=1(0.5x-0.5)^2+2(y-0.25)^2=1

The initial guess can be adjusted to find a different solution.

Problem #3

The Redlich/Kwong Equation of State is

P=Rg TV−b−aT1/2 V (V+b)P = \frac{R_g \, T}{V-b} - \frac{a}{T^{1/2} \, V \, \left(V+b\right)}

where TT is the temperature, VV is the molar volume, RgR_g is the universal gas constant, and aa and bb are compound-specific constants.

A plot of the pressure as a function of the molar volume at a temperature of -87 C is shown below. Notice there are three roots at that temperature (three molar volumes where the curve yields 1 bar). The left most is the liquid phase and the right most is the vapor phase.

Find the molar volume of ethane for each phase (liquid and gas) that is present at T=−87 °CT = -87 \, °C and P=1 barP = 1 \, bar. For ethane, a=2.877×108 cm6barK0.5mol2a = 2.877×10^8 \, \frac{cm^6 bar K^{0.5}}{mol^2} and b=60.211 cm3molb = 60.211 \, \frac{cm^3}{mol}.

  • Pay attention to units.

  • Use the ideal gas law to obtain the guess of the vapor volume.

  • Use a guess value of 1.01*b for the liquid volume.

  • You should obtain something around 16000 cm3/molcm^3/mol for the vapor volume and 55 cm3/molcm^3/mol for the liquid volume. The middle root (not required but for your information) is around 60 cm3/molcm^3/mol (as shown in the above plot).

  • Compare your answer for the vapor volume (actual) to the ideal gas law (estimate) by calculating a percent error: (actual-estiate)/actual*100).

  • Also compare your answer for the liquid volume you solved for to the liquid volume of ethane at -87 °C (look up on the web or dippr). Also calculate a percent error.

Problem #4

Imagine mixing liquid benzene (species 1) and toluene (species 2) together in an initially empty container. At equilibrium, some of the liquid from both species evaporates into the vapor phase and some is left liquid phase for certain temperatures and pressures. In thermodynamics, you learn that Raoult’s law may be used to describe the distribution of species in each phase. For the specific example mentioned above, Raoult’s law gives the following two expressions describing the equilibrium state

y1P=x1P1sat(T)y2P=x2P2sat(T)\begin{align} y_1 P = x_1 P_1^{sat}(T) \\ y_2 P = x_2 P_2^{sat}(T) \end{align}

where yiy_i is the mole fraction of species ii (either 1 or 2) in the vapor phase, xix_i is the mole fraction of species ii in the liquid phase, Pisat(T)P_i^{sat}(T) is the vapor pressure of species ii at the system temperature TT, and PP is the system pressure.

The vapor pressures of benzene and toluene can be found using the Antione Equation given below:

ln⁡(Pisat(T))=Ai−BiT+Ci\ln{\left(P_i^{sat}(T) \right)} = A_i - \frac{B_i}{T+ C_i}

Note that Pisat(T)P_i^{sat}(T) is not PisatP_i^{sat} times TT, but is simply showing that PisatP_i^{sat} is a function of TT in kPa. The constants, AA, BB, and CC, are compound specific and are found in the table below. TT is in °C°C in the above equation.

Antoine Constants for Benzene and Toluene:

CompoundABC
Benzene13.78192726.81217.572
Toluene13.93203056.96217.625

Setup Equations

Since the liquid (x’s) and vapor (y’s) mole fractions must sum to 1, we can write the following equations:

y1P=x1P1sat(T)(1−y1)P=(1−x1)P2sat(T)\begin{align} y_1 P = x_1 P_1^{sat}(T) \\ (1-y_1) P = (1-x_1) P_2^{sat}(T) \end{align}

If a mixture of benzene (species 1) and toluene (species 2) has y1=0.33y_1 = 0.33 and P=120 kPaP = 120 \, kPa, find x1x_1 and TT.

  • For full credit, you must use an object-oriented approach to solve this problem; meaning you need to define a class and have a Psat function as part of that class. That Psat function should be written such that all the parameters except for the temperature are defined by self. That is, def Psat(self,T): and then the function should return the vapor pressure referring to the self.antione coefficients in the function.

  • Use a guess value of 0.5 for x1x_1 and 100 °C for TT.

  • You should get about 0.15 for x1x_1 and 110 °C for TT.

  • Report the solved for x1x_1 and TT values.

Credit to Dr. John Hedengren for some of these problems.