5 - Conditioning and Stability

Updated 4 Oct 2026

Big-Oh Notation for Small Variables

Example

  • Consider f(n)=4n4+3n3−n2+9nf(n) = 4n^4 + 3n^3 - n^2 + 9n
    • When n≫1n \gg 1:
    • f(n)=O(n4)f(n) = O(n^4)
  • What if 0<n≪10 < n \ll 1?
    • In this case: n>n2>n3>n4n > n^2 > n^3 > n^4
    • So f(n)=O(n)f(n) = O(n)

General Rules

  • When n≫1n \gg 1:
    O(np)=cpnp+cp−1np−1+…+c0\boxed{O(n^p) = c_p n^p + c_{p-1} n^{p-1} + \ldots + c_0}
  • When 0<h≪10 < h \ll 1:
    O(hp)=cphp+cp+1hp+1+cp+2hp+2+…\boxed{O(h^p) = c_p h^p + c_{p+1} h^{p+1} + c_{p+2} h^{p+2} + \ldots}
  • Convention: hh is used for small values

Causes of Inaccurate Solutions

There are two main causes:

  • Bad algorithms
  • The problems themselves (Nature, give up!)

Conditioning

อันนี้มาดูของ Problem themselves ก่อน

Definitions

  • Well-conditioned (insensitive) problem:
    • A small change in the input data causes a reasonably small change in the solution
  • Ill-conditioned (sensitive) problem:
    • A small change in the input data causes a much larger change in the solution relatively

Examples of Ill- and Well-conditioned Problems

Example 1: Computing cos⁡x\cos x (Ill-conditioned case)

The problem: Compute cos⁡x\cos x

  • Input: x=1020πx = 10^{20}\pi

  • Exact answer: cos⁡(1020π)=1\cos(10^{20}\pi) = 1

  • What if input changes to (1020+1)π(10^{20} + 1)\pi?

  • Solution becomes: cos⁡((1020+1)π)=cos⁡(1020π+π)=−1\cos((10^{20} + 1)\pi) = \cos(10^{20}\pi + \pi) = -1

  • Relative change of input:
    ∣(1020π+π)−1020π∣∣1020π∣=π1020π=11020\frac{|(10^{20}\pi + \pi) - 10^{20}\pi|}{|10^{20}\pi|} = \frac{\pi}{10^{20}\pi} = \frac{1}{10^{20}}

  • Relative change of solution:
    ∣−1−1∣∣1∣=2\frac{|-1 - 1|}{|1|} = 2

  • Conclusion: Ill-conditioned because small relative change to the data leads to large relative change to the solution

เปลี่ยน input จาก 1020π10^{20}\pi → (1020+1)π(10^{20}+1)\pi ซึ่งเป็นแค่การเปลี่ยนไป 1/10201/10^{20} ของค่าเดิม
แต่ผลลัพธ์กระโดดจาก 11 → −1-1 เลย → Ill-conditioned

Example 2: Computing cos⁡x\cos x (Well-conditioned case)

  • Input: x=0.1x = 0.1
  • cos⁡(0.1)\cos(0.1) is well-conditioned
  • Any small changes to the input yields small change in output

Analysis:

  • cos⁡(0.1)=0.1−(0.1)22!+(0.1)44!−…\cos(0.1) = 0.1 - \frac{(0.1)^2}{2!} + \frac{(0.1)^4}{4!} - \ldots
  • cos⁡(0.1+ϵ)=(0.1+ϵ)−(0.1+ϵ)22!+(0.1+ϵ)44!−…\cos(0.1 + \epsilon) = (0.1 + \epsilon) - \frac{(0.1 + \epsilon)^2}{2!} + \frac{(0.1 + \epsilon)^4}{4!} - \ldots
  • cos⁡(0.1+ϵ)−cos⁡(0.1)=O(ϵ)\cos(0.1 + \epsilon) - \cos(0.1) = O(\epsilon)

จะเห็นได้ว่าจาก Example 1 & 2 จะโชว์ว่าเป็น Ill-condition นั้นง่ายกว่ามาก เพราะว่าจะโชว์ว่าเป็น Ill- ก็โชว์แค่ one particular example

แต่จะโชว์ Well- ต้องโชว์ any small changes input causes small change to the solution which is harder

Example 3: Finding Roots of Quadratic Equations

The problem: Find the roots of ax2+bx+c=0ax^2 + bx + c = 0

Case 1: Ill-conditioned

  • Roots of x2−2x+1=0x^2 - 2x + 1 = 0 (i.e., a=1a = 1, b=−2b = -2, c=1c = 1)

  • Exact root(s):
    x2−2x+1=0x^2 - 2x + 1 = 0
    (x−1)2=0(x - 1)^2 = 0
    x=1x = 1

  • Suppose we perturb cc to 1−δ1 - \delta where δ>0\delta > 0 is very small:
    x2−2x+(1−δ)=0x^2 - 2x + (1 - \delta) = 0

  • Roots of perturbed equation:
    2±(−2)2−4(1−δ)2=2±4−4+4δ2=2±4δ2=1±δ\frac{2 \pm \sqrt{(-2)^2 - 4(1 - \delta)}}{2} = \frac{2 \pm \sqrt{4 - 4 + 4\delta}}{2} = \frac{2 \pm \sqrt{4\delta}}{2} = 1 \pm \sqrt{\delta}

  • Relative change of input:
    ∣(1−δ)−1∣∣1∣=δ\frac{|(1 - \delta) - 1|}{|1|} = \delta

  • Relative change of solution:
    ∣(1±δ)−1∣∣1∣=δ\frac{|(1 \pm \sqrt{\delta}) - 1|}{|1|} = \sqrt{\delta}

  • Conclusion: Ill-conditioned because if δ=10−16⇒δ=10−8\delta = 10^{-16} \Rightarrow \sqrt{\delta} = 10^{-8}

  • Large change in solution compared to change in input

จาก x2−2x+1=0x^2 - 2x + 1 = 0 ได้ root = 1
ถ้าเปลี่ยน cc จาก 11 → 1−δ1-\delta นิดเดียว แต่คำตอบ root กระโดดเป็น 1±δ1 \pm \sqrt{\delta}
เช่น δ=10−16\delta = 10^{-16} คำตอบเปลี่ยนระดับ 10−810^{-8} เลย → Ill-conditioned

Case 2: Well-conditioned

  • Finding the roots of x2−3x+1=0x^2 - 3x + 1 = 0 is well-conditioned

END OF WEEK 3

Example 4: Solving Linear Systems Ax=bAx = b

Case 1: Well-conditioned

  • Solving [1101]x=[34]\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} x = \begin{bmatrix} 3 \\ 4 \end{bmatrix} is well-conditioned
  • Small changes to the matrix and/or right-hand side vector result in small changes of the solution

Case 2: Ill-conditioned

  • Consider solving [1236+δ]x=[12]\begin{bmatrix} 1 & 2 \\ 3 & 6 + \delta \end{bmatrix} x = \begin{bmatrix} 1 \\ 2 \end{bmatrix} where δ>0\delta > 0 is very small
  • This is ill-conditioned

Numerical Example:

  • Solution of [1236+10−8]x=[12]\begin{bmatrix} 1 & 2 \\ 3 & 6 + 10^{-8} \end{bmatrix} x = \begin{bmatrix} 1 \\ 2 \end{bmatrix} is x≈[2×108−1×108]x \approx \begin{bmatrix} 2 \times 10^8 \\ -1 \times 10^8 \end{bmatrix}
  • Solution of [1236+10−9]x^=[12]\begin{bmatrix} 1 & 2 \\ 3 & 6 + 10^{-9} \end{bmatrix} \hat{x} = \begin{bmatrix} 1 \\ 2 \end{bmatrix} is x^≈[2×109−1×109]\hat{x} \approx \begin{bmatrix} 2 \times 10^9 \\ -1 \times 10^9 \end{bmatrix}

ดูสิเปลี่ยน Input ไปนิดเดียว คือ คำตอบเพิ่มขึ้น 10 เท่า กูงงเลยล่ะ

Analysis:

  • Relative change in input:
    ∥[1236+10−9]−[1236+10−8]∥1∥[1236+10−8]∥1≈1.12×10−9\frac{\left\|\begin{bmatrix} 1 & 2 \\ 3 & 6 + 10^{-9} \end{bmatrix} - \begin{bmatrix} 1 & 2 \\ 3 & 6 + 10^{-8} \end{bmatrix}\right\|_1}{\left\|\begin{bmatrix} 1 & 2 \\ 3 & 6 + 10^{-8} \end{bmatrix}\right\|_1} \approx 1.12 \times 10^{-9}
  • Relative change in solution:
    ∥x^−x∥1∥x∥1≈9\frac{\|\hat{x} - x\|_1}{\|x\|_1} \approx 9

Condition Number

อันนี้ก็มาดูบ้างว่าแล้วถ้าเป็น Well condition, อันไหนดีกว่าล่ะ มันก็มี well มาก well น้อยถูก้ปะ
เพราะในทางคณิตศาสตร์ “well-conditioned” ไม่ได้มีแค่ขาวหรือดำ แต่มีระดับความ “well” อยู่ → ซึ่งวัดได้ด้วย Condition Number

Definition

  • Define the condition number of a problem as the ratio of the relative change in the solution to the relative change in the input
  • For example, if the problem is to compute cos⁡x\cos x, xx is the true input, and x^\hat{x} is the perturbed input, then:

Condition number=∣(cos⁡x^−cos⁡x)/cos⁡x∣∣(x^−x)/x∣{\text{Condition number} = \frac{|(\cos \hat{x} - \cos x) / \cos x|}{|(\hat{x} - x)/x|}}

Small condition number = well condition

  • But the above definition is very difficult to compute for complex problems therefore
    • ถ้าเอาความหมายแบบด้านบนคือมันทำได้ยาก คนก็เลยบอก relax หน่อยละกัน
  • A rough estimate or an upper bound of condition numbers over some domain of inputs are usually used instead

Conditioning of a Linear System

Problem Statement

  • How sensitive is a solution xx to Ax=bAx = b if AA and bb are perturbed?

Setup

  • Suppose the true problem (with true inputs) is:
    Ax=bAx = b
  • Perturb AA to A+EA + E and bb to b+eb + e, where EE and ee are "small"
  • The perturbed problem is therefore:
    (A+E)x^=b+e(A + E)\hat{x} = b + e

Small Perturbation Analysis

  • We consider small perturbation. That is: (LHS, and RHS เลยนะข้าง่างอะ)
    ∥(A+E)−A∥∥A∥=∥E∥∥A∥≤δ\frac{\|(A + E) - A\|}{\|A\|} = \frac{\|E\|}{\|A\|} \leq \delta
    ∥(b+e)−b∥∥b∥=∥e∥∥b∥≤δ\frac{\|(b + e) - b\|}{\|b\|} = \frac{\|e\|}{\|b\|} \leq \delta

  • where δ>0\delta > 0 is small (say, 0<δ≪10 < \delta \ll 1)

  • Above are the same as: (คืออันเดียวกันข้างบน แค่ย้ายไปอีกฝั่ง)
    ∥E∥≤δ∥A∥(1)\boxed{\|E\| \leq \delta \|A\|} \quad (1)
    ∥e∥≤δ∥b∥(2)\boxed{\|e\| \leq \delta \|b\|} \quad (2)

  • Assume pp-norm is used for vectors and matrices

Derivation

Starting from the perturbed equation:
(A+E)x^=b+e(A + E)\hat{x} = b + e
Ax^+Ex^=b+eA\hat{x} + E\hat{x} = b + e
Ax^=b+e−Ex^A\hat{x} = b + e - E\hat{x}
x^=A−1b+A−1e−A−1Ex^\hat{x} = A^{-1}b + A^{-1}e - A^{-1}E\hat{x}

But A−1b=xA^{-1}b = x. So:
x^=x+A−1e−A−1Ex^\hat{x} = x + A^{-1}e - A^{-1}E\hat{x}
x^−x=A−1e−A−1Ex^\hat{x} - x = A^{-1}e - A^{-1}E\hat{x}
∥x^−x∥=∥A−1e−A−1Ex^∥\|\hat{x} - x\| = \|A^{-1}e - A^{-1}E\hat{x}\|

Using triangular inequality:
∥x^−x∥≤∥A−1e∥+∥−A−1Ex^∥\|\hat{x} - x\| \leq \|A^{-1}e\| + \|-A^{-1}E\hat{x}\|
=∥A−1e∥+∥A−1Ex^∥= \|A^{-1}e\| + \|A^{-1}E\hat{x}\|
≤∥A−1∥⋅∥e∥+∥A−1∥⋅∥E∥⋅∥x^∥\leq \|A^{-1}\| \cdot \|e\| + \|A^{-1}\| \cdot \|E\| \cdot \|\hat{x}\|

(using subordination and submultiplicativity ก็คือใช้ 3 - Norms)

Continuing the Analysis

From (1) and (2):
∥x^−x∥≤∥A−1∥⋅δ∥b∥+∥A−1∥⋅δ∥A∥⋅∥x^∥\|\hat{x} - x\| \leq \|A^{-1}\| \cdot \delta \|b\| + \|A^{-1}\| \cdot \delta \|A\| \cdot \|\hat{x}\|

Note that:
∥x^∥=∥x+(x^−x)∥≤∥x∥+∥x^−x∥\|\hat{x}\| = \|x + (\hat{x} - x)\| \leq \|x\| + \|\hat{x} - x\|

From above:
∥x^−x∥≤δ∥A−1∥⋅∥b∥+δ∥A−1∥⋅∥A∥(∥x^−x∥+∥x∥)\|\hat{x} - x\| \leq \delta \|A^{-1}\| \cdot \|b\| + \delta \|A^{-1}\| \cdot \|A\| (\|\hat{x} - x\| + \|x\|)

Let κ(A)\kappa(A) denote ∥A−1∥⋅∥A∥\|A^{-1}\| \cdot \|A\|.

Rewrite as:
∥x^−x∥≤δ∥A−1∥⋅∥b∥+δκ(A)(∥x^−x∥+∥x∥)\|\hat{x} - x\| \leq \delta \|A^{-1}\| \cdot \|b\| + \delta\kappa(A) (\|\hat{x} - x\| + \|x\|)
∥x^−x∥≤δ∥A−1∥⋅∥b∥+δκ(A)∥x^−x∥+δκ(A)∥x∥\|\hat{x} - x\| \leq \delta \|A^{-1}\| \cdot \|b\| + \delta\kappa(A) \|\hat{x} - x\| + \delta\kappa(A) \|x\|

Then:
∥x^−x∥−δκ(A)∥x^−x∥≤δ∥A−1∥∥b∥+δκ(A)∥x∥\|\hat{x} - x\| - \delta\kappa(A) \|\hat{x} - x\| \leq \delta \|A^{-1}\| \|b\| + \delta\kappa(A) \|x\|
(1−δκ(A))∥x^−x∥≤δ∥A−1∥∥b∥+δκ(A)∥x∥(1 - \delta\kappa(A)) \|\hat{x} - x\| \leq \delta \|A^{-1}\| \|b\| + \delta\kappa(A) \|x\|

Final Result

But b=Axb = Ax. So:
∥b∥=∥Ax∥≤∥A∥⋅∥x∥\|b\| = \|Ax\| \leq \|A\| \cdot \|x\|

From the previous slide:
(1−δκ(A))∥x^−x∥≤δ∥A−1∥⋅∥A∥⋅∥x∥+δκ(A)∥x∥(1 - \delta\kappa(A)) \|\hat{x} - x\| \leq \delta \|A^{-1}\| \cdot \|A\| \cdot \|x\| + \delta\kappa(A) \|x\|
=δκ(A)∥x∥+δκ(A)∥x∥= \delta\kappa(A) \|x\| + \delta\kappa(A) \|x\|
=2δκ(A)∥x∥= 2\delta\kappa(A) \|x\|

Therefore:
(1−δκ(A))∥x^−x∥∥x∥≤2δκ(A)(1 - \delta\kappa(A)) \frac{\|\hat{x} - x\|}{\|x\|} \leq 2\delta\kappa(A)

∥x^−x∥∥x∥≤2δκ(A)1−δκ(A)\boxed{\frac{\|\hat{x} - x\|}{\|x\|} \leq \frac{2\delta\kappa(A)}{1 - \delta\kappa(A)}}

  • ผลลัพธ์สุดท้าย บอกเราว่า relative error ของคำตอบ (solution) ถูกควบคุมโดย δ\delta (ขนาดของ input error) และ κ(A)\kappa(A) (condition number)
  • ใจความสำคัญ
    • ถ้า κ(A)\kappa(A) เล็ก → well-conditioned
    • ถ้า κ(A)\kappa(A) ใหญ่ → ill-conditioned

Conclusion

  • When δ\delta is small, the right-hand side is an increasing function with respect to δκ(A)\delta\kappa(A)
  • The relative change of the solution of a linear system depends on κ(A)\kappa(A) (and the relative change of the inputs δ\delta)
  • The larger κ(A)\kappa(A) is, the larger the relative change of the solution

Matrix Condition Number

Definition

  • The condition number of a nonsingular square matrix AA is defined to be:
    cond(A)=∥A∥⋅∥A−1∥\boxed{\text{cond}(A) = \|A\| \cdot \|A^{-1}\|}

  • If AA is singular, say that cond(A)=∞\text{cond}(A) = \infty

  • Sometimes written as κ(A)\kappa(A)

  • The larger cond(A)\text{cond}(A) is, the more ill-conditioned the linear system is

Example 1

Compute the condition numbers in 1-norm and ∞\infty-norm of:
A=[20−11]A = \begin{bmatrix} 2 & 0 \\ -1 & 1 \end{bmatrix}

Solution:

  • อันนี้เป็น 2x2 หา Inverse ก็ง่าย ๆ เลยอะ ใช้สูตรได้เลยจาก 05 Inverse Matrix หรือถ้ามันอย่างกว่านั้นก็ใช้ GEPP เด้อ
    A−1=12⋅[1012]=[1/201/21]A^{-1} = \frac{1}{2} \cdot \begin{bmatrix} 1 & 0 \\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 1/2 & 0 \\ 1/2 & 1 \end{bmatrix}

So:

  • cond1(A)=∥A∥1⋅∥A−1∥1=3(1)=3\text{cond}_1(A) = \|A\|_1 \cdot \|A^{-1}\|_1 = 3(1) = 3
    • ก็คืออิงจากการหา ∥A∥1\|A\|_1 ได้จาก 3 - Norms เลยนะ
  • cond∞(A)=∥A∥∞⋅∥A−1∥∞=2(3/2)=3\text{cond}_\infty(A) = \|A\|_\infty \cdot \|A^{-1}\|_\infty = 2(3/2) = 3

Exercise 2

Compute the condition numbers in 1-norm and ∞\infty-norm of:
A=[2−111013−14]A = \begin{bmatrix} 2 & -1 & 1 \\ 1 & 0 & 1 \\ 3 & -1 & 4 \end{bmatrix}
Solution:

  • เช่นเคยมันก็มีสูตรการหา A−1A^{-1 } อะนะ แต่ว่าเราจะใช้ GEPP ก็ได้ too!
    A−1=[.51.5−.5−.52.5−.5−.5−.5.5]A^{-1} = \begin{bmatrix} .5 & 1.5 & -.5 \\ -.5 & 2.5 & -.5 \\ -.5 & -.5 & .5 \end{bmatrix}
    So:
  • cond1(A)=∥A∥1⋅∥A−1∥1=6(4.5)=27\text{cond}_1(A) = \|A\|_1 \cdot \|A^{-1}\|_1 = 6(4.5) = 27
  • cond∞(A)=∥A∥∞⋅∥A−1∥∞=8(3.5)=28\text{cond}_\infty(A) = \|A\|_\infty \cdot \|A^{-1}\|_\infty = 8(3.5) = 28

แค่นี้เราก็ได้คำตอบแล้วว่า Exercise 2 เป็น more ill condition กว่า Example 1 55555 เพราะว่าตัวเลข Matrix Condition Number เยอะกว่านั่นเอง!

Condition Numbers in MATLAB

>> cond(A, p); % p can be 1, 2, Inf, or 'fro'
>> cond(A); % This gives the condition number in 2-norm.

Properties of Matrix Condition Number

Property 1: Lower Bound

Claim: condp(A)≥1\text{cond}_p(A) \geq 1

Proof:
condp(A)=∥A∥p∥A−1∥p≥∥AA−1∥p=∥I∥p\text{cond}_p(A) = \|A\|_p \|A^{-1}\|_p \geq \|AA^{-1}\|_p = \|I\|_p

But:
∥I∥p=max⁡x≠0∥Ix∥p∥x∥p=max⁡x≠0∥x∥p∥x∥p=1\|I\|_p = \max_{x \neq 0} \frac{\|Ix\|_p}{\|x\|_p} = \max_{x \neq 0} \frac{\|x\|_p}{\|x\|_p} = 1

Therefore: condp(A)≥∥I∥p=1\boxed{\text{cond}_p(A) \geq \|I\|_p = 1}

บอกเราว่าไม่มีระบบไหน “ดีกว่า identity” ได้ ค่าต่ำสุดคือ 1 เท่านั้น

Property 2: Identity Matrix

Claim: condp(I)=1\text{cond}_p(I) = 1

Proof:

  • Recall I−1=II^{-1} = I
  • So: condp(I)=∥I∥p∥I−1∥p=∥I∥p2=1\text{cond}_p(I) = \|I\|_p \|I^{-1}\|_p = \|I\|_p^2 = 1
  • (∥I∥p=1\|I\|_p = 1 from the previous proof)

Note: Not true for Frobenius norm. E.g.:

\end{bmatrix}\right\|_F \left\|\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\right\|_F = \sqrt{1 + 1} \cdot \sqrt{1 + 1} = 2$$ >identity matrix คือกรณีสมบูรณ์แบบที่สุด (perfectly conditioned) >**==“ค่าที่เล็กสุดคือ 1 → ยิ่งใกล้ 1 ยิ่งดี”==** ### Property 3: Scaling Invariance **Claim:** If $\alpha \neq 0$ is a scalar, then: $$\boxed{\text{cond}(A) = \text{cond}(\alpha A)}$$ under any matrix norms (not only $p$-norms). **Proof:** - Note that: $(\alpha A)^{-1} = \frac{1}{\alpha} A^{-1}$ - Because: $\alpha A \left(\frac{1}{\alpha} A^{-1}\right) = AA^{-1} = I$ - Recall the property of norm that $\|\alpha A\| = |\alpha| \|A\|$ Therefore: $$\text{cond}(\alpha A) = \|\alpha A\| \left\|\frac{1}{\alpha} A^{-1}\right\|$$ $$= |\alpha| \left|\frac{1}{\alpha}\right| \|A\| \|A^{-1}\| = \|A\| \|A^{-1}\| = \text{cond}(A)$$ >ถ้า matrix ถูกคูณด้วย scalar ค่า condition number ไม่เปลี่ยน >สะท้อนว่า **การ scaling input/output ไม่ทำให้ปัญหาเสถียรขึ้นหรือแย่ลง** ### Property 4: Diagonal Matrices **Claim:** If $D$ is a diagonal matrix, that is, $d_{ij} = 0$ for $i \neq j$, then: $$\boxed{\text{cond}_p(D) = \frac{\max_i |d_{ii}|}{\min_i |d_{ii}|}}$$ **Examples:** - The condition number in $p$-norm of $\begin{bmatrix} 2 & 0 \\ 0 & -3 \end{bmatrix}$ is $3/2$ - The condition number in $p$-norm of $\begin{bmatrix} 20 & 0 & 0 \\ 0 & -10 & 0 \\ 0 & 0 & 1 \end{bmatrix}$ is $20/1 = 20$ >สำหรับ diagonal matrix เราสามารถหาค่า condition number ได้ทันทีจาก ratio ของค่าสูงสุด/ต่ำสุดของ diagonal → ช่วยให้เห็น intuition: ถ้า diagonal entries ต่างกันมาก (เช่น มี $10^6$ กับ $10^{-6}$) → ill-conditioned แน่นอน ### Interpretation - ถ้า $\text{cond}(A) \approx 1$ (เข้าใกล้) → well-conditioned - ถ้า $\text{cond}(A)$ ใหญ่มาก → ill-conditioned ___ ## Stability >อีก Cause นึงที่ได้พูดไปว่าทำให้เกิด Inaccurate solution ก็คือ Algorithm นั่นเอง มาพูดถึงตรงนี้แล้วนะจ๊ะ เช่นเดียวกับการที่เรา *define Conditioning to talk about issue from the problem, let’s define Stability to talk about algorithm*, and how much error they produced ### Definition - An algorithm is **stable** if the result it produces is relatively insensitive to perturbations due to approximations made **during the computation** - In general, ==we can think of any problems as computing $f(x)$ for the input $x$== >- **หมายถึง:** Algorithm ไม่ทำให้ error บานปลายจากการคำนวณ (เช่นการบวก/ลบ/คูณที่มี floating-point rounding) >- **ตัวอย่าง:** สมมติเราจะคำนวณ $a+b+c$ > - ถ้าเราบวกจากซ้ายไปขวา $(a+b)+c$ อาจจะเสถียรกว่า $a+(b+c)$ ถ้าตัวเลขต่าง scale กันมาก ๆ > - Algorithm ที่ไม่ทำให้ error โตเกินจำเป็น → stable ### Forward Stability - A **forward stable** algorithm produces a result that is very close to the correct result (true solution) - That is, a forward stable algorithm outputs $f(x) + \epsilon_f$, where $\epsilon_f$ is small (relatively) - This $\epsilon_f$ is called the **forward error** - I.e., a forward stable algorithm produces small forward error ### Backward Stability - A **backward stable** algorithm produces the **exact** result for the nearby problem - That is, a backward stable algorithm outputs the exact $f(x + \epsilon_b)$, where $\epsilon_b$ is small (relatively) - This $\epsilon_b$ is called the **backward error** - I.e., a backward stable algorithm produces small backward error - In other words, **for a backward stable algorithm, the effect of perturbations during the computation is no worse than the effect of a small amount of data error in the input for the given problem** ### Practical Considerations - **Backward stability is more often used in practice** - A backward stable algorithm is also forward stable for well-conditioned problems - For most problems, analyzing the backward error is much easier - The forward error analysis often yields a very pessimistic bound for the error ### Mixed Stability - A **mixed forward-backward stable** (or mixed backward-forward stable) algorithm produces nearly the correct result for the nearby problem - That is, it outputs $f(x + \epsilon_b) + \epsilon_f$, where $\epsilon_b$ and $\epsilon_f$ are small (relatively) ### Conditional Stability - A **conditionally stable** algorithm is backward stable under some conditions but unstable otherwise ___ # Examples of Stable and Unstable Algorithms ## Algorithm: Compute $e^x$ from Taylor series ### The Taylor Series Formula $$\boxed{e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{3!} + \cdots}$$ ### Stability Analysis - This algorithm is **unstable** for $x \ll 0$ (much much smaller than zero) - For example, consider computing $e^{-300}$: $$e^{-300} = 1 - 300 + \frac{300^2}{2} - \frac{300^3}{6} + \frac{300^4}{24} - \cdots$$ - $e^{-300}$ is a very small positive number - However, **==many terms on the right-hand side are large==** - This results in [[4 - Approximations in Scientific Computing; Computer Arithmetic#Catastrophic Cancellation|Catastrophic Cancellation]] ### Stable Solution for Negative x - **How to compute $e^x$ stably for $x \ll 0$?** - Use Taylor series to compute $e^{-x}$ and then output $\frac{1}{e^{-x}}$ - For example, to compute $e^{-300}$: 1. First compute: $$e^{300} = 1 + 300 + \frac{300^2}{2} + \frac{300^3}{6} + \frac{300^4}{24} + \cdots$$ 2. Then output $\boxed{\frac{1}{e^{300}}}$ ## Algorithm: Finding Roots of Quadratic Equations ### Standard Quadratic Formula $$\boxed{x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}}$$ ### Instability Problem - The formula is **unstable** for one of the roots when $|ac| \ll |b|$ - In this case, $b$ and $\sqrt{b^2 - 4ac}$ are close in absolute values - **Catastrophic cancellation occurs:** - If $b > 0$: $-b + \sqrt{b^2 - 4ac}$ causes catastrophic cancellation - If $b < 0$: $-b - \sqrt{b^2 - 4ac}$ causes catastrophic cancellation ### Stable Solution for Quadratic Roots - **Key insight:** Use the relationship between roots - Recall that for $ax^2 + bx + c = 0$: - $(x - r_1)(x - r_2) = 0$ where $r_1$ and $r_2$ are the roots - Therefore: $\boxed{r_1 r_2 = \frac{c}{a}}$ and $\boxed{r_2 = \frac{c}{ar_1}}$ #### Stable Algorithm: 1. **Find the root with larger absolute value** using the quadratic formula (this doesn't cause cancellation): - If $b > 0$: compute $$\boxed{r_1 = \frac{-b - \sqrt{b^2 - 4ac}}{2a}}$$ - If $b < 0$: compute $$\boxed{r_1 = \frac{-b + \sqrt{b^2 - 4ac}}{2a}}$$ 2. **Find the other root** using: $$\boxed{r_2 = \frac{c}{ar_1}}$$ --- ## Stability of Gaussian Elimination (GE) ### Example: Plain GE Instability Consider performing plain GE on matrix: $$A = \begin{bmatrix} \epsilon & 1 \\ 1 & 1 \end{bmatrix}$$ where $\epsilon$ is a very small positive number. **Note:** The condition number of $A$ is small $\Rightarrow$ $A$ is well-conditioned. #### GE Process: 1. Subtract $(\frac{1}{\epsilon})$ of Row 1 from Row 2: $$A \sim \begin{bmatrix} \epsilon & 1 \\ 0 & 1 - \frac{1}{\epsilon} \end{bmatrix} = U$$ 2. The L matrix becomes: $$L = \begin{bmatrix} 1 & 0 \\ \frac{1}{\epsilon} & 1 \end{bmatrix}$$ 3. **Problem:** Since $\frac{1}{\epsilon} \gg 1$, the value $1 - \frac{1}{\epsilon}$ gets rounded to $-\frac{1}{\epsilon}$: $$\hat{U} \approx \begin{bmatrix} \epsilon & 1 \\ 0 & -\frac{1}{\epsilon} \end{bmatrix}$$ 4. **Result:** The computed $L\hat{U}$ doesn't equal the original matrix $A$: $$L\hat{U} = \begin{bmatrix} 1 & 0 \\ \frac{1}{\epsilon} & 1 \end{bmatrix} \begin{bmatrix} \epsilon & 1 \\ 0 & -\frac{1}{\epsilon} \end{bmatrix} = \begin{bmatrix} \epsilon & 1 \\ 1 & 0 \end{bmatrix} \neq A$$ #### Example Problem Solution: - **True solution** for $Ax = \begin{bmatrix} -1 \\ 1 \end{bmatrix}$: $$x = \begin{bmatrix} \frac{2}{1-\epsilon} \\ -1 \end{bmatrix}$$ - **Computed solution** (due to roundoff errors): $$\hat{x} = \begin{bmatrix} 1 \\ -1-\epsilon \end{bmatrix}$$ - **Conclusion:** Plain GE is **==unstable==** ### Gaussian Elimination with Partial Pivoting (GEPP) >**Much better!** #### Wilkinson's Theorem (Simplified) **Theorem:** Suppose we solve $Ax = b$, $A \in \mathbb{R}^{n \times n}$ using GEPP with roundoff errors. The computed $\hat{x}$ satisfies: $$\boxed{(A + E)\hat{x} = b}$$ where $E$ satisfies: $$\boxed{\frac{\|E\|_\infty}{\|A\|_\infty} \leq \rho n \epsilon_{mach} + O(\epsilon_{mach}^2)}$$ **Key Points:** - In the (extremely rare) worst case: $\rho = 2^{n-1}$ - In practice: $\rho \approx 2$ - **Conclusion:** GEPP is **backward stable** (in practice) - GECP (complete pivoting) yields even smaller $\rho$, but the improvement usually doesn't justify the extra computational cost --- ## Conditioning and Stability Relationship ### When Can We Expect Accurate Solutions? **Question:** In which case can we expect an accurate solution? - Using a stable algorithm on a well-conditioned problem? - Using an unstable algorithm on an ill-conditioned problem? - Using an unstable algorithm on a well-conditioned problem? - Using a stable algorithm on an ill-conditioned problem? **Answer:** $$\boxed{\text{Only when we use a stable algorithm on a well-conditioned problem}}$$ --- ## Exercise 3: Conditioning of tan(x) near π/2 >ควรแอบทำให้ได้! ### Problem Is the problem of computing $\tan x$ for $x$ near $\frac{\pi}{2}$ well-conditioned or ill-conditioned? ![[Pasted image 20250910212538.png|center]] ### Solution - For small $\epsilon_1 > 0$: $\tan(\frac{\pi}{2} - \epsilon_1)$ is a very large positive number - For small $\epsilon_2 > 0$: $\tan(\frac{\pi}{2} + \epsilon_2)$ is a very large negative number - A small perturbation from $\frac{\pi}{2} - \epsilon_1$ to $\frac{\pi}{2} + \epsilon_2$ creates a huge change in output (from very large positive to very large negative) - **Conclusion:** The problem is **ill-conditioned** **Additional Notes:** - Even small perturbations from $x < \frac{\pi}{2}$ to $\hat{x} < \frac{\pi}{2}$ cause large changes - Similarly for perturbations from $x > \frac{\pi}{2}$ to $\hat{x} > \frac{\pi}{2}$ --- ## Exercise 4: Numerical Cancellation in Expressions ### Problem Consider the expression: $$\frac{1}{1-x} - \frac{1}{1+x}$$ (assuming $x \neq \pm 1$) #### Part 1: Problematic Range of x **Ranges where computation is difficult:** 1. **For x close to 0:** - $\frac{1}{1-x}$ and $\frac{1}{1+x}$ are close in magnitude - Subtraction causes **catastrophic cancellation** 2. **For x close to -1:** - Cancellation occurs in computing $1 + x$ - Computed $1 + x$ has large relative error - This propagates to large relative error in the final expression 3. **For x close to 1:** - Cancellation occurs in computing $1 - x$ - Similar propagation of errors as the $x \approx -1$ case #### Part 2: Stable Rearrangement **For x close to 0**, use the algebraically equivalent form: $$\boxed{\frac{1}{1-x} - \frac{1}{1+x} = \frac{(1+x) - (1-x)}{(1-x)(1+x)} = \frac{2x}{(1-x)(1+x)}}$$ - This rearrangement **avoids catastrophic cancellation** - More accurate for computation when $x \approx 0$ **Important Note:** - For $x$ close to $\pm 1$, no rearrangement can improve accuracy - This is because **the problem itself is ill-conditioned** for these ranges of $x$