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.

Practice analyzing errors, stability, and conditioning.

Self-Assessment Questions

Test your understanding with these conceptual questions:

  1. Machine Epsilon: What happens when you add a number smaller than εmach\varepsilon_{\text{mach}} to 1.0?

  2. Conditioning: What makes evaluating tan(x)\tan(x) near x=π/2x = \pi/2 ill-conditioned? Can any algorithm fix this?

  3. Stability: Why is computing x+1x\sqrt{x+1} - \sqrt{x} directly unstable for large xx? What’s a stable alternative?

  4. Machine Epsilon: What is the approximate machine epsilon for single and double precision? What does it tell us about the accuracy of floating point computations?

  5. Binary Representation: How do you convert a positive integer to its negative two’s complement representation?

  6. Subtractive Cancellation: When subtracting two numbers aa and bb, under what conditions is the result reliable?

  7. Forward vs. Backward: If an algorithm has small backward error but large forward error, what does this tell you about the problem?

  8. Trade-offs: In finite differences, why can’t we just use an extremely small hh to get arbitrary accuracy?

Floating-Point Arithmetic


Q2.1: Machine Epsilon


Q2.2: Representation Errors


Q2.3: Summation Order

Consider summing s=k=1n1ks = \sum_{k=1}^{n} \frac{1}{k} for n=107n = 10^7.


Q2.4: Kahan Summation

Kahan (1965) proposed a compensated summation algorithm that tracks roundoff error:

def kahan_sum(xs):
    s = 0.0  # Running sum
    c = 0.0  # Compensation for lost low-order bits

    for x in xs:
        y = x - c        # Compensate for previous error
        t = s + y        # Add to sum
        c = (t - s) - y  # Recover the roundoff error
        s = t

    return s

Forward and Backward Error


Q2.5: Quadratic Formula

For ax2+bx+c=0ax^2 + bx + c = 0 with a=1a = 1, b=108b = -10^8, c=1c = 1:


Q2.6: Exponential Minus One


Q2.7: Variance Computation

Consider computing variance: σ2=1n(xixˉ)2\sigma^2 = \frac{1}{n}\sum(x_i - \bar{x})^2.

An alternative “one-pass” formula is: σ2=1nxi2xˉ2\sigma^2 = \frac{1}{n}\sum x_i^2 - \bar{x}^2.


Condition Numbers


Q2.8: Condition Number Computation

Compute the condition number κ=xf(x)/f(x)\kappa = |xf'(x)/f(x)| for:

Which evaluations are well-conditioned? Which are ill-conditioned?

Solution for (a)

For f(x)=exf(x) = e^x, we have f(x)=exf'(x) = e^x.

K=xf(x)f(x)=xexex=xK = \left|\frac{x f'(x)}{f(x)}\right| = \left|\frac{x e^x}{e^x}\right| = |x|

At x=1x = 1: K=1K = 1. This is well-conditioned.


Q2.9: Subtractive Cancellation

The expression x+1x\sqrt{x+1} - \sqrt{x} suffers from cancellation for large xx.


Q2.10: Trigonometric Reformulation

For small xx, the expression 1cos(x)1 - \cos(x) loses accuracy.


Stability Analysis


Q2.11: Stable Reformulation

For each of the following expressions, identify the potential numerical issue and propose a stable reformulation:

Solution for (a)

Problem: Subtractive cancellation when x2+1x\sqrt{x^2+1} \approx x.

Stable form: Multiply by x2+1+xx2+1+x\frac{\sqrt{x^2+1}+x}{\sqrt{x^2+1}+x}:

x2+1x=(x2+1)x2x2+1+x=1x2+1+x\sqrt{x^2+1} - x = \frac{(x^2+1) - x^2}{\sqrt{x^2+1}+x} = \frac{1}{\sqrt{x^2+1}+x}

Q2.12: Residuals and Forward Error

Solve the system:

(1111.0001)(x1x2)=(22)\begin{pmatrix} 1 & 1 \\ 1 & 1.0001 \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} = \begin{pmatrix} 2 \\ 2 \end{pmatrix}

Computational Exercises


Q2.13: Unstable Recursion

Consider evaluating the integrals

yn=01xn10+xdxy_n = \int_0^1 \frac{x^n}{10 + x}\, dx

for n=1,2,,30n = 1, 2, \dots, 30.

Hint for (e)

Use the backward recursion yn=110(1n+1yn+1)y_n = \frac{1}{10}\left(\frac{1}{n+1} - y_{n+1}\right) assuming y=0y_\infty = 0. Repeatedly substitute to find the pattern.

Hint for (g)

Include a small round-off error at each step. What happens to this error as you iterate backward?

Solution for (a)

Using integration by parts or direct manipulation:

yn+10yn1=01xn+10xn110+xdx=01xn1(x+10)10+xdx=01xn1dx=1ny_n + 10y_{n-1} = \int_0^1 \frac{x^n + 10x^{n-1}}{10+x}\,dx = \int_0^1 \frac{x^{n-1}(x+10)}{10+x}\,dx = \int_0^1 x^{n-1}\,dx = \frac{1}{n}

Q2.14: Trapezoidal Rule with Round-off

Consider computing the integral:

I=12kdxx=2(2k/21)I = \int_1^{2^k} \frac{dx}{\sqrt{x}} = 2(2^{k/2} - 1)

using the trapezoidal rule with k=22k = 22. The approximation is:

IΔx(i=1N1f(xi)+f(x0)+f(xN)2)I \approx \Delta x \left(\sum_{i=1}^{N-1} f(x_i) + \frac{f(x_0) + f(x_N)}{2}\right)

where f(x)=x1/2f(x) = x^{-1/2} and Δx=2k1N\Delta x = \frac{2^k - 1}{N}.

Hint

To force single precision in NumPy:

s = np.asarray(0.0, dtype=np.float32)

Q2.15: Fast Inverse Square Root Accuracy

Refer to the fast inverse square root code.