Data Analysis for Engineers/Module 3

Module 3 · Section 4 of 11

Lesson 3.3 - Linear Algebra

Target: ~10 min read - 20 min hands-on

Overview

np.linalg solves systems of linear equations - essential wherever a physical balance gives you one equation per unknown. np.linalg.solve(A, b) solves Ax = b directly (faster and more numerically stable than inverting A by hand).

Why This Matters (Engineering Context)

Nodal analysis of a circuit, a pipe-network flow balance, a truss, a heat-exchanger network, a mass balance across process units - all reduce to assembling a coefficient matrix and solving Ax = b. This is the linear-algebra core under every analysis package.

Code-Along

import numpy as np

# [Electrical] Nodal analysis of a resistor network as a linear system:  G V = I
# G = node conductance matrix (siemens), I = injected current vector (amps)
G = np.array([
    [ 0.30, -0.10,  0.00],
    [-0.10,  0.25, -0.05],
    [ 0.00, -0.05,  0.15],
])
I = np.array([2.0, 0.0, -1.0])

# np.linalg.solve(A, b) returns x such that A @ x == b (more stable than inv(A) @ b)
V = np.linalg.solve(G, I)
print("Node voltages (V):", np.round(V, 3))
# @ is matrix multiply; G @ V should reproduce I (a quick correctness check)
print("Check (G @ V):", np.round(G @ V, 4), " vs I:", I)

# [Electrical] a 2-mesh circuit -> a 2x2 system for the two loop currents
R = np.array([[10.0, -4.0],
              [-4.0, 12.0]])
E = np.array([6.0, 0.0])
loop_I = np.linalg.solve(R, E)
print("\nLoop currents (A):", np.round(loop_I, 3))
print("det(R):", np.linalg.det(R))   # determinant != 0 -> the system has a unique solution

Run it: G @ V should reproduce I to rounding. A negative node voltage or loop current is a perfectly normal result - the sign just tells you the actual direction relative to your assumed reference.

Practice Exercises

  1. Change I[0] from 2.0 to 3.0 and re-solve - how do the node voltages change?
  2. Use np.linalg.inv(G) @ I and verify it matches np.linalg.solve(G, I).
  3. For the 2-mesh circuit, what happens to the loop currents if E = [6.0, 6.0] instead of [6.0, 0.0]?
# Try the practice exercises here

Knowledge Check

  1. What does np.linalg.solve(A, b) compute?
  2. What does a negative solved value (node voltage, loop current, member force) usually indicate?
  3. Why is np.linalg.solve() generally preferred over inverting A and multiplying?
Answer key
  1. The solution vector x to the linear system Ax = b
  2. The actual direction/sign is opposite to the reference you assumed - not an error
  3. It's more numerically stable and efficient than explicitly forming a matrix inverse

26 / 63 sections · Course home · Join the coaching cohort