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.Floating pointAgreesWhat 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; rounded to 53 significant bits it is , slightly above 0.1. Machine epsilon is .RatMath code
[analytic.floatParts(0.1).exact, analytic.floatParts(1).ulp]Output[3602879701896397/36028797018963968, 1/4503599627370496]
- 2.Catastrophic cancellationAgreesEvaluate at in double precision. What goes wrong, and what is the true value?Independent answerrounds to , so the computed difference is 0, with every digit lost. Rewriting as 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.BisectionAgreesBisection on over : how many steps until the guaranteed error is below ?Independent answerneeds (): 19 halvings, i.e. 20 midpoints counting the first, with bound .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.Newton's methodAgreesApply Newton's method to from . Give .Independent answer: , , . 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 and come back rounded; the root itself is certified. - 5.Secant methodAgreesApply the secant method to from , . Give and .Independent answer; .RatMath code
analytic.secant("x^2 - 2", "1", "2").iterations.slice(2, 4).map((i) => i.x)Output[1.3333333333333335, 1.4000000000000001]
- 6.Fixed-point iterationAgreesFor , by what factor does the error shrink each step near the fixed point?Independent answer: linear convergence with that ratio.RatMath code
analytic.fixedPoint("cos(x)", "1").multiplierOutput-0.6736120291832148153[5:4]
- 7.InterpolationAgreesFind the polynomial through , , and evaluate it at 3.Independent answerDivided differences: 1; 2, 4; 1. , .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.Natural cubic splineAgreesFind the natural cubic spline through , , and its value at .Independent answerWith : , . On : , so .RatMath code
analytic.spline([[0, 0], [1, 1], [2, 0]]).at("1/2")Output11/16
- 9.Chebyshev nodesAgreesGive the three Chebyshev nodes on .Independent answer: .RatMath code
analytic.chebyshevNodes(-1, 1, 3)Output[-56755/65536, 0, 14189/16384]
The nodes are rounded to short rationals (within by default), as the documentation says. - 10.Least squaresAgreesFit a least-squares line to , , , .Independent answer, , , : slope 1, intercept . .RatMath code
String(analytic.leastSquares([[0, 1], [1, 3], [2, 4], [3, 4]], 1).polynomial)Outputx + 3/2
- 11.Richardson extrapolationAgreesEstimate for with central differences at and , then one Richardson step.Independent answer, . , error about instead of .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.Trapezoid ruleAgreesApproximate by the trapezoid rule with 4 panels.Independent answer(exact ).RatMath code
analytic.quadratureRule("x^2", ["0", "1"], { rule: "trapezoid", n: 4 }).valueOutput11/32
- 13.Simpson's ruleAgreesApply Simpson's rule with one panel (three points) to .Independent answer(exact ).RatMath code
analytic.quadratureRule("x^4", ["0", "1"], { rule: "simpson", n: 1 }).valueOutput5/24
- 14.Gaussian quadratureAgreesApply two-point Gauss–Legendre quadrature to .Independent answerNodes , weights 1: (exact ; the rule is exact only up to degree 3).RatMath code
analytic.gaussRule("x^4", ["-1", "1"], 2).valueOutput0.2222222222222222222[2:3]
- 15.Euler's methodAgreesUse Euler's method with on , , up to .Independent answerEach step multiplies by 1.1: (exact ).RatMath code
analytic.odeMethod("y", { y0: 1, until: "0.5", steps: 5 }).steps.at(-1).yOutput1.61051
- 16.Runge–KuttaAgreesTake one classical RK4 step of size 0.1 on , .Independent answer(exact ).RatMath code
analytic.odeMethod("y", { y0: 1, until: "0.1", steps: 1, method: "rk4" }).steps.at(-1).yOutput1.1051708333333332
- 17.StabilityAgreesFor , what is the largest step for which Euler's method does not grow?Independent answerEach step multiplies by ; needs . The stability interval for is .RatMath code
const s = analytic.stability("euler"); [s.interval, s.largestStep(-10)]Output[[-2, 0], 1/5]
- 18.LU factorisationAgreesFactor as (no pivoting).Independent answer: , .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.Condition numberAgreesFind of .Independent answer; , . .RatMath code
linalg.cond([["1", "1"], ["1", "1.0001"]], "inf").exact()Output400040001/10000
- 20.Iterative methods and eigenvaluesAgrees(a) Two Jacobi steps for , from . (b) The dominant eigenvalue of by the power method.Independent answer(a) , then ; the solution is . (b) .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]