Back to the Electrical Engineering roadmap

Linear Algebra I · LU factorization with pivoting

Prerequisites: Gaussian elimination, matrix products, permutations, determinants and consistency of linear systems. Goals: construct and check PA=LU, apply the same permutation to each right-hand side, solve triangular systems, preserve earlier multipliers during a later swap, and reuse factors only while A stays fixed. Work through the examples and downloadable lab, then attempt the open-book assessment before revealing its rubric.

Review elimination · Review determinants · Review consistency and ambiguity

The convention: permute A, then factor

For a real square matrix A, L is unit lower triangular (ones on its diagonal, zeros above) and U is upper triangular (zeros below). A permutation matrix P rearranges rows; P⁻¹=Pᵀ. We use PA=LU throughout. Thus A=PᵀLU, and Ax=b is equivalent to LUx=Pb. Some sources write A=PLU with a differently defined permutation; identify the convention before copying a formula. A product of several row swaps need not satisfy P=Pᵀ.

Without swaps, one elimination step subtracts ℓ times a pivot row. Its elimination matrix is E=I−ℓeᵢeₖᵀ for i>k; E⁻¹=I+ℓeᵢeₖᵀ because (eᵢeₖᵀ)²=0. If Eₛ⋯E₁A=U, then A=E₁⁻¹⋯Eₛ⁻¹U=LU. Products of unit lower triangular matrices remain unit lower triangular. L stores the multipliers ℓ, not their negatives. For a 2×2 matrix with a≠0, direct multiplication verifies L=[1 0;c/a 1], U=[a b;0 d−cb/a], and LU=[a b;c d].

Partial pivoting and later row swaps

  1. Start with U=A, L=I and P=I. At column k, choose a row p≥k maximizing |uₚₖ| among the active rows; keep the first row on a tie. Swap rows k,p of U and P. This is row partial pivoting; columns are not permuted.
  2. If k>1, also swap entries in columns 1,…,k−1 of those two rows of L. These already stored multipliers refer to row histories that have just moved. Do not swap entire L rows: its unbuilt trailing part must retain the unit lower triangular structure. With S the active-row swap, the invariant SPA=(SLSᵀ)(SU) explains this prefix update because the unbuilt trailing block of L is still the identity. Example 3 checks it numerically.
  3. If the chosen pivot is nonzero, set ℓᵢₖ=uᵢₖ/uₖₖ for each i>k and update row i of U by subtracting ℓᵢₖ times row k. Store ℓᵢₖ in L. Continue until U is upper triangular. Validate PA=LU explicitly. If every active entry in a pivot column is zero, do not divide: that step has no pivot. A singular matrix can still have triangular factors, but unique triangular solution requires every diagonal entry of U to be nonzero.

Partial pivoting avoids a zero pivot when an available nonzero entry can replace it and keeps multipliers bounded by 1 in magnitude. It often reduces rounding problems, but is not a universal numerical-stability or small-forward-error guarantee. Small residual does not imply accurate unknowns for an ill-conditioned system. In this lab, rank and singularity are decided from exact half-unit inputs, not from a rounded display.

Two triangular solves and factor reuse

For each new b, form g=Pb. Solve Ly=g forward: yᵢ=gᵢ−∑ⱼ₍ⱼ<ᵢ₎ℓᵢⱼyⱼ, since L has unit diagonal. Then solve Ux=y backward: xᵢ=(yᵢ−∑ⱼ₍ⱼ>ᵢ₎uᵢⱼxⱼ)/uᵢᵢ, requiring uᵢᵢ≠0. Substitution verifies each row as soon as its unknown is determined. The proof of the whole procedure is LUx=Ly=g=Pb, followed by multiplication by Pᵀ to obtain Ax=b. Always check the original residual Ax−b, not only the permuted system.

For several right-hand sides, factor A once and reuse P,L,U; recompute Pb and both triangular solves each time. Dense factorization takes O(n³) arithmetic, while each pair of triangular solves takes O(n²), as seen from the three nested elimination loops versus the triangular sums. These are operation-count orders, not timing promises for this small lab. If A changes, its old factors generally cannot be reused. Also det(A)=(−1)ˢ∏uᵢᵢ for s row swaps, since det(P)=(−1)ˢ and det(L)=1. Forgetting the swap sign can give the wrong determinant.

Original right-hand sidePermute the dataForward substitutionBackward substitution b=(1,2)Pb=(2,1)y=(2,0)x=(1/2,0)PLy=PbUx=y
Original solve-flow diagram for the first lab case. P,L,U depend on A; the right-hand side passes through the permutation and two solves. The intermediate y is not the final solution x.

Worked examples

  1. No swap needed: A=[2 1;1 3], P=I. The multiplier is 1/2, giving L=[1 0;1/2 1], U=[2 1;0 5/2]. For b=(1,2), forward substitution gives y=(1,3/2), then x=(1/5,3/5). For b′=(3,1), reuse the factors: y′=(3,−1/2), x′=(8/5,−1/5). Direct multiplication verifies both. The determinant is 2·(5/2)=5.
  2. Zero first pivot, nonsingular matrix: A=[0 2;1 3]. Swap rows, so P=[0 1;1 0], L=I, U=[1 3;0 2]. With b=(4,5), Pb=(5,4), y=(5,4), then x₂=2 and x₁=5−3·2=−1. Thus x=(−1,2), and Ax=(4,5). The determinant is −(1·2)=−2. Without a row swap, a unit-diagonal LU cannot exist here: its first diagonal U entry would be 0 but the required lower-left A entry is 1. A zero original pivot does not prove singularity.
  3. A later swap must move old multipliers: A=[1 1 0;2 2 1;0 3 1]. Swap rows 1,2 and eliminate with multiplier 1/2. The trailing rows become (0,0,−1/2),(0,3,1), so swap rows 2,3. Move the previously stored first-column multiplier 1/2 into row 3 of L. Final factors are P=[0 1 0;0 0 1;1 0 0], L=[1 0 0;0 1 0;1/2 0 1], U=[2 2 1;0 3 1;0 0 −1/2]. Their product is PA=[2 2 1;0 3 1;1 1 0]. For b=(3,9,9), Pb=(9,9,3), y=(9,9,−3/2), and x=(1,2,3). Here P≠Pᵀ; two swaps give det(A)=2·3·(−1/2)=−3.
  4. Singular factors, two different outcomes: A=[1 2;2 4] gives P=[0 1;1 0], L=[1 0;1/2 1], U=[2 4;0 0]. For b=(3,6), y=(6,0), so x₁+2x₂=3 gives infinitely many solutions. For b=(3,7), y=(7,−1/2), and the last triangular equation is 0=−1/2, so no exact solution exists. The factors alone do not decide consistency without the right-hand side.

Common mistakes and checks

Do not solve Ly=b after factoring PA=LU: use Pb. Do not reverse L and U or store the negative of an elimination multiplier. A later row swap must also move the completed part of L. Verify PA=LU, Ly=Pb, Ux=y and Ax=b. Do not divide by a zero U diagonal, call every zero original pivot singular, or declare a singular system inconsistent without checking b. A representative singular solution obtained by setting free variables to zero is not automatically minimum norm.

Practice

  1. Factor A=[3 1;1 2] with row partial pivoting and solve Ax=(4,5). Then reuse the factors for (1,−1).
  2. For A=[0 1;2 3] and b=(1,5), construct P,L,U, solve, and calculate det(A) with the permutation sign.
  3. In worked example 3, what goes wrong if L keeps its multiplier 1/2 in row 2 instead of row 3? Compute row 2 of that incorrect LU and compare with PA.
  4. For A=[0 1;0 2], classify Ax=(1,2) and Ax=(1,−1). Explain why a zero first column prevents a unique solution but does not prevent every exact solution.
Fully worked solutions
  1. P=I,L=[1 0;1/3 1],U=[3 1;0 5/3]. For (4,5), y=(4,11/3), then x=(3/5,11/5). For (1,−1), y=(1,−4/3), then x=(3/5,−4/5). Multiplying each x by A recovers its right-hand side.
  2. Swap rows: P=[0 1;1 0],L=I,U=[2 3;0 1]. Pb=(5,1), so x₂=1 and x₁=(5−3)/2=1. Thus x=(1,1); det(A)=−2·1=−2, not +2.
  3. The incorrect row 2 is half of U row 1 plus U row 2: (1,1,1/2)+(0,3,1)=(1,4,3/2). But PA row 2 is (0,3,1). The earlier multiplier belongs to the row that moved, so PA≠LU unless its stored position also moves.
  4. For (1,2), x₂=1 and x₁ is arbitrary: infinitely many exact solutions. For (1,−1), x₂ would need to equal 1 and −1/2 simultaneously, so it is inconsistent. The first column zero means changing x₁ never changes Ax, preventing uniqueness.

Assessment · Open book · 18 points

Open book: use books, lesson notes, cited readings, calculators, and relevant free software. Explain your own reasoning, assumptions, units, and checks; identify references or software you use. Attempt the questions before opening the answer key. Suggested study time: 40–60 minutes; this is a self-study guide, not a timed restriction.

Use references and relevant free software. State the permutation convention and show factor and residual checks. The application problem adds 6 points to these 12.

  1. Factor A=[1 2;3 4] with partial pivoting and solve Ax=(5,6), showing Pb, the forward vector and original residual. (5)
  2. Use the factors in worked example 3 for b=(3,9,9). Compute Pb,y,x and det(A); explain through your displayed Pb why leaving b unpermuted is not the same algorithm. (4)
  3. For S=[1 2;2 4], classify right-hand sides (1,2) and (1,3). Does a zero U diagonal justify computing S⁻¹ or dividing to force one solution? Explain. (3)

Application and reasoning · 6 points

For A=[2 1;4 3], use P=[0 1;1 0], L=[1 0;1/2 1], U=[4 3;0 −1/2]. A solver receives new data r=(3,1), uses Ly=r instead of Ly=Pr, and reports x=(0,1). Diagnose the error by tracing the wrong forward vector and checking Ax (2). Repair the solve and verify it (2). If A then changes to [2 1;4 4], may the old factors be reused? Show a factor-check discrepancy and state what must change (2).

Complete answer key and scoring rubric
  1. P=[0 1;1 0] (1). L=[1 0;1/3 1],U=[3 4;0 2/3], with PA=LU (1). Pb=(6,5),y=(6,3) (1). Backward substitution gives x₂=9/2,x₁=−4 (1). A(−4,9/2)=(5,6), so the residual is zero (1).
  2. Pb=(9,9,3), which differs from b (1). Forward y=(9,9,−3/2) (1). Backward x=(1,2,3) (1). Two swaps have positive sign, so det(A)=2·3·(−1/2)=−3 (1).
  3. (1,2) satisfies row 2 = twice row 1 and gives infinitely many solutions x₁+2x₂=1 (1). For (1,3), elimination gives 0=1, so no exact solution (1). S is singular: no inverse exists and division by zero is invalid (1).

Application and reasoning · 6 points

The wrong forward vector is y=(3,−1/2), and back substitution gives (0,1) (1). Its image A(0,1)=(1,3)=Pr, not r=(3,1), revealing the missing permutation (1). Correct Pr=(1,3), y=(1,5/2) (1), and x=(4,−5), with A(4,−5)=(3,1) (1). For the changed matrix, PAnew=[4 4;2 1] while old LU=[4 3;2 1] (1). The coefficient matrix changed, so a new factorization or a separately justified update is required before reuse; simply changing the right-hand side cannot fix PAnew≠LU (1).

Scoring: existing questions 12 + application problem 6 = 18 points. Award the stated method and reasoning marks, not only final answers.

Download the pivoted-LU lab

Download the exact-fraction Python lab

Objective: factor a 2×2 A once, reuse it for two right-hand sides, and classify singular cases safely. Install free Python 3 for Windows, macOS or Linux from the official link below. Save the file, open a terminal in its folder, and run the commands; Windows can use py -3 instead of python3. No account, university login, paid license or additional packages are required, and it runs offline after installation. The six inputs a,b,c,d,u,v define A=[a b;c d] and first right-hand side (u,v); the second is fixed at (1,−1). Each input lies from −4 to 6 in half-unit steps; fractions such as -1/2 are accepted.

python3 ee-lu-factorization.py 2 1 4 3 1 2
python3 ee-lu-factorization.py 0 2 1 3 4 5
python3 ee-lu-factorization.py 3 1 1 2 4 5
python3 ee-lu-factorization.py 1 2 2 4 3 6
python3 ee-lu-factorization.py 0 0 0 0 0 0
python3 ee-lu-factorization.py 0 1 0 2 1 2

Procedure: predict whether a swap occurs, write P,L,U and det(A), then predict the solution status for both right-hand sides. The first three runs have unique first solutions (1/2,0),(−1,2),(3/5,11/5); their second solutions are (2,−3),(−5/2,1/2),(3/5,−4/5). The last three first systems have infinitely many solutions; their displayed representatives with free variables set to zero are (3,0),(0,0),(0,1). Their second systems are inconsistent. These representatives are not claimed minimum norm. Compare Python and browser outputs. Python checks PA=LU exactly, uses forward/back substitution for nonsingular matrices and independently validates with RREF; singular cases use RREF rather than a zero-pivot division.

Questions with answers: why can the first run reuse factors for its second right-hand side? A is unchanged. Why does the second run need a swap despite being invertible? Its original first pivot is zero, but a nonzero replacement exists below. Why does the fifth run classify the zero right-hand side differently from (1,−1)? A=0 maps every input to zero, so only zero data are compatible. The browser uses floating point and six-decimal displays; factor and solve checks use entrywise scaled tolerance 10⁻⁹·max(1,|reference|). Singularity and compatibility for its bounded half-unit 2×2 inputs are decided by exact cross-products, not a tolerance on a rounded pivot. The activity is a small synthetic algebra model, not a general numerical-stability benchmark or substitute for physical instrument training.

Free primary readings and next step

Read MIT Lecture 4 and its summary for elimination factors and triangular solves. Then read Fundamentals of Numerical Computation §2.4 for LU and §2.6 for row partial pivoting; check its permutation convention before comparing formulas. The online text is public without purchasing its print edition, and the lesson’s original lab needs none of the external examples’ optional numerical packages. The scoped coverage review is now available with the cumulative Linear Algebra I review and open-book practice final in the next lesson. EE Year 1 is still in progress.

LU lab · Factor once, solve twice

Construct PA=LU for A=[a b;c d], solve for (u,v) and fixed (1,−1), and classify singular cases.

Free tool built into this page. Use it in your browser without an account or download.

Procedure

Try (a,b,c,d,u,v)=(2,1,4,3,1,2),(0,2,1,3,4,5),(3,1,1,2,4,5),(1,2,2,4,3,6),(0,0,0,0,0,0),(0,1,0,2,1,2). Predict swaps, factors and both solution statuses. Compare with Python. For singular consistent cases, the displayed solution sets free variables to zero; it is not claimed minimum norm. Use decimal halves such as −0.5 in the browser.

Expected results and answer check

The first three first solutions are (1/2,0),(−1,2),(3/5,11/5), and the second solutions are (2,−3),(−5/2,1/2),(3/5,−4/5). The last three first systems have infinitely many solutions and second systems are inconsistent. Reuse is justified by the same A, not by the right-hand sides being similar.

The shared activity is bounded 2×2 algebra. Python uses exact fractions; browser checks use floating point, scaled tolerance 1e−9 and rounded displays. Exact input cross-products decide singularity and compatibility. This is not a general stability benchmark or a physical experiment.