Level 3 — ProductionNumerical Methods

Numerical Methods

45 minutes60 marksprintable — key stays hidden on paper

Level: 3 (Production — from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60

Instructions: Show all derivations from first principles. Where "pseudocode from memory" is requested, write clean, runnable-style pseudocode. Explain reasoning where prompted.


Question 1 — Newton–Raphson from scratch (10 marks)

(a) Starting from a Taylor expansion of ff about the current iterate xnx_n, derive the Newton–Raphson update formula. (3)

(b) Let en=xnre_n = x_n - r where rr is a simple root. Prove that the method is quadratically convergent, giving the asymptotic error constant in terms of f(r)f''(r) and f(r)f'(r). (5)

(c) State one condition under which quadratic convergence is lost, and what convergence order results. (2)


Question 2 — Catastrophic cancellation (10 marks)

(a) Define catastrophic cancellation and explain, in terms of relative error and machine epsilon, why subtracting two nearly-equal numbers is dangerous. (3)

(b) The roots of x22bx+c=0x^2 - 2bx + c = 0 (with b>0b>0, b2cb^2 \gg c) are computed via x=bb2cx = b - \sqrt{b^2 - c} for the smaller root. Explain why this loses accuracy and give an algebraically equivalent, numerically stable rewrite. (4)

(c) For b=108b = 10^{8}, c=1c = 1, estimate roughly how many significant decimal digits are lost in the naive formula using IEEE 754 double precision (16\approx 16 digits). (3)


Question 3 — Composite Simpson's 1/3 rule (12 marks)

(a) Derive Simpson's 1/3 rule on [x0,x2][x_0, x_2] by integrating the quadratic interpolating polynomial through (x0,f0),(x1,f1),(x2,f2)(x_0,f_0),(x_1,f_1),(x_2,f_2) with equal spacing hh. (5)

(b) Write the composite Simpson's 1/3 rule formula for nn subintervals (nn even) and state the requirement on nn. (3)

(c) Apply composite Simpson's 1/3 with n=4n=4 to approximate 01ex2dx\int_0^1 e^{-x^2}\,dx. Give the answer to 5 decimal places. (4)


Question 4 — Iterative solver, code from memory (10 marks)

(a) Write pseudocode from memory for the Gauss–Seidel iteration to solve Ax=bA\mathbf{x}=\mathbf{b}, including a stopping criterion. (5)

(b) State the diagonal-dominance sufficient condition for convergence and explain in one line why Gauss–Seidel typically converges faster than Jacobi. (3)

(c) For the system 4x1x2=3,x1+4x2=6,4x_1 - x_2 = 3,\qquad -x_1 + 4x_2 = 6, perform one Gauss–Seidel sweep starting from (x1,x2)=(0,0)(x_1,x_2)=(0,0). (2)


Question 5 — RK4 and error orders (10 marks)

(a) Write the classical RK4 stage equations and update for y=f(t,y)y' = f(t,y), step hh. (4)

(b) State the local truncation error order and global error order of RK4, and contrast with Euler's method. (3)

(c) Given y=yy' = y, y(0)=1y(0)=1, take one RK4 step with h=0.5h=0.5 to estimate y(0.5)y(0.5). Give 5 decimal places and the absolute error against e0.5e^{0.5}. (3)


Question 6 — Condition number & explain-out-loud (8 marks)

(a) Derive the relative condition number of evaluating a differentiable function ff at xx, expressing it as κ=xf(x)f(x)\kappa = \left|\dfrac{x f'(x)}{f(x)}\right|. (4)

(b) Explain out loud (in words) the difference between an ill-conditioned problem and an unstable algorithm, and why a stable algorithm cannot rescue an ill-conditioned problem. (4)

Answer keyMark scheme & solutions

Question 1 (10)

(a) Taylor about xnx_n: f(r)=f(xn)+f(xn)(rxn)+O((rxn)2)f(r) = f(x_n) + f'(x_n)(r - x_n) + O((r-x_n)^2). Set f(r)=0f(r)=0, drop quadratic term, solve for rxn+1r \approx x_{n+1}: xn+1=xnf(xn)f(xn).x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}. (1 for expansion, 1 for setting f(r)=0f(r)=0 & truncating, 1 for formula.)

(b) Write xn+1r=en+1x_{n+1} - r = e_{n+1}. Expand f(r)f(r) and ff' around xnx_n: 0=f(xn)enf(xn)+12en2f(xn)+O(en3).0 = f(x_n) - e_n f'(x_n) + \tfrac{1}{2}e_n^2 f''(x_n) + O(e_n^3). Since xn+1=xnf(xn)/f(xn)x_{n+1} = x_n - f(x_n)/f'(x_n), en+1=enf(xn)f(xn).e_{n+1} = e_n - \frac{f(x_n)}{f'(x_n)}. Substitute f(xn)=enf(xn)12en2f(xn)+f(x_n) = e_n f'(x_n) - \tfrac12 e_n^2 f''(x_n) + \dots (from expanding f(r)=0f(r)=0 rearranged) to get en+1=f(xn)2f(xn)en2+O(en3)    en+1f(r)2f(r)en2.e_{n+1} = \frac{f''(x_n)}{2 f'(x_n)} e_n^2 + O(e_n^3) \;\Rightarrow\; e_{n+1} \approx \frac{f''(r)}{2f'(r)} e_n^2. Asymptotic constant C=f(r)2f(r)C = \left|\dfrac{f''(r)}{2 f'(r)}\right|; order p=2p=2. (2 for expansion setup, 2 for substitution/cancellation of ene_n term, 1 for constant & stating quadratic.)

(c) If f(r)=0f'(r)=0 (multiple root) quadratic convergence is lost; convergence becomes linear (order 1). (1+1)


Question 2 (10)

(a) Catastrophic cancellation: subtracting two nearly equal floating-point numbers. Each operand already carries relative rounding error εmach\sim \varepsilon_{mach} (absolute error εx\sim \varepsilon |x|). Their difference is small but the absolute errors persist, so the relative error of the difference is magnified by roughly xxy\frac{|x|}{|x-y|}, destroying significant digits. (1 def, 2 explanation.)

(b) x=bb2cx = b - \sqrt{b^2 - c}: when b2cb^2 \gg c, b2cb\sqrt{b^2-c} \approx b, so we subtract nearly-equal quantities → cancellation. Multiply by conjugate: x=(bb2c)(b+b2c)b+b2c=cb+b2c,x = \frac{(b-\sqrt{b^2-c})(b+\sqrt{b^2-c})}{b+\sqrt{b^2-c}} = \frac{c}{\,b + \sqrt{b^2 - c}\,}, an addition — no cancellation. (2 identify problem, 2 stable form.)

(c) b2cb1c/b2b(1c/(2b2))\sqrt{b^2-c} \approx b\sqrt{1 - c/b^2} \approx b(1 - c/(2b^2)). Cancellation removes log10(b2/c)\log_{10}(b^2/c) digits log10(1016/1)=16\approx \log_{10}(10^{16}/1) = 16. Essentially ~16 digits lost — the naive result is worthless (true small root c/(2b)=5×109\approx c/(2b) = 5\times10^{-9}). Acceptable answer: about 15–16 digits. (3.)


Question 3 (12)

(a) With Lagrange quadratic P2P_2 through the three points and substitution x=x0+shx = x_0 + sh: x0x2P2dx=h3(f0+4f1+f2).\int_{x_0}^{x_2} P_2\,dx = \frac{h}{3}\left(f_0 + 4f_1 + f_2\right). Derivation: integrate the interpolating polynomial (or Newton forward form) over s[0,2]s\in[0,2]; the coefficients 13,43,13\tfrac13,\tfrac43,\tfrac13 emerge. (3 setup/integration, 2 result.)

(b) Composite (nn even, h=(ba)/nh=(b-a)/n): abfdxh3[f0+fn+4 ⁣ ⁣i odd ⁣fi+2 ⁣ ⁣i eveni0,n ⁣fi].\int_a^b f\,dx \approx \frac{h}{3}\Big[f_0 + f_n + 4\!\!\sum_{i\text{ odd}}\!f_i + 2\!\!\sum_{i\text{ even}\atop i\neq 0,n}\!f_i\Big]. Requirement: nn must be even. (2 formula, 1 requirement.)

(c) h=0.25h=0.25; nodes 0,0.25,0.5,0.75,10,0.25,0.5,0.75,1. f0=1, f1=e0.0625=0.939413, f2=e0.25=0.778801, f3=e0.5625=0.569783, f4=e1=0.367879.f_0=1,\ f_1=e^{-0.0625}=0.939413,\ f_2=e^{-0.25}=0.778801,\ f_3=e^{-0.5625}=0.569783,\ f_4=e^{-1}=0.367879. I=0.253[1+0.367879+4(0.939413+0.569783)+2(0.778801)]I=\frac{0.25}{3}\big[1 + 0.367879 + 4(0.939413+0.569783) + 2(0.778801)\big] =0.253[1.367879+4(1.509196)+1.557602]=0.253(8.962265)=0.74685.=\frac{0.25}{3}[1.367879 + 4(1.509196) + 1.557602] = \frac{0.25}{3}(8.962265) = 0.74685. (True value 0.7468240.746824.) 0.74685\approx 0.74685. (2 evaluations, 2 answer.)


Question 4 (10)

(a) Pseudocode:

GaussSeidel(A, b, x0, tol, maxit):
    x = x0
    for k = 1 to maxit:
        x_old = copy(x)
        for i = 1 to n:
            s = b[i]
            for j = 1 to n, j != i:
                s = s - A[i][j] * x[j]   # uses updated x for j<i
            x[i] = s / A[i][i]
        if norm(x - x_old) < tol: return x
    return x   # (or flag non-convergence)

(2 loop structure, 2 in-place update using new values, 1 stopping test.)

(b) Sufficient condition: AA strictly diagonally dominant, aii>jiaij|a_{ii}| > \sum_{j\neq i}|a_{ij}| (also converges for SPD AA). Gauss–Seidel uses already-updated components within the same sweep, so it incorporates newer information → generally faster than Jacobi. (2 condition, 1 reason.)

(c) x1=3/4=0.75x_1 = 3/4 = 0.75. Then x2=(6+0.75)/4=1.6875x_2 = (6 + 0.75)/4 = 1.6875. Result: (0.75, 1.6875)(0.75,\ 1.6875). (2.) (True solution: x1=1.2, x2=1.8x_1=1.2,\ x_2=1.8.)


Question 5 (10)

(a) k1=f(tn,yn),k2=f(tn+h2,yn+h2k1),k_1 = f(t_n,y_n),\quad k_2 = f(t_n+\tfrac h2, y_n+\tfrac h2 k_1), k3=f(tn+h2,yn+h2k2),k4=f(tn+h,yn+hk3),k_3 = f(t_n+\tfrac h2, y_n+\tfrac h2 k_2),\quad k_4 = f(t_n+h, y_n+h k_3), yn+1=yn+h6(k1+2k2+2k3+k4).y_{n+1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4). (2 stages, 2 update.)

(b) RK4 local truncation error O(h5)O(h^5); global error O(h4)O(h^4). Euler: local O(h2)O(h^2), global O(h)O(h). RK4 is 4th-order accurate vs Euler 1st-order. (1 LTE, 1 global, 1 Euler contrast.)

(c) f=yf=y, h=0.5h=0.5, y0=1y_0=1: k1=1k_1=1, k2=1+0.25(1)=1.25k_2=1+0.25(1)=1.25, k3=1+0.25(1.25)=1.3125k_3=1+0.25(1.25)=1.3125, k4=1+0.5(1.3125)=1.65625k_4=1+0.5(1.3125)=1.65625. y1=1+0.56(1+2(1.25)+2(1.3125)+1.65625)=1+0.56(7.78125)=1.648438.y_1 = 1 + \frac{0.5}{6}(1 + 2(1.25)+2(1.3125)+1.65625) = 1 + \frac{0.5}{6}(7.78125)=1.648438. e0.5=1.648721e^{0.5}=1.648721; abs error 0.000283\approx 0.000283. y(0.5)1.64844y(0.5)\approx1.64844, error 2.8×104\approx2.8\times10^{-4}. (2 stages, 1 answer+error.)


Question 6 (8)

(a) Relative condition number = (relative change in output)/(relative change in input): κ=limδx0(f(x+δx)f(x))/f(x)δx/x=f(x)xf(x).\kappa = \lim_{\delta x\to 0}\left|\frac{(f(x+\delta x)-f(x))/f(x)}{\delta x / x}\right| = \left|\frac{f'(x)\,x}{f(x)}\right|. Using f(x+δx)f(x)f(x)δxf(x+\delta x)-f(x)\approx f'(x)\delta x. (2 definition, 2 limit/derivative.)

(b) Ill-conditioned problem: inherent property — small input perturbations cause large output changes regardless of method (κ1\kappa \gg 1). Unstable algorithm: the computational procedure introduces/amplifies rounding error beyond what the problem's conditioning warrants. A stable algorithm bounds added error, but the inherent sensitivity κ\kappa still multiplies the unavoidable input rounding — so no algorithm can produce an accurate result for an ill-conditioned problem. (2 distinction, 2 why stability can't rescue.)


[
  {"claim":"Composite Simpson n=4 for integral e^{-x^2} on [0,1] ≈ 0.74685",
   "code":"h=Rational(1,4);f=lambda x: exp(-x**2);I=h/3*(f(0)+f(1)+4*(f(Rational(1,4))+f(Rational(3,4)))+2*f(Rational(1,2)));result=abs(float(I)-0.74685)<1e-4"},
  {"claim":"RK4 one step h=0.5 for y'=y,y0=1 gives 1.648438",
   "code":"h=0.5;y=1.0;f=lambda t,y:y;k1=f(0,y);k2=f(0,y+h/2*k1);k3=f(0,y+h/2*k2);k4=f(0,y+h*k3);y1=y+h/6*(k1+2*k2+2*k3+k4);result=abs(y1-1.648438)<1e-5"},
  {"claim":"RK4 abs error vs e^0.5 ≈ 2.8e-4",
   "code":"h=0.5;y=1.0;k1=y;k2=y+h/2*k1;k3=y+h/2*k2;k4=y+h*k3;y1=y+h/6*(k1+2*k2+2*k3+k4);result=abs(abs(y1-float(exp(Rational(1,2))))-0.000283)<5e-5"},
  {"claim":"Gauss-Seidel one sweep gives (0.75,1.6875)",
   "code":"x1=(3-0)/4;x2=(6+x1)/4;result=abs(x1-0.75)<1e-12 and abs(x2-1.6875)<1e-12"}
]