Docsity

Numerical Methods for Computing: Roots, Interpolation, Integration, Systems, ODEs, Study Guides, Projects, Research of Numerical Methods in Engineering

Toggle sidebar
bg1
Numerical Methods for Computing:
Roots, Interpolation, Integration,
Systems, ODEs
A study guide with runnable Python, real program output, a summary table and practice
questions
Author: Psychedelic Tunes
How to use this guide. Numerical methods replace an exact answer we cannot get (or cannot get
cheaply) with an approximation we can compute, together with an estimate of how wrong it is. This
guide covers the topics that appear on most first courses in numerical methods or numerical analysis for
computing students: error analysis, root finding, interpolation, numerical differentiation and integration,
direct and iterative solvers for linear systems, and initial-value problems. Every program is plain Python
3 (no libraries beyond math ) and is shown with the output it actually produced. Every hand-worked
number was rechecked with a separate script.
Contents
Errors and floating-point arithmetic
Root finding: the bisection method
Newton–Raphson
The secant method and comparing root finders
Polynomial interpolation: Lagrange and Newton
Numerical differentiation
Numerical integration: trapezoidal and Simpson's rules
Direct methods: Gaussian elimination and LU decomposition
Iterative methods: Jacobi and Gauss–Seidel
Ordinary differential equations: Euler and RK4
Summary table
Practice questions and answers
1. Errors and floating-point arithmetic
If p is the true value and p* the approximation:
Absolute error = |p − p*|
Relative error = |p − p*| ÷ |p| (often quoted as a percentage). Relative error is the fairer
measure when values differ in size: an error of 1 in 1,000,000 matters less than an error of 1 in 10.
p* approximates p to n significant digits if the relative error is below 5 × 10−n.
Errors come from two main sources. Truncation error comes from the method itself: stopping an
infinite process (a Taylor series, an iteration, a limit as h → 0) after finitely many steps. Round-off error
comes from storing real numbers in finite precision. Doubles (IEEE 754, 64-bit) keep 53 binary digits,
about 15–16 significant decimal digits. Machine epsilon ε is the gap between 1 and the next larger
double, 2−52 ≈ 2.22 × 10−16.
Two classic round-off traps: values such as 0.1 have no exact binary form, so never test floats with == ;
and subtractive cancellation — subtracting two nearly equal numbers wipes out the leading digits,
leaving mostly noise. The cure is to rewrite the formula, for example 1 − cos x = 2 sin²(x/2).
# Errors, floating point and significant digits
import math
true, approx = math.pi, 22/7
abs_err = abs(true - approx)
rel_err = abs_err / abs(true)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
•
•
•
Psychedelic Tunes Page 1 of 12
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Numerical Methods for Computing: Roots, Interpolation, Integration, Systems, ODEs and more Study Guides, Projects, Research Numerical Methods in Engineering in PDF only on Docsity!

Numerical Methods for Computing:

Roots, Interpolation, Integration,

Systems, ODEs

A study guide with runnable Python, real program output, a summary table and practice

questions

Author: Psychedelic Tunes

How to use this guide. Numerical methods replace an exact answer we cannot get (or cannot get cheaply) with an approximation we can compute, together with an estimate of how wrong it is. This guide covers the topics that appear on most first courses in numerical methods or numerical analysis for computing students: error analysis, root finding, interpolation, numerical differentiation and integration, direct and iterative solvers for linear systems, and initial-value problems. Every program is plain Python 3 (no libraries beyond math ) and is shown with the output it actually produced. Every hand-worked number was rechecked with a separate script.

Contents

Errors and floating-point arithmetic Root finding: the bisection method Newton–Raphson The secant method and comparing root finders Polynomial interpolation: Lagrange and Newton Numerical differentiation Numerical integration: trapezoidal and Simpson's rules Direct methods: Gaussian elimination and LU decomposition Iterative methods: Jacobi and Gauss–Seidel Ordinary differential equations: Euler and RK Summary table Practice questions and answers

1. Errors and floating-point arithmetic

If p is the true value and p* the approximation:

Absolute error = |p − p| Relative error = |p − p| ÷ |p| (often quoted as a percentage). Relative error is the fairer measure when values differ in size: an error of 1 in 1,000,000 matters less than an error of 1 in 10. p* approximates p to n significant digits if the relative error is below 5 × 10−n.

Errors come from two main sources. Truncation error comes from the method itself: stopping an infinite process (a Taylor series, an iteration, a limit as h → 0) after finitely many steps. Round-off error comes from storing real numbers in finite precision. Doubles (IEEE 754, 64-bit) keep 53 binary digits, about 15–16 significant decimal digits. Machine epsilon ε is the gap between 1 and the next larger

double, 2−52^ ≈ 2.22 × 10−16.

Two classic round-off traps: values such as 0.1 have no exact binary form, so never test floats with == ; and subtractive cancellation — subtracting two nearly equal numbers wipes out the leading digits, leaving mostly noise. The cure is to rewrite the formula, for example 1 − cos x = 2 sin²(x/2).

Errors, floating point and significant digits

import math true, approx = math.pi, 22/ abs_err = abs(true - approx) rel_err = abs_err / abs(true)

print(f"pi = {true:.10f}, 22/7 = {approx:.10f}") print(f"absolute error = {abs_err:.3e}, relative error = {rel_err:.3e}")

print("0.1 + 0.2 == 0.3 ?", 0.1 + 0.2 == 0.3, "->", repr(0.1 + 0.2)) print("safe test: math.isclose ->", math.isclose(0.1 + 0.2, 0.3))

eps = 1. while 1.0 + eps / 2 > 1.0: eps /= 2 print("machine epsilon (double) =", eps)

Subtractive cancellation: 1 - cos(x) for small x

x = 1e- naive = 1 - math.cos(x) stable = 2 * math.sin(x / 2) ** 2 print(f"1 - cos(1e-8): naive = {naive}, stable = {stable:.6e}")

pi = 3.1415926536, 22/7 = 3. absolute error = 1.264e-03, relative error = 4.025e- 0.1 + 0.2 == 0.3? False -> 0. safe test: math.isclose -> True machine epsilon (double) = 2.220446049250313e- 1 - cos(1e-8): naive = 0.0, stable = 5.000000e-

The naive formula returns 0.0, a 100% relative error; the rewritten one gives the correct 5 × 10− (since 1 − cos x ≈ x²/2). Errors also propagate : in a sum, absolute errors add (roughly); in a product or quotient, relative errors add (roughly). A problem is ill-conditioned if small changes in its input cause large changes in the answer — no algorithm can fully fix that. An algorithm is stable if it does not magnify round-off unnecessarily.

2. Root finding: the bisection method

A root of f is an x with f(x) = 0. The Intermediate Value Theorem says that if f is continuous on [a, b] and f(a) and f(b) have opposite signs, then a root lies in between. Bisection uses this directly:

Compute the midpoint c = (a + b) / 2. If f(a) · f(c) < 0, the root is in [a, c], so set b = c; otherwise set a = c. Repeat until the interval is short enough.

Each halving cuts the bracket in half, so after n halvings the midpoint is within (b − a) / 2n+1^ of a root. For an error at most tol, choose n ≥ log 2 ((b − a) / tol) − 1. Convergence is linear with ratio ½ — slow

but guaranteed, and it needs only a sign change, not a derivative.

The running example for Sections 2–4 is f(x) = x³ − x − 2. Since f(1) = −2 and f(2) = 4, a root lies in [1,

2]. With tol = 10−6: n ≥ log 2 (10^6 ) − 1 = 19.93 − 1, so 19 halvings.

Bisection, Newton-Raphson and secant on f(x) = x^3 - x - 2

def f(x): return x3 - x - 2 def df(x): return 3x*2 - 1

def bisection(a, b, tol=1e-6): n = 0 while (b - a) / 2 > tol: c = (a + b) / 2 n += 1 if n <= 4: print(f" bis {n}: a={a:.6f} b={b:.6f} c={c:.6f} f(c)={f(c):+.6f}") if f(a) * f(c) < 0: b = c else: a = c return (a + b) / 2, n

4. The secant method and comparing root finders

The secant method replaces f′ with the slope through the last two points, so no derivative is needed:

xn+1 = xn − f(xn) (xn − xn−1) / (f(xn) − f(xn−1))

From x₀ = 1, x₁ = 2: f(1) = −2, f(2) = 4, so x₂ = 2 − 4 × 1 / 6 = 1.333333 , as in the output. Its order of convergence is the golden ratio, about 1.618 (superlinear). It costs one new function evaluation per step, versus two (f and f′) for Newton, so it is often just as fast in practice. Unlike bisection, it does not keep the root bracketed and can fail if f(xn) = f(xn−1). (The regula falsi or false-position method uses

the same secant formula but keeps a bracket, like bisection.)

Method Needs Convergence order Guaranteed? Iterations here (from output) Bisection Sign change on [a, b]

1 (linear, ratio ½) Yes, if f is continuous

19 to reach 10−

Newton– Raphson

f and f′, one start 2 (quadratic) No — local only (^) 4 to reach 10−

Secant f, two starts ≈ 1.618 No (^) 8 to reach 10−

A common strategy in real libraries is to combine them: bisection for safety, then switch to a faster method once close (Brent's method is the standard example).

5. Polynomial interpolation: Lagrange and Newton

Given n + 1 points with distinct x-values, there is exactly one polynomial of degree at most n through all of them. Lagrange and Newton forms are two ways of writing that same polynomial.

Lagrange form. P(x) = Σ yi Li(x), where Li(x) = Πj≠i (x − xj) / (xi − xj). Each Li equals 1 at xi and 0 at

every other node. It is easy to write down, but adding a new point means starting again.

Newton divided-difference form. P(x) = f[x₀] + fx₀,x₁ + fx₀,x₁,x₂(x − x₁) + …, where f[xi, xi+1] =

(f[xi+1] − f[xi]) / (xi+1 − xi), and higher orders follow the same pattern. Adding a point just adds one

term.

Example data: (0, 1), (1, 3), (2, 2), (4, 5).

Lagrange and Newton divided-difference interpolation through 4 points

xs = [0, 1, 2, 4] ys = [1, 3, 2, 5]

def lagrange(x): total = 0. for i in range(len(xs)): L = 1. for j in range(len(xs)): if j != i: L *= (x - xs[j]) / (xs[i] - xs[j]) total += ys[i] * L return total

def divided_differences(): n = len(xs); table = [list(map(float, ys))] for k in range(1, n): prev = table[-1] table.append([(prev[i+1] - prev[i]) / (xs[i+k] - xs[i]) for i in range(n - k)]) return [col[0] for col in table], table

coef, table = divided_differences() for k, col in enumerate(table): print(f"order {k}:", [round(v, 6) for v in col])

print("Newton coefficients:", [round(c, 6) for c in coef])

def newton_eval(x): result, prod = coef[0], 1. for k in range(1, len(coef)): prod *= (x - xs[k-1]) result += coef[k] * prod return result

for x in (1.5, 3): print(f"P({x}) Lagrange = {lagrange(x):.6f}, Newton = {newton_eval(x):.6f}")

order 0: [1.0, 3.0, 2.0, 5.0] order 1: [2.0, -1.0, 1.5] order 2: [-1.5, 0.833333] order 3: [0.583333] Newton coefficients: [1.0, 2.0, -1.5, 0.583333] P(1.5) Lagrange = 2.656250, Newton = 2. P(3) Lagrange = 1.500000, Newton = 1.

Reading the table: first differences (3 − 1)/1 = 2, (2 − 3)/1 = −1, (5 − 2)/2 = 1.5; second (−1 − 2)/2 = −1.5, (1.5 − (−1))/3 = 0.8333; third (0.8333 + 1.5)/4 = 0.5833. So P(x) = 1 + 2x − 1.5x(x − 1) + 0.58333x(x − 1)(x − 2), and both forms give P(1.5) = 2.65625.

Interpolation error. f(x) − P(x) = f(n+1)(ξ) / (n + 1)! × Π (x − xi) for some ξ in the data range. Two

consequences: estimates inside the data range (interpolation) are far safer than outside it ( extrapolation ), and high-degree polynomials through equally spaced points can oscillate wildly near the ends ( Runge's phenomenon ). The usual fixes are piecewise low-degree fits (such as cubic splines ) or Chebyshev-spaced nodes.

6. Numerical differentiation

From Taylor series, with step h:

Formula Expression Truncation error Forward difference (f(x + h) − f(x)) / h O(h) Backward difference (f(x) − f(x − h)) / h O(h) Central difference (f(x + h) − f(x − h)) / (2h) O(h²) Second derivative (central) (f(x + h) − 2f(x) + f(x − h)) / h² O(h²)

The program in Section 7 tests f(x) = eˣ at x = 1, where f′(1) = e. In its output, each tenfold cut in h makes the forward error about 10× smaller (O(h)) and the central error about 100× smaller (O(h²)). But h cannot shrink forever: the numerator becomes a difference of nearly equal numbers (Section 1), so

round-off grows like ε/h. Total error is smallest at a moderate h — roughly 10−8^ for forward and 10− for central differences in double precision.

Richardson extrapolation combines two estimates with steps h and h/2 to cancel the leading error term; applied to the trapezoidal rule, it gives Romberg integration.

7. Numerical integration: trapezoidal and Simpson's rules

Split [a, b] into n equal strips of width h = (b − a)/n, with xi = a + ih.

Composite trapezoidal rule: T = h [ f₀/2 + f₁ + f₂ + … + fn−1 + fn/2 ]. Error = −(b − a) h² f″(ξ) / 12, so it is O(h²); exact for straight lines. Composite Simpson's 1/3 rule (n even): S = (h/3) [ f₀ + 4f₁ + 2f₂ + 4f₃ + … + 2fn−2 + 4fn−

  • fn ]. Error = −(b − a) h⁴ f(4)(ξ) / 180, so it is O(h⁴); exact for polynomials up to cubic.

Running system: 4x − y + z = 12, −x + 4y − 2z = −1, x − 2y + 4z = 5.

Column 1 (pivot 4, already largest): m₂₁ = −0.25, m₃₁ = 0.25. New row 2: 3.75y − 1.75z = 2. New row 3: −1.75y + 3.75z = 2. Column 2 (pivot 3.75 is larger than |−1.75|, no swap): m₃₂ = −1.75/3.75 = −0.46667. New row 3: 2.93333z = 2.93333. Back substitution: z = 1 ; y = (2 + 1.75 × 1)/3.75 = 1 ; x = (12 + 1 − 1)/4 = 3.

The product of the pivots, 4 × 3.75 × 2.93333 = 44, is det A (no row swaps were made).

LU decomposition stores that same elimination as A = LU, where L is unit lower-triangular and holds the multipliers, and U is the upper-triangular result. To solve Ax = b, solve Ly = b by forward substitution, then Ux = y by back substitution. The point: factor once (O(n³)), then solve for many right-hand sides at O(n²) each. With pivoting, the factorisation is PA = LU. Doolittle puts 1s on L's diagonal; Crout puts them on U's; Cholesky (A = LLᵀ) halves the work for symmetric positive-definite matrices.

Gaussian elimination, LU (Doolittle), Jacobi and Gauss-Seidel

A = [[4.0, -1.0, 1.0], [-1.0, 4.0, -2.0], [1.0, -2.0, 4.0]] b = [12.0, -1.0, 5.0]

def gauss(A, b): n = len(b); M = [row[:] + [bi] for row, bi in zip(A, b)] for k in range(n): p = max(range(k, n), key=lambda i: abs(M[i][k])) # partial pivoting M[k], M[p] = M[p], M[k] for i in range(k + 1, n): m = M[i][k] / M[k][k] for j in range(k, n + 1): M[i][j] -= m * M[k][j] x = [0.0] * n for i in range(n - 1, -1, -1): x[i] = (M[i][n] - sum(M[i][j] * x[j] for j in range(i + 1, n))) / M[i][i] return x

def lu(A): n = len(A); L = [[float(i == j) for j in range(n)] for i in range(n)] U = [[0.0] * n for _ in range(n)] for i in range(n): for j in range(i, n): U[i][j] = A[i][j] - sum(L[i][k] * U[k][j] for k in range(i)) for j in range(i + 1, n): L[j][i] = (A[j][i] - sum(L[j][k] * U[k][i] for k in range(i))) / U[i][i] return L, U

print("Gauss x =", [round(v, 10) for v in gauss(A, b)]) L, U = lu(A) print("L =", [[round(v, 4) for v in r] for r in L]) print("U =", [[round(v, 4) for v in r] for r in U])

def iterate(method, tol=1e-6): x = [0.0, 0.0, 0.0] for k in range(1, 100): old = x[:] src = old if method == "jacobi" else x # G-S uses newest values for i in range(3): s = sum(A[i][j] * src[j] for j in range(3) if j != i) x[i] = (b[i] - s) / A[i][i] if k <= 2: print(f" {method} k={k}: {[round(v, 4) for v in x]}") if max(abs(x[i] - old[i]) for i in range(3)) < tol: return x, k

for m in ("jacobi", "gauss-seidel"):

x, k = iterate(m) print(f"{m}: {[round(v, 6) for v in x]} in {k} iterations")

Gauss x = [3.0, 1.0, 1.0] L = [[1.0, 0.0, 0.0], [-0.25, 1.0, 0.0], [0.25, -0.4667, 1.0]] U = [[4.0, -1.0, 1.0], [0.0, 3.75, -1.75], [0.0, 0.0, 2.9333]] jacobi k=1: [3.0, -0.25, 1.25] jacobi k=2: [2.625, 1.125, 0.375] jacobi: [3.0, 1.0, 1.0] in 39 iterations gauss-seidel k=1: [3.0, 0.5, 0.75] gauss-seidel k=2: [2.9375, 0.8594, 0.9453] gauss-seidel: [3.0, 1.0, 1.0] in 11 iterations

L holds exactly the multipliers found by hand (−0.25, 0.25, −0.4667), and U holds the reduced rows.

9. Iterative methods: Jacobi and Gauss–Seidel

For large, sparse systems, iterative methods are often cheaper than elimination. Solve equation i for xi:

Jacobi: xi(k+1)^ = ( bi − Σj≠i aij xj(k)^ ) / aii. Every component uses the old iterate, so all of them can be updated in parallel. Gauss–Seidel: the same formula, but each new xj is used as soon as it is computed. It usually converges about twice as fast, and it needs only one vector of storage.

Using the system from Section 8 and starting from x = (0, 0, 0), the first Jacobi step gives x = 12/4 = 3, y = −1/4 = −0.25, z = 5/4 = 1.25. Gauss–Seidel gets x = 3 the same way, then immediately uses it: y = (−1 + 3)/4 = 0.5, then z = (5 − 3 + 2 × 0.5)/4 = 0.75. The program output above shows both converging to (3, 1, 1), Gauss–Seidel in 11 iterations against Jacobi's 39 (stopping when no component

changes by more than 10−6).

Convergence. Both methods are guaranteed to converge if A is strictly diagonally dominant (|aii| >

Σj≠i |aij| in every row). Gauss–Seidel also converges if A is symmetric positive-definite. Here each row's

diagonal is 4 and the off-diagonal sums are 2, 3 and 3, so A is strictly diagonally dominant. If a system is not dominant, reordering the equations can sometimes make it so. SOR (successive over-relaxation) speeds up Gauss–Seidel with a weight 1 < ω < 2.

10. Ordinary differential equations: Euler and RK

An initial-value problem y′ = f(t, y), y(t₀) = y₀ is solved step by step with step size h.

Euler's method: yn+1 = yn + h f(tn, yn). It follows the tangent line. The local error is O(h²) per step and the global error is O(h) (first order). Heun's method (improved Euler, RK2): average the slope at the start with the slope at an Euler-predicted end point. Global error O(h²). Classical fourth-order Runge–Kutta (RK4): k₁ = f(tn, yn), k₂ = f(tn + h/2, yn + h k₁/2), k₃ = f(tn + h/2, yn + h k₂/2), k₄ = f(tn + h, yn + h k₃), yn+1 = yn + (h/6)(k₁ + 2k₂ + 2k₃ + k₄). Global error O(h⁴) , at four f-evaluations per step.

Test problem: y′ = t − 2y, y(0) = 1, with exact solution y = t/2 − 1/4 + (5/4)e−2t. Euler's first step with h = 0.1: y₁ = 1 + 0.1 × (0 − 2) = 0.8. RK4's first step: k₁ = −2, k₂ = 0.05 − 2(1 − 0.1) = −1.75, k₃ = 0. − 2(1 − 0.0875) = −1.775, k₄ = 0.1 − 2(1 − 0.1775) = −1.545.

Euler vs RK4 for y' = t - 2y, y(0) = 1; exact y = t/2 - 1/4 + (5/4)e^(-2t)

import math f = lambda t, y: t - 2 * y exact = lambda t: t / 2 - 0.25 + 1.25 * math.exp(-2 * t)

def euler(h, T=1.0):

11. Summary table

Topic Key formula or rule Order / cost Example result in this guide Errors abs = |p − p*|; rel = abs ÷ |p| (^) ε ≈ 2.22 × 10−16^ 22/7 for π: rel. error 4.0 × 10 − Bisection Keep the half with a sign change

Linear; n ≥ log₂((b − a)/ tol) − 1

Root 1.521380 of x³ − x − 2 in 19 halvings Newton–Raphson x − f/f′ Quadratic; needs f′ 1.521379706805 in 4 steps Secant Slope through the last two points

≈ 1.618; no f′ Same root in 8 steps

Lagrange / Newton Unique degree-≤n polynomial through n + 1 points

Newton form extends easily

P(1.5) = 2.

Forward / central difference

(f(x+h) − f(x))/h; (f(x+h) − f(x−h))/2h

O(h); O(h²) (^) h = 0.01: errors 1.36 × 10− vs 4.53 × 10− Trapezoidal h[f₀/2 + … + fn/2] O(h²); exact for lines (^) n = 8: error 6.51 × 10− Simpson 1/3 (h/3)[1, 4, 2, 4, …, 4, 1] O(h⁴); exact for cubics; n even

n = 8: error 3.78 × 10−

Gaussian elimination

Eliminate below pivots, back- substitute

≈ ⅔n³ (3, 1, 1); det = 44

LU A = LU; Ly = b, Ux = y O(n³) once, O(n²) per b L multipliers −0.25, 0.25, −0. Jacobi / Gauss– Seidel

Solve row i for xi; G–S uses new values

Converge if strictly diagonally dominant

39 vs 11 iterations

Euler y + h f(t, y) Global O(h) (^) h = 0.1: error 3.5 × 10− RK4 (h/6)(k₁ + 2k₂ + 2k₃ + k₄) Global O(h⁴) (^) h = 0.1: error 5.3 × 10−

12. Practice questions

The true value is e = 2.718281828… and the approximation is 2.718. Find the absolute and relative errors. To how many significant digits is the approximation correct? Explain why 0.1 + 0.2 == 0.3 is False in Python, and say how floating-point values should be compared instead. Apply three iterations of bisection to f(x) = x² − 3 on [1, 2]. Give each midpoint, the sign of f there, and the final interval. How many bisection iterations are needed on [0, 1] to guarantee that the midpoint cn is within

10 −4^ of the root, using the bound (b − a)/2n? Use Newton–Raphson on f(x) = x² − 10 with x₀ = 3 to find three iterates, and compare them with √10. Use Newton–Raphson on f(x) = cos x − x (x in radians) with x₀ = 1. Give x₁, x₂ and x₃ to 7 decimal places. Apply two steps of the secant method to f(x) = x² − 3 with x₀ = 1 and x₁ = 2. Give three situations in which Newton–Raphson can fail or converge slowly, and one way to make root finding more robust. Given ln 2 = 0.6931 and ln 3 = 1.0986, estimate ln 2.5 by linear interpolation. Compare with the true value 0.916291 and explain the sign of the error. Find the Lagrange interpolating polynomial through (0, 1), (1, 2) and (2, 5). Simplify it and evaluate P(1.5). Build the Newton divided-difference table for (1, 1), (2, 8), (3, 27), (4, 64). Write the polynomial and evaluate P(2.5). Why is the answer exact? For f(x) = x³ at x = 2 with h = 0.1, compute the forward and central difference estimates of f′(2) and their errors.

Use the central second-difference formula with h = 0.1 to estimate f″(1) for f(x) = x⁴. What is the exact value? Use the composite trapezoidal rule with n = 4 to approximate ∫₀² x² dx. Find the error and explain its sign. Use Simpson's 1/3 rule with n = 2 to approximate ∫₀¹ eˣ dx. Find the error. Solve x + 2y + z = 8, 2x + y − z = 1, 3x − y + 2z = 7 by Gaussian elimination (no pivoting), showing the multipliers. Find the LU decomposition (Doolittle) of A = [[2, 1], [6, 5]], and use it to solve Ax = b with b = (3, 13). For 10x + y = 12 and x + 5y = 11, starting from (0, 0), perform two iterations of Jacobi and two of Gauss–Seidel. What is the exact solution, and why are both methods guaranteed to converge? Use Euler's method with h = 0.25 to approximate y(1) for y′ = y, y(0) = 1. Find the error against e. Perform one RK4 step with h = 0.1 for y′ = y, y(0) = 1. Give k₁ to k₄, y₁ and the error against e0.1.

Answers

1. Absolute error = 2.718281828 − 2.718 = 2.82 × 10−4. Relative error = 2.82 × 10−4^ ÷ 2.71828 =

1.04 × 10−4. Since 1.04 × 10−4^ < 5 × 10−4, it is correct to 4 significant digits (it is not below 5 ×

10 −5).

2. 0.1, 0.2 and 0.3 have no exact binary (base-2) representation, so each is stored rounded; the rounded sum is 0.30000000000000004, which differs from the stored 0.3 in the last bit. Compare with a tolerance instead: abs(a − b) <= tol , or math.isclose(a, b) , which uses a relative tolerance. 3. c₁ = 1.5: f = −0.75 < 0, so the root is in [1.5, 2]. c₂ = 1.75: f = 0.0625 > 0, so [1.5, 1.75]. c₃ = 1.625: f = −0.359375 < 0, so the final interval is [1.625, 1.75]. (√3 ≈ 1.7321 lies inside.) 4. Need 1/2n^ ≤ 10−4, so n ≥ log₂(10^4 ) = 13.29; therefore n = 14. 5. xn+1 = (xn + 10/xn)/2. x₁ = (3 + 3.33333)/2 = 3.1666667 ; x₂ = 3.1622807 ; x₃ = 3.1622777. √

= 3.16227766, so x₃ is correct to 7 decimal places. The error went 4.4 × 10−3^ → 3.0 × 10−6^ → about

10 −12, which is quadratic convergence.

6. f′(x) = −sin x − 1. x₁ = 1 − (cos 1 − 1)/(−sin 1 − 1) = 1 − (−0.459698)/(−1.841471) = 0.7503639 ; x₂ = 0.7391129 ; x₃ = 0.7390851 (the root is 0.7390851…). 7. f(1) = −2, f(2) = 1. x₂ = 2 − 1 × (2 − 1)/(1 − (−2)) = 1.666667. f(x₂) = −0.222222, so x₃ = 1. − (−0.222222)(1.666667 − 2)/(−0.222222 − 1) = 1.. 8. Any three of: f′(xn) = 0 or near 0 (flat tangent, huge step); a poor starting point causing divergence or

a cycle; a multiple root (convergence becomes only linear); f′ unavailable or expensive. Robustness: bracket the root first with bisection and switch to Newton or secant once close (a hybrid, as in Brent's method), or reject any step that leaves the bracket.

9. ln 2.5 ≈ 0.6931 + (1.0986 − 0.6931) × 0.5 = 0.89585. True 0.916291, so the error is 0.0204 (an underestimate). ln is concave (f″ < 0), so the chord lies below the curve. 10. L₀ = (x − 1)(x − 2)/2, L₁ = −x(x − 2), L₂ = x(x − 1)/2. P(x) = 1·L₀ + 2·L₁ + 5·L₂ = x² + 1. P(1.5) = 3.. 11. First differences: 7, 19, 37. Second: 6, 9. Third: 1. P(x) = 1 + 7(x − 1) + 6(x − 1)(x − 2) + 1(x − 1)(x − 2)(x − 3). P(2.5) = 15.625. It equals 2.5³ exactly because the data come from f(x) = x³, and a cubic is reproduced exactly by the unique cubic interpolant (the error term has f⁽⁴⁾ = 0). 12. Forward: (2.1³ − 8)/0.1 = (9.261 − 8)/0.1 = 12.61 , error 0.61. Central: (9.261 − 6.859)/0.2 = 12.01 , error 0.01. Exact f′(2) = 12. Central difference is far better (O(h²) versus O(h)). 13. (1.1⁴ − 2 × 1 + 0.9⁴)/0.01 = (1.4641 − 2 + 0.6561)/0.01 = 12.02. Exact f″(1) = 12x² = 12 , so the error is 0.02 (= h² f⁽⁴⁾/12 = 0.01 × 24/12). 14. h = 0.5: T = 0.5 × [0/2 + 0.25 + 1 + 2.25 + 4/2] = 0.5 × 5.5 = 2.75. Exact 8/3 = 2.6667, so the error is 0.0833 (overestimate). f″ = 2 > 0, so the function is convex and each chord lies above the curve; this matches −(b − a)h²f″/12 = −2 × 0.25 × 2/12 = −0.0833 (exact minus approximate).