Big-Oh Notation for Small Variables
Example
- Consider
- When :
- What if ?
- In this case:
- So
General Rules
- When :
- When :
- Convention: 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 (Ill-conditioned case)
The problem: Compute
-
Input:
-
Exact answer:
-
What if input changes to ?
-
Solution becomes:
-
Relative change of input:
-
Relative change of solution:
-
Conclusion: Ill-conditioned because small relative change to the data leads to large relative change to the solution
เปลี่ยน input จาก → ซึ่งเป็นแค่การเปลี่ยนไป ของค่าเดิม
แต่ผลลัพธ์กระโดดจาก → เลย → Ill-conditioned
Example 2: Computing (Well-conditioned case)
- Input:
- is well-conditioned
- Any small changes to the input yields small change in output
Analysis:
จะเห็นได้ว่าจาก 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
Case 1: Ill-conditioned
-
Roots of (i.e., , , )
-
Exact root(s):
-
Suppose we perturb to where is very small:
-
Roots of perturbed equation:
-
Relative change of input:
-
Relative change of solution:
-
Conclusion: Ill-conditioned because if
-
Large change in solution compared to change in input
จาก ได้ root = 1
ถ้าเปลี่ยน จาก → นิดเดียว แต่คำตอบ root กระโดดเป็น
เช่น คำตอบเปลี่ยนระดับ เลย → Ill-conditioned
Case 2: Well-conditioned
- Finding the roots of is well-conditioned
END OF WEEK 3
Example 4: Solving Linear Systems
Case 1: Well-conditioned
- Solving 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 where is very small
- This is ill-conditioned
Numerical Example:
- Solution of is
- Solution of is
ดูสิเปลี่ยน Input ไปนิดเดียว คือ คำตอบเพิ่มขึ้น 10 เท่า กูงงเลยล่ะ
Analysis:
- Relative change in input:
- Relative change in solution:
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 , is the true input, and is the perturbed input, then:
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 to if and are perturbed?
Setup
- Suppose the true problem (with true inputs) is:
- Perturb to and to , where and are "small"
- The perturbed problem is therefore:
Small Perturbation Analysis
-
We consider small perturbation. That is: (LHS, and RHS เลยนะข้าง่างอะ)
-
where is small (say, )
-
Above are the same as: (คืออันเดียวกันข้างบน แค่ย้ายไปอีกฝั่ง)
-
Assume -norm is used for vectors and matrices
Derivation
Starting from the perturbed equation:
But . So:
Using triangular inequality:
(using subordination and submultiplicativity ก็คือใช้ 3 - Norms)
Continuing the Analysis
From (1) and (2):
Note that:
From above:
Let denote .
Rewrite as:
Then:
Final Result
But . So:
From the previous slide:
Therefore:
- ผลลัพธ์สุดท้าย บอกเราว่า relative error ของคำตอบ (solution) ถูกควบคุมโดย (ขนาดของ input error) และ (condition number)
- ใจความสำคัญ
- ถ้า เล็ก → well-conditioned
- ถ้า ใหญ่ → ill-conditioned
Conclusion
- When is small, the right-hand side is an increasing function with respect to
- The relative change of the solution of a linear system depends on (and the relative change of the inputs )
- The larger is, the larger the relative change of the solution
Matrix Condition Number
Definition
-
The condition number of a nonsingular square matrix is defined to be:
-
If is singular, say that
-
Sometimes written as
-
The larger is, the more ill-conditioned the linear system is
Example 1
Compute the condition numbers in 1-norm and -norm of:
Solution:
- อันนี้เป็น 2x2 หา Inverse ก็ง่าย ๆ เลยอะ ใช้สูตรได้เลยจาก 05 Inverse Matrix หรือถ้ามันอย่างกว่านั้นก็ใช้ GEPP เด้อ
So:
-
- ก็คืออิงจากการหา ได้จาก 3 - Norms เลยนะ
Exercise 2
Compute the condition numbers in 1-norm and -norm of:
Solution:
- เช่นเคยมันก็มีสูตรการหา อะนะ แต่ว่าเราจะใช้ GEPP ก็ได้ too!
So:
แค่นี้เราก็ได้คำตอบแล้วว่า 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:
Proof:
But:
Therefore:
บอกเราว่าไม่มีระบบไหน “ดีกว่า identity” ได้ ค่าต่ำสุดคือ 1 เท่านั้น
Property 2: Identity Matrix
Claim:
Proof:
- Recall
- So:
- ( 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$