Course Test · University: upper level

Numerical analysis: RatMath answers

The same twenty exercises, answered with RatMath Core and the Shelf libraries only. The code below was run when this page was built; its output is shown as printed, and compared with the independent answer.

Agrees 20

Libraries used here: core, analytic, linalg. In this code each library is in scope under its own name (linalg.det, stats.tTest), and Core's names (R, Rational, …) are in scope directly.

  1. 1.Floating pointAgrees
    What exact rational number does the double nearest to 0.1 equal? What is machine epsilon (the gap from 1 to the next double)?
    Independent answer
    0.1=0.000110011…20.1 = 0.000110011\ldots_2; rounded to 53 significant bits it is 3602879701896397255\dfrac{3602879701896397}{2^{55}}, slightly above 0.1. Machine epsilon is 2−52≈2.22×10−162^{-52} \approx 2.22\times10^{-16}.
    RatMath code
    [analytic.floatParts(0.1).exact, analytic.floatParts(1).ulp]
    Output
    [3602879701896397/36028797018963968, 1/4503599627370496]
  2. 2.Catastrophic cancellationAgrees
    Evaluate x+1−x\sqrt{x + 1} - \sqrt{x} at x=1016x = 10^{16} in double precision. What goes wrong, and what is the true value?
    Independent answer
    1016+110^{16} + 1 rounds to 101610^{16}, so the computed difference is 0, with every digit lost. Rewriting as 1x+1+x≈5×10−9\dfrac{1}{\sqrt{x+1} + \sqrt x} \approx 5\times10^{-9} gives the true value.
    RatMath code
    const r = analytic.roundoff("sqrt(x + 1) - sqrt(x)", { x: 1e16 });
    [r.float, r.exact, r.digitsLost]
    Output
    [0, 0.00000000499999999999:0.000000005, 16]
  3. 3.BisectionAgrees
    Bisection on x2−2x^2 - 2 over [1,2][1, 2]: how many steps until the guaranteed error (b−a)/2n+1(b - a)/2^{n+1} is below 10−610^{-6}?
    Independent answer
    2−(n+1)<10−62^{-(n+1)} < 10^{-6} needs n+1≥20n + 1 \ge 20 (220=10485762^{20} = 1048576): 19 halvings, i.e. 20 midpoints counting the first, with bound 2−20≈9.54×10−72^{-20} \approx 9.54\times10^{-7}.
    RatMath code
    const b = analytic.bisection("x^2 - 2", ["1", "2"], { tol: "1e-6" });
    [b.iterations.length, b.iterations.at(-1).bound]
    Output
    [20, 9.5367431640625e-7]
  4. 4.Newton's methodAgrees
    Apply Newton's method to x2−2x^2 - 2 from x0=1x_0 = 1. Give x1,x2,x3x_1, x_2, x_3.
    Independent answer
    xk+1=12(xk+2/xk)x_{k+1} = \tfrac12(x_k + 2/x_k): 32\tfrac32, 1712\tfrac{17}{12}, 577408≈1.4142157\tfrac{577}{408} \approx 1.4142157. The correct digits roughly double each step.
    RatMath code
    analytic.newton("x^2 - 2", "1").iterations.slice(1, 4).map((i) => i.x)
    Output
    [1.5, 1.4166666666666667, 1.4142156862745099]
    The iterates are doubles, so 1712\tfrac{17}{12} and 577408\tfrac{577}{408} come back rounded; the root itself is certified.
  5. 5.Secant methodAgrees
    Apply the secant method to x2−2x^2 - 2 from x0=1x_0 = 1, x1=2x_1 = 2. Give x2x_2 and x3x_3.
    Independent answer
    x2=2−2⋅2−12−(−1)=43x_2 = 2 - 2\cdot\dfrac{2 - 1}{2 - (-1)} = \tfrac43; x3=43−(−29)4/3−2−2/9−2=75x_3 = \tfrac43 - \left(-\tfrac29\right)\dfrac{4/3 - 2}{-2/9 - 2} = \tfrac75.
    RatMath code
    analytic.secant("x^2 - 2", "1", "2").iterations.slice(2, 4).map((i) => i.x)
    Output
    [1.3333333333333335, 1.4000000000000001]
  6. 6.Fixed-point iterationAgrees
    For xk+1=cos⁡xkx_{k+1} = \cos x_k, by what factor does the error shrink each step near the fixed point?
    Independent answer
    ∣g′(x∗)∣=sin⁡x∗≈sin⁡0.7390851≈0.6736|g'(x^*)| = \sin x^* \approx \sin 0.7390851 \approx 0.6736: linear convergence with that ratio.
    RatMath code
    analytic.fixedPoint("cos(x)", "1").multiplier
    Output
    -0.6736120291832148153[5:4]
  7. 7.InterpolationAgrees
    Find the polynomial through (0,1)(0, 1), (1,3)(1, 3), (2,7)(2, 7) and evaluate it at 3.
    Independent answer
    Divided differences: 1; 2, 4; 1. p=1+2x+x(x−1)=x2+x+1p = 1 + 2x + x(x - 1) = x^2 + x + 1, p(3)=13p(3) = 13.
    RatMath code
    const p = analytic.interpolate([[0, 1], [1, 3], [2, 7]]);
    [String(p.polynomial), p.at(3)]
    Output
    ["x^2 + x + 1", 13]
  8. 8.Natural cubic splineAgrees
    Find the natural cubic spline through (0,0)(0, 0), (1,1)(1, 1), (2,0)(2, 0) and its value at x=12x = \tfrac12.
    Independent answer
    With M0=M2=0M_0 = M_2 = 0: 4M1=6(0−2+0)=−124M_1 = 6(0 - 2 + 0) = -12, M1=−3M_1 = -3. On [0,1][0, 1]: S=−12x3+32xS = -\tfrac12x^3 + \tfrac32x, so S(12)=34−116=1116S(\tfrac12) = \tfrac34 - \tfrac1{16} = \tfrac{11}{16}.
    RatMath code
    analytic.spline([[0, 0], [1, 1], [2, 0]]).at("1/2")
    Output
    11/16
  9. 9.Chebyshev nodesAgrees
    Give the three Chebyshev nodes on [−1,1][-1, 1].
    Independent answer
    cos⁡(2i+1)π6\cos\dfrac{(2i + 1)\pi}{6}: 32,0,−32≈±0.8660254\tfrac{\sqrt3}2, 0, -\tfrac{\sqrt3}2 \approx \pm0.8660254.
    RatMath code
    analytic.chebyshevNodes(-1, 1, 3)
    Output
    [-56755/65536, 0, 14189/16384]
    The nodes are rounded to short rationals (within 2−162^{-16} by default), as the documentation says.
  10. 10.Least squaresAgrees
    Fit a least-squares line to (0,1)(0, 1), (1,3)(1, 3), (2,4)(2, 4), (3,4)(3, 4).
    Independent answer
    xˉ=1.5\bar x = 1.5, yˉ=3\bar y = 3, Sxy=5S_{xy} = 5, Sxx=5S_{xx} = 5: slope 1, intercept 3−1.5=1.53 - 1.5 = 1.5. y=x+32y = x + \tfrac32.
    RatMath code
    String(analytic.leastSquares([[0, 1], [1, 3], [2, 4], [3, 4]], 1).polynomial)
    Output
    x + 3/2
  11. 11.Richardson extrapolationAgrees
    Estimate f′(0)f'(0) for f=exf = e^x with central differences at h=0.1h = 0.1 and h=0.05h = 0.05, then one Richardson step.
    Independent answer
    D(0.1)=e0.1−e−0.10.2≈1.0016675D(0.1) = \dfrac{e^{0.1} - e^{-0.1}}{0.2} \approx 1.0016675, D(0.05)≈1.0004167D(0.05) \approx 1.0004167. 4D(0.05)−D(0.1)3≈0.99999979\dfrac{4D(0.05) - D(0.1)}{3} \approx 0.99999979, error about 2×10−72\times10^{-7} instead of 4×10−44\times10^{-4}.
    RatMath code
    const T = analytic.richardson("exp(x)", 0, { h: "0.1" }).table;
    [T[0][0], T[1][0], T[1][1]]
    Output
    [1.001667500198441, 1.000416718753101, 0.9999997916046544]
  12. 12.Trapezoid ruleAgrees
    Approximate ∫01x2 dx\int_0^1 x^2\,dx by the trapezoid rule with 4 panels.
    Independent answer
    18(0+2⋅1+4+916+1)=1132=0.34375\tfrac18\left(0 + 2\cdot\tfrac{1 + 4 + 9}{16} + 1\right) = \tfrac{11}{32} = 0.34375 (exact 13\tfrac13).
    RatMath code
    analytic.quadratureRule("x^2", ["0", "1"], { rule: "trapezoid", n: 4 }).value
    Output
    11/32
  13. 13.Simpson's ruleAgrees
    Apply Simpson's rule with one panel (three points) to ∫01x4 dx\int_0^1 x^4\,dx.
    Independent answer
    16(0+4⋅116+1)=524≈0.2083\tfrac16\left(0 + 4\cdot\tfrac1{16} + 1\right) = \tfrac{5}{24} \approx 0.2083 (exact 15\tfrac15).
    RatMath code
    analytic.quadratureRule("x^4", ["0", "1"], { rule: "simpson", n: 1 }).value
    Output
    5/24
  14. 14.Gaussian quadratureAgrees
    Apply two-point Gauss–Legendre quadrature to ∫−11x4 dx\int_{-1}^1 x^4\,dx.
    Independent answer
    Nodes ±1/3\pm1/\sqrt3, weights 1: 2⋅19=292\cdot\tfrac19 = \tfrac29 (exact 25\tfrac25; the rule is exact only up to degree 3).
    RatMath code
    analytic.gaussRule("x^4", ["-1", "1"], 2).value
    Output
    0.2222222222222222222[2:3]
  15. 15.Euler's methodAgrees
    Use Euler's method with h=0.1h = 0.1 on y′=yy' = y, y(0)=1y(0) = 1, up to t=0.5t = 0.5.
    Independent answer
    Each step multiplies by 1.1: 1.15=1.610511.1^5 = 1.61051 (exact e0.5≈1.64872e^{0.5} \approx 1.64872).
    RatMath code
    analytic.odeMethod("y", { y0: 1, until: "0.5", steps: 5 }).steps.at(-1).y
    Output
    1.61051
  16. 16.Runge–KuttaAgrees
    Take one classical RK4 step of size 0.1 on y′=yy' = y, y(0)=1y(0) = 1.
    Independent answer
    1+h+h22+h36+h424=1.10517083‾1 + h + \tfrac{h^2}2 + \tfrac{h^3}6 + \tfrac{h^4}{24} = 1.1051708\overline3 (exact e0.1=1.1051709181e^{0.1} = 1.1051709181).
    RatMath code
    analytic.odeMethod("y", { y0: 1, until: "0.1", steps: 1, method: "rk4" }).steps.at(-1).y
    Output
    1.1051708333333332
  17. 17.StabilityAgrees
    For y′=−10yy' = -10y, what is the largest step for which Euler's method does not grow?
    Independent answer
    Each step multiplies by 1−10h1 - 10h; ∣1−10h∣≤1|1 - 10h| \le 1 needs h≤0.2h \le 0.2. The stability interval for hλh\lambda is [−2,0][-2, 0].
    RatMath code
    const s = analytic.stability("euler");
    [s.interval, s.largestStep(-10)]
    Output
    [[-2, 0], 1/5]
  18. 18.LU factorisationAgrees
    Factor A=(2143)A = \begin{pmatrix}2&1\\4&3\end{pmatrix} as LULU (no pivoting).
    Independent answer
    ℓ21=2\ell_{21} = 2: L=(1021)L = \begin{pmatrix}1&0\\2&1\end{pmatrix}, U=(2101)U = \begin{pmatrix}2&1\\0&1\end{pmatrix}.
    RatMath code
    const F = linalg.lu([[2, 1], [4, 3]]).exact();
    [F.L, F.U].map(String)
    Output
    ["[1, 0]\n[2, 1]", "[2, 1]\n[0, 1]"]
  19. 19.Condition numberAgrees
    Find κ∞\kappa_\infty of (1111.0001)\begin{pmatrix}1&1\\1&1.0001\end{pmatrix}.
    Independent answer
    ∥A∥∞=2.0001\|A\|_\infty = 2.0001; A−1=104(1.0001−1−11)A^{-1} = 10^4\begin{pmatrix}1.0001&-1\\-1&1\end{pmatrix}, ∥A−1∥∞=20001\|A^{-1}\|_\infty = 20001. κ=2.0001⋅20001=40004.0001\kappa = 2.0001\cdot20001 = 40004.0001.
    RatMath code
    linalg.cond([["1", "1"], ["1", "1.0001"]], "inf").exact()
    Output
    400040001/10000
  20. 20.Iterative methods and eigenvaluesAgrees
    (a) Two Jacobi steps for 4x+y=14x + y = 1, x+3y=2x + 3y = 2 from (0,0)(0, 0). (b) The dominant eigenvalue of (2113)\begin{pmatrix}2&1\\1&3\end{pmatrix} by the power method.
    Independent answer
    (a) (14,23)(\tfrac14, \tfrac23), then (1−2/34,2−1/43)=(112,712)\left(\tfrac{1 - 2/3}{4}, \tfrac{2 - 1/4}{3}\right) = (\tfrac1{12}, \tfrac7{12}); the solution is (111,711)(\tfrac1{11}, \tfrac7{11}). (b) λ=5+52≈3.618034\lambda = \tfrac{5 + \sqrt5}{2} \approx 3.618034.
    RatMath code
    const J = linalg.methods.jacobi([[4, 1], [1, 3]], [1, 2], { steps: 2 });
    const P = linalg.methods.powerMethod([[2, 1], [1, 3]]);
    [...J.rows[2].x, P.rows.at(-1).estimate]
    Output
    [0.08333333333333334, 0.5833333333333334, 3.6180339887498945]