ϵ = eps( Float64 )
(1.0 + ϵ/2) - 1.0 == 1.0 + (ϵ/2 - 1.0)false
Lecture 3: Conditioning and stability and the start of Chapter 2: Solving nonlinear equations in 1d
These lecture notes are mainly a record of what we discussed in class. They should not be used as a replacement for attending lectures or reading books. Please let me know if you find any errors or have any questions about the material.
You can use the Hypothesis commenting system available on the right side panel (you can login with your U/Google account). This allows you annotate the notes (private) and leave comments or reply to other people’s comments (public).
I encourage you to download the lecture notes and play around with the Julia code.
This lecture is based on Chapter 1 of [Fundamentals of Numerical Computation by Driscoll, Braun] which is avaliable online (opens in a new tab)
ChimeIn: floating point numbers, round off error, …
Let \(\epsilon\) be the machine precision. Is \((1 + \epsilon/2) - 1 = 1 + (\epsilon/2-1)\) in Float64?
Let \(X\) be an array containing \(10^{16}, -10^{16}\), and \(N\) copies of \(1\). What is the sum of all the entries of the array \(X\)? Does it depend on the order of summation? What about in Float64? Why?
When adding or subtracting numbers so that the result is much smaller in magnitude. For example, we know \(x = -1.0000000000000000000147\) to \(23\) significant digits but \(x + 1 = -1.47 \times 10^{-20}\) to only \(3\) significant digits.
This concept can be illustrated using (relative) condition number:
Suppose we are interested in a quantity \(f(x)\) for \(x\in \mathbb R\). In practice, we can only store \(\widetilde{x} := \mathrm{fl}(x)\) in a computer (recall that \(\widetilde{x} = x(1 + \epsilon)\) for some \(|\epsilon|\leq \frac12 \epsilon_{\mathrm{mach}}\)). Then the ratio of the relative error in the output to the relative error in the input in the limit as the precision goes to zero is known as the (relative) condition number \(\kappa_f(x)\):
Definition 3.1 (Relative condition number) \[\begin{align} \kappa_f(x) := \lim_{\epsilon\to0}\left| \frac {f(x) - f(x + \epsilon x)} {\epsilon f(x)} \right| \nonumber % \end{align}\]
That is, the (relative) condition number is approximately the constant of proportionality between the relative error in the output and the relative error of the input:
\[\begin{align} \left|\frac {f(x) - f(\widetilde{x})} {f(x)} \right| \approx \kappa_f(x) \left|\frac {x - \widetilde{x}} {x} \right| % = \kappa_f(x) |\epsilon|. \nonumber \end{align}\]
Example 3.1 The condition number of \(f(x) = x + 1\) is \(\kappa_f(x) = \left|\frac{x}{x+1}\right|\). This blows up when \(x\approx -1\) which explains subtractive cancellation that we mentioned above.
Definition 3.2 If the condition number is large, we say the problem is ill-conditioned or the problem is badly conditioned
Notice that
\[\begin{align} \kappa_f(x) &= \lim_{\epsilon\to0}\left| \frac {f(x) - f(x + \epsilon x)} {\epsilon f(x)} \right| \nonumber\\ % &= \lim_{\epsilon\to0}\left| \frac {f(x) - f(x + \epsilon x)} {\epsilon x} \right| \left| \frac{x}{f(x)} \right| \nonumber\\ % &= \left| \frac{x f'(x)}{f(x)} \right| \end{align}\]
Exercise
Example 3.2 (Polynomial roots as a function of monomial basis coefficients) The polynomial
\[\begin{align} a \xi^2 + b \xi + c = 0 \end{align}\]
has roots \(\xi_1,\xi_2\). Fix \(b,c\) and consider \(\xi_1 = \xi_1(a)\). What is \(\kappa_{\xi_1}(a)\)?
For higher-order polynomials, representing functions in the standard monomial basis (\(\{1, x, x^2, \dots\}\)) is usually a bad idea. Monomial bases are ill-conditioned; tiny perturbations in the coefficients can drastically change the behaviour of the polynomial and shift the roots significantly. To maintain numerical stability, it’s a better idea to use to an orthogonal basis like Chebyshev polynomials (see later in the semester).
Potential presentation topics include: Wilkinson’s Polynomial, conditioning of Vandermonde matrices, understand and explain this, this, ….
Poor conditioning is a problem with the problem! On the other hand, if we see a large error in the result, the problem might be with the solution! That is, the algorithm can also introduce errors. When the error can not be explained by an ill-conditioned problem, we will say the algorithm is unstable.
Consider \(\xi^2 - (10^n + 10^{-n})\xi + 1 = 0\). The roots of this equation are \(\xi_1 := 10^{-n}\) and \(\xi_2 := 10^n\). These roots are well separated so we expect the problem of finding the roots to be well-conditioned. We show below that the quadratic formula is unstable.
The quadratic formula in floating point arithmetic gives:
n = 8;
a, b, c = 1, -(10.0^n + 10.0^(-n)), 1;
ξ1, ξ2 = ( -b - sqrt( b^2 - 4*a*c) )/(2*a), ( -b + sqrt( b^2 - 4*a*c) )/(2*a);
@show ( ξ1, ξ2 ) ;(ξ1, ξ2) = (1.4901161193847656e-8, 1.0e8)
The error in approximating the first root is quite big!
println( "relative errors = (", abs( ξ1 - 10.0^(-n) )/10.0^(-n) , ", ", abs( ξ2 - 10.0^n )/10.0^n, ")" )relative errors = (0.49011611938476557, 0.0)
But why?
-b1.0000000000000001e8
sqrt( b^2 - 4 * a * c )9.999999999999999e7
-b - sqrt( b^2 - 4*a*c)2.9802322387695312e-8
The error is coming from Subtractive Cancellation! The quadratic formula is thus ill-conditioned. Alternatively we could compute \(\xi_1\) from \(\xi_2\):
ξ1 = (c/a)/ξ21.0e-8
Which is exact in Float64.
Another way of computing \(\xi_1\) is to rationalise the numerator:
\[\begin{align} \xi_1 &= \frac{ -b - \sqrt{ b^2 - 4ac } }{2a} \\ % &= \frac{ -b - \sqrt{ b^2 - 4ac } }{2a} \times \frac{ -b + \sqrt{ b^2 - 4ac } }{-b + \sqrt{ b^2 - 4ac } } \nonumber\\ % &= \frac { b^2 - ( b^2 - 4ac ) } {2a( -b + \sqrt{ b^2 - 4ac } )} \\ % &= \frac { 2c } { -b + \sqrt{ b^2 - 4ac } } \end{align}\]
Using this formula, we get
ξ1 = 2*c / ( -b + sqrt(b^2 - 4*a*c) )1.0e-8
Which is again exact in Float64
We then looked at questions 1.4.1 and 1.4.2 from [Fundamentals of Numerical Computation by Driscoll, Braun]
Exercise 1.4.1 from Driscoll and Braun (2017), [Fundamentals of Numerical Computation by Driscoll, Braun]
Exercise 1.4.2 from Driscoll and Braun (2017), [Fundamentals of Numerical Computation by Driscoll, Braun]
This chapter will be mainly based on Chapter 1 of Süli and Mayers (2003).
Definition 3.3 We say \(\xi\) is a root (or a zero) of \(f: [a,b] \to \mathbb R\) if \(f(\xi) = 0\).
p = Polynomial([-2, -4, 0, 0, 0, 1])
@show p;
plot(p, legend=false)
plot!( real( filter( isreal, roots(p) ) ), zeros(3), seriestype=:scatter )p = Polynomial(-2 - 4*x + x^5)
Theorem 3.1 (Intermediate value theorem) Suppose that \(f : [a,b] \to \mathbb R\) is continuous and \(f(a) < y < f(b)\) or \(f(b) < y < f(a)\). Then, there exists a \(c\in [a,b]\) such that \(f(c) = y\).
This result, implies the following useful theorem for determining whether a function has a root:
Theorem 3.2 (Change of sign) If \(f\) is continuous on \([a,b]\) and \(f(a) f(b) \leq 0\) then there exists \(\xi \in [a,b]\) with \(f(\xi) = 0\).
Proof. If \(f(a) f(b) = 0\) then either \(a\) or \(b\) is a root. We can therefore suppose that \(f(a) f(b) < 0\) and so either \(f(a) < 0 < f(b)\) or \(f(b) < 0 < f(a)\). Since \(f\) is continuous, we can apply the intermediate value theorem on \(f\) to conclude there exists a root \(c \in [a,b]\).
The converse to this theorem is untrue. i.e. there exists \(f\) with a real root in \([a,b]\) but with \(f(a)f(b) > 0\).
Let \(f(x) = e^x - 2x - 1\). Apply change of sign theorem to \(f\) to conclude there exists \(\xi \in [1,2]\) such that \(f(\xi) = 0\).
f = x -> exp(x) - 2*x - 1;
@show ( f(1), f(2) )
plot( f, 1, 2; label = L"y = f(x)" )
plot!( x->0; label = L"y = 0", linestyle=:dot )(f(1), f(2)) = (-0.2817181715409549, 2.3890560989306504)
Definition 3.4 We say \(\xi\) is a fixed point of \(g: [a,b] \to \mathbb R\) if \(g(\xi) = \xi\).
The change of sign theorem can be applied here:
Theorem 3.3 (Brouwer’s fixed point theorem) Suppose \(g : [a,b]\to\mathbb R\) is continuous with \(g(x) \in [a,b]\) for all \(x \in [a,b]\). Then, there exists \(\xi\in [a,b]\) with \(g(\xi) = \xi\).
Proof. We can apply the change of sign theorem to \(f(x) := g(x) - x\). Indeed, because \(g(a), g(b) \in [a,b]\), we have \(f(a) = g(a) - a \geq 0\) and \(f(b) = g(b) - b \leq 0\) and thus \(f\) is continuous with \(f(a) f(b) \leq 0\). All roots \(\xi\in[a,b]\) of \(f\) are fixed points of \(g\).
Let’s return to Exercise 3.5. You showed that \(f(x) = e^x - 2x - 1\) has a root \(\xi \in [1,2]\).
Show that \(f(x) = 0\) can be solved by instead considering the fixed point problems \(g(x) = x\) or \(h(x) = x\) where
\[ \begin{align} g(x) &= \log( 2x + 1 ), \\ h(x) &= \frac{e^x - 1}{2}. \end{align} \tag{3.1}\]
Show that Brouwer’s fixed point theorem applies to \(g\) but not \(h\) on \([1,2]\).
g = x -> log( 2*x + 1 );
h = x -> (exp(x) - 1)/2;
id = x -> x;
@show (g(1), g(2));
@show (h(1), h(2));
plot( [g, h, id], 1, 2, label=[L"g" L"h" L"x"])
hline!( [1, 2]; linestyle=:dot, color=:black, primary=false )(g(1), g(2)) = (1.0986122886681098, 1.6094379124341003)
(h(1), h(2)) = (0.8591409142295225, 3.194528049465325)
Exercise 3.7 (Lambert W function)
Assignment 1, Fall 2025, [30 pts]. Prove that, for \(x \geq 0\), there exists a unique \(W(x) \geq 0\) for which \(W(x) e^{W(x)} = x\). Show that \(\Omega := W(1)\) satisfies \(0.56 < \Omega < 0.58\).
Assignment 2, Fall 2025, [10 pts]. For fixed \(f\), define
\[\begin{align} g(x) &= f(x - a), \\ h(x) &= b f(x), \\ j(x) &= f(c x), \end{align}\]
Assignment 2, Fall 2025, [25 pts]. Consider the differential equation (initial value problem)
\[ \begin{align} \begin{split} u'(x) &= u(x) +e^{-x} \nonumber \\ u(0) &= -\frac12 . \end{split} \end{align} \tag{3.2}\]
Now we perturb the initial data, by considering
\[ \begin{align} \begin{split} u'(x) &= u(x) +e^{-x} \nonumber \\ u(0) &= \alpha \end{split} \end{align} \tag{3.3}\]
for \(\alpha := \epsilon - \frac12\).