Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

11) Rootfinding

Last time

  • More on floating point

  • Discuss Taylor Series activity

  • Condition numbers

Today

  1. Intro to forward and backward error

  2. Computing volume of a polygon

  3. Rootfinding

  4. Bisection Method

What we’re hoping for is for our problems (functions) and algorithms to be reliable (i.e., we can trust the result that our computer spits out). Reliable = well-conditioned and stable. But unfortunately

Mathematical functions f(x)f(x) can be ill-conditioned: (big κ\kappa)

  • Modeling is how we turn an abstract question into a mathematical function

  • We want well-conditioned models (small κ\kappa)

  • Some systems are intrinsically sensitive: fracture, chaotic systems, combustion

Algorithms f(x) can be unstable

  • Unreliable, though sometimes practical

  • Unstable algorithms are constructed from ill-conditioned parts

1. Introduction to forward and backward error

  • Backward error measures what change to the original data would reproduce the result found by an algorithm.

  • Let y~=f~(x)\tilde{y} = \tilde{f}(x) be a computed result for the original data xx. If there is a value x~\tilde{x} such that

f(x~)=y~=f~(x),f(\tilde{x})=\tilde{y} =\tilde{f}(x),

then we call ∣x~−x∣/∣x∣|\tilde{x} −x|/|x| the (relative) backward error of the result.

  • Instead of asking, “How close to the true answer is your answer?”, backward error asks, “How close to the true question is the question you answered?”

  • For ill-conditioned functions, the best we can hope for is small backward error.

  • If an algorithm always produces small backward errors, then it is stable. But the converse is not always true: some stable algorithms may produce a large backward error.

Exercise 11.1

  • Find a function f(x)f(x) that models something you’re interested in

  • Plot its condition number κ\kappa as a function of xx

  • Plot the relative error (using single or double precision; compare using Julia’s big)

  • Is the relative error ever much bigger than κϵmachine\kappa \epsilon_{\text{machine}}?

  • If so, can you find what caused the instability in your algorithm?

Further reading: FNC Introduction

2. Stability demo: Volume of a polygon

  • Activity: run the following code and comment on the result

Loading...
ref_vol = pvolume(X) = 9.25

rotated_vol = pvolume(Y) = 9.25
9.25
  • What happens if this polygon is translated, perhaps far away? (We’ll see more next time)

3. Rootfinding

Given f(x)f(x), find x⋆x_{\star} such that f(x⋆)=0f(x_{\star}) = 0.

We’ll work with scalars (ff and xx are just numbers) for now, but keep in mind that they can be vector-valued.

Exercise 11.2

Solve for xx

  • f(x;b)=x2−bf(x; b) = x^2 - b

    • x(b)=±bx(b) = \pm \sqrt{b}

  • f(x;b)=tan⁡x−bf(x; b) = \tan x - b

    • x(b)=arctan⁡bx(b) = \arctan b

  • f(x)=cos⁡x+x−bf(x) = \cos x + x - b

    • x(b)=?x(b) = ?

We aren’t given f(x)f(x), but rather an algorithm f(x) that approximates it.

  • Sometimes we get extra information, like fprime(x) that approximates f′(x)f'(x)

  • If we have source code for f(x), maybe it can be transformed “automatically” to obtain fprime(x)

Example: Queueing

In a simple queueing model, there is an arrival rate and a departure (serving) rate. While waiting in the queue, there is a probability of “dropping out”. The length of the queue in this model is

length=arrival−dropdeparture\text{length} = \frac{\text{arrival} - \text{drop}}{\text{departure}}

One model for the waiting time (where these rates are taken from exponential distributions) is

Loading...

Departure rate given wait

  • Easy to measure wait

  • I have a limited tolerance for waiting

Loading...
     Project No packages added to or removed from `~/.julia/environments/v1.13/Project.toml`
    Manifest No packages added to or removed from `~/.julia/environments/v1.13/Manifest.toml`
Loading...

4. Bisection Method

Bisection is a rootfinding technique that starts with an interval [a,b][a,b] containing a root and does not require derivatives. Suppose ff is continuous. What is a sufficient condition for ff to have a root on [a,b][a,b]?

Hint: Intermediate value theorem and its corollary, Bolzano’s theorem.

Loading...

Bisection Algorithm: a recursive algorithm

0.7390861511230469
-1.7035832658995886e-6

How fast does it converge?

Let’s look at how many iterations it took to find the root.

bisect_hist (generic function with 1 method)
17-element Vector{Float64}: 1.0 0.0 0.5 0.75 0.625 0.6875 0.71875 0.734375 0.7421875 0.73828125 0.740234375 0.7392578125 0.73876953125 0.739013671875 0.7391357421875 0.73907470703125 0.739105224609375

Iterative bisection: an optimized algorithm

  • Data structures are often optimized for appending rather than prepending.

  • Saves stack space

bisect_iter (generic function with 1 method)
16-element Vector{Float64}: 1.0 0.0 0.5 0.75 0.625 0.6875 0.71875 0.734375 0.7421875 0.73828125 0.740234375 0.7392578125 0.73876953125 0.739013671875 0.7391357421875 0.73907470703125

Let’s plot the error

∣bisectk(f,a,b)−r∣,k=1,2,…\lvert \texttt{bisect}^k(f, a, b) - r \rvert, \quad k = 1, 2, \dotsc

where rr is the true root, f(r)=0f(r) = 0.

Loading...

Evidently the error ek=xk−x∗e_k = x_k - x_* after kk bisection iterations satisfies the bound

∣ek∣≤c2−k.|e^k| \le c 2^{-k} .
  • The absolute error is halved at each step so the method converges linearly. But is this all there is to it? We’ll see more next time.

Exercise 11.3

  1. Find an interval of length 1 with integer endpoints on which a solution to x3=9x^3=9 lies. Then use the bisection code starting with this interval to find the solution within a 10-4 tolerance.

  2. Find an interval of length 1 with integer endpoints on which a solution to 6+cos⁡2x=x6 + \cos^2{x} = x lies. Then use the bisection code starting with this interval to find the solution within a 10-4 tolerance.

  3. Suppose we want to know what interest rate we would need to achieve (assuming constant interest rates) such that the initial deposit of $50,000\$50,000 and annual contribution of $10,000\$10,000 will grow to $1,000,000\$1,000,000 in 20 years. Thus we need to solve 1000000=(50000+10000r)e20r−10000r1000000 = \left( 50000 + \frac{10000}{r} \right) e^{20r} - \frac{10000}{r} for the interest rate, rr. Find a reasonable interval on which the solution ought to lie and use the bisection code to find rr to within 10-6.

  4. Find appropriate intervals and use the bisection code to find ALL the root(s) of ∣x∣ex=0.25|x|e^{x}= 0.25.

  5. Use the bisection code to find the root of 27x3−27x2+9x=127 x^3 - 27 x^2 + 9x = 1 on [0,1][0, 1] to within 10-6.

  6. Use the bisection code to find the root of 64x3−48x2+12x=164 x^3 - 48x^2 + 12x = 1 on [0,1][0, 1] to within 10-6.