Lab 06: NumPy Arrays and Linear Algebra#

Examples (by instructor)#

In this lab, you’ll learn to work with NumPy arrays and perform linear algebra operations essential for chemical engineering calculations.

Example 1: Matrix Operations#

Example 2: Balancing a Synthesis Reaction using Linear Algebra#

Use the linear algebra approach to balance the synthesis of ammonia (Haber process):

\[ N_2 + H_2 \rightarrow NH_3 \]

Approach:

  1. Assign coefficients: a·N₂ + b·H₂ → c·NH₃

  2. Write balance equations for each element (N, H)

  3. Set one coefficient (a = 1) to make the system solvable

  4. Solve for the remaining coefficients (b, c) using np.linalg.solve()

  5. Verify the result


Practice Problems (by students)#

Problem 1: Matrix Inverse#

Define matrix M = np.array([[2, 3], [4, 5]]). Find its inverse M⁻¹ and check that MM⁻¹ = M⁻¹M = I (identity matrix). Check this in Python.

Hint: Use np.linalg.inv() to find the inverse matrix.

Problem 2: Solving System of Linear Equations#

Solve the following system of linear equations by hand, and then check the result in Python (using np.linalg.solve(A, b)):

\[\begin{split} \begin{align} 2x + 3y + 4z &= 6 \\ 4x + 2y - 3z &= 8 \\ -2x + 5y + 8z &= -4 \end{align} \end{split}\]

Note: For np.linalg.solve(), the coefficient matrix A should contain the coefficients of x, y, z, and the vector b should contain the right-hand side values.

Problem 3: Balancing a Combustion Reaction using Linear Algebra#

Use the linear algebra approach to balance the combustion of propane:

\[ C_3H_8 + O_2 \rightarrow CO_2 + H_2O \]

Approach:

  1. Assign coefficients: a·C₃H₈ + b·O₂ → c·CO₂ + d·H₂O

  2. Write balance equations for each element (C, H, O)

  3. Set one coefficient (a = 1) to make the system solvable

  4. Solve for the remaining coefficients (b, c, d) using np.linalg.solve()

  5. Verify the result

Problem 4: Four-Component Separation Train: 5×5 System#

A feed stream passes through two splitters in series before entering a mixer that combines it with a known recycle stream. We have 5 unknown flow rates and can write 5 independent mass balance equations.

                    ┌──────────┐           ┌──────────┐           ┌──────────┐
F1 (100 mol/s) ───► │ Split-1  ├──► S1 ──► │ Split-2  ├──► S2 ──► │  Mixer   ├──► F4 (bottoms)
                    └────┬─────┘           └────┬─────┘           └──────────┘
                         │                      │                       ▲
                         ▼                      ▼                       │
                    F2 (product A)         F3 (product B)          F5 = 10 mol/s
                    (40% of F1)            (50% of S1)              (known recycle)

Unknowns: \(F_2, F_3, F_4, S_1, S_2\) (mol/s)

Known: \(F_1 = 100\) mol/s; \(F_5 = 10\) mol/s (recycle); Split-1 sends 40% to \(F_2\) (so 60% continues as \(S_1\)); Split-2 sends 50% of its inlet to \(F_3\) (so 50% continues as \(S_2\)).

5 equations:

  1. Split-1 specification: \(F_2 = 0.40 \cdot F_1 = 40\)

  2. Split-1 overall balance: \(F_1 = F_2 + S_1\)\(F_2 + S_1 = 100\)

  3. Split-2 specification: \(F_3 = 0.50 \cdot S_1\)\(F_3 - 0.5\,S_1 = 0\)

  4. Split-2 overall balance: \(S_1 = F_3 + S_2\)\(F_3 - S_1 + S_2 = 0\)

  5. Mixer overall balance: \(S_2 + F_5 = F_4\)\(-F_4 + S_2 = -10\) (since \(F_5 = 10\) is known)

Rewriting as \(A\mathbf{x} = \mathbf{b}\) with \(\mathbf{x} = [F_2, F_3, F_4, S_1, S_2]^T\):

Eq.

Equation

\(F_2\)

\(F_3\)

\(F_4\)

\(S_1\)

\(S_2\)

RHS

1

Split-1 spec: \(F_2 = 40\)

1

0

0

0

0

40

2

Split-1 balance: \(F_2 + S_1 = 100\)

1

0

0

1

0

100

3

Split-2 spec: \(F_3 - 0.5\,S_1 = 0\)

0

1

0

-0.5

0

0

4

Split-2 balance: \(F_3 - S_1 + S_2 = 0\)

0

1

0

-1

1

0

5

Mixer balance: \(-F_4 + S_2 = -10\)

0

0

-1

0

1

-10