Course Overview
Numerical Methods introduces computational techniques for solving mathematical problems that cannot be addressed analytically. The course covers methods for finding roots of equations, interpolating functions, differentiating and integrating numerically, solving linear and differential equations, and handling partial differential equations. Students learn to implement algorithms using computational tools and analyze their accuracy, stability, and efficiency. The course builds foundational skills for scientific computing, engineering simulations, and data-driven problem solving across computer science and applied mathematics disciplines.
Learning Outcomes
Upon completion of this course, students will be able to:
- Classify numerical problems and select appropriate solution methods based on problem structure and required precision.
- Implement root-finding algorithms (bisection, false position, Newton-Raphson) and analyze their convergence properties.
- Construct interpolating polynomials using Lagrange and Newton divided-difference forms, and apply cubic spline interpolation for smooth curve fitting.
- Differentiate and integrate numerically using finite-difference formulas, Simpson's rules, Romberg integration, and handle double integration.
- Solve systems of linear algebraic equations using direct methods (Gaussian elimination, Gauss-Jordan) and iterative methods (Jacobi, Gauss-Seidel, power method).
- Formulate and solve ordinary differential equations using Taylor series, Euler, Heun, and Runge-Kutta methods, including boundary value problems via the shooting method.
- Discretize and solve partial differential equations using finite-difference approaches for Laplace and Poisson equations.
- Evaluate the error, stability, and computational cost of each numerical method and justify method selection for specific applications.
- Write complete, runnable implementations of numerical algorithms in a programming language, with proper input handling and result verification.
- Compare and contrast numerical methods with analytical techniques, recognizing trade-offs between accuracy, efficiency, and generality.
Unit 1: Solution of Nonlinear Equations
Teaching Hours: 10
Introduction
A nonlinear equation is an equation in which the unknown variable appears with a non-linear exponent or inside a non-linear function. Finding the roots (zeros) of such equations is fundamental to many areas of science and engineering. Unlike linear equations, nonlinear equations often cannot be solved analytically, necessitating numerical approximation methods. This unit covers the most widely used root-finding algorithms, their implementation, and their comparative advantages.
Types of Equation
Equations are classified based on the nature of the function involved:
- Polynomial equations: $f(x) = a_nx^n + a_{n-1}x^{n-1} + \cdots + a_1x + a_0 = 0$
- Transcendental equations: $f(x) = \sin x, e^x, \ln x, \cos x = 0$
- Mixed equations: combinations of polynomial and transcendental terms
The intermediate value theorem guarantees a root exists in an interval $[a, b]$ if $f(a)$ and $f(b)$ have opposite signs (for continuous functions). This forms the basis for bracketing methods.
Errors in Computing
All numerical computations introduce errors. The two primary error types are:
- Absolute error: $|x_{\text{exact}} - x_{\text{approx}}|$
- Relative error: $\frac{|x_{\text{exact}} - x_{\text{approx}}|}{|x_{\text{exact}}|}$ (when $|x_{\text{exact}}| \neq 0$)
Round-off error arises from finite-precision arithmetic in computers. Truncation error results from approximating mathematical procedures (e.g., truncating an infinite series). Understanding and controlling these errors is essential for reliable numerical computation.
The Bisection Method
The bisection method is a bracketing root-finding algorithm that repeatedly halves an interval and selects the subinterval in which a root must lie.
Algorithm:
- Given a continuous function $f(x)$ and an interval $[a, b]$ such that $f(a) \cdot f(b) < 0$
- Compute the midpoint $c = \frac{a + b}{2}$
- If $f(c) = 0$ or the interval size $|b - a|$ is within tolerance, stop; $c$ is the root
- Otherwise, determine the subinterval containing the root:
- If $f(a) \cdot f(c) < 0$, the root lies in $[a, c]$; set $b = c$
- If $f(c) \cdot f(b) < 0$, the root lies in $[c, b]$; set $a = c$
- Repeat from step 2 until convergence
Convergence: The bisection method converges linearly. After $k$ iterations, the interval width is $(b - a)/2^k$, and the error bound is $|x_k - x^*| \leq (b - a)/2^k$. This guarantees convergence regardless of the function's behavior, but the rate is slow compared to other methods.
Worked Example: Find the root of $f(x) = x^2 - 2 = 0$ in the interval $[1, 2]$ with tolerance $10^{-4}$.
- Initial interval: $[1, 2]$, $f(1) = -1$, $f(2) = 2$, sign change confirmed
- Iteration 1: $c_1 = 1.5$, $f(1.5) = 2.25 - 2 = 0.25 > 0$. Since $f(1) \cdot f(1.5) < 0$, new interval: $[1, 1.5]$
- Iteration 2: $c_2 = 1.25$, $f(1.25) = 1.5625 - 2 = -0.4375 < 0$. Since $f(1.25) \cdot f(1.5) < 0$, new interval: $[1.25, 1.5]$
- Iteration 3: $c_3 = 1.375$, $f(1.375) = 1.890625 - 2 = -0.109375 < 0$. New interval: $[1.375, 1.5]$
- Iteration 4: $c_4 = 1.4375$, $f(1.4375) = 2.06640625 - 2 = 0.06640625 > 0$. New interval: $[1.375, 1.4375]$
- Iteration 5: $c_5 = 1.40625$, $f(1.40625) = 1.9775390625 - 2 = -0.0224609375 < 0$. New interval: $[1.40625, 1.4375]$
- After 5 iterations, interval width = $0.03125 > 10^{-4}$. Continue...
- After 14 iterations, interval width = $\frac{2-1}{2^{14}} = \frac{1}{16384} \approx 6.1 \times 10^{-5} < 10^{-4}$
- Approximate root: $c_{14} \approx 1.41406$, with true value $\sqrt{2} \approx 1.41421$
The Method of False Position (Regula Falsi)
The method of false position is also a bracketing method, but unlike bisection, it uses a straight line (linear interpolation) to estimate the root rather than simply halving the interval.
Algorithm:
- Given $[a, b]$ with $f(a) \cdot f(b) < 0$
- Compute the x-intercept of the line through $(a, f(a))$ and $(b, f(b))$: $$c = \frac{a \cdot f(b) - b \cdot f(a)}{f(b) - f(a)}$$
- If $f(c) = 0$ or $|b - a|$ is within tolerance, stop; $c$ is the root
- Otherwise, same interval selection as bisection: set $a = c$ or $b = c$ based on sign of $f(c)$
- Repeat from step 2
Comparison with Bisection: The false position method can be faster than bisection because it uses the function values to improve the estimate. However, it can suffer from slow convergence if one endpoint remains fixed for many iterations (the "stagnation" problem). Bisection always halves the interval, guaranteeing steady progress but at a predictable linear rate.
Worked Example: Same function $f(x) = x^2 - 2$, interval $[1, 2]$.
- Initial: $a = 1, f(a) = -1$; $b = 2, f(b) = 2$
- Compute $c = \frac{1 \cdot 2 - 2 \cdot (-1)}{2 - (-1)} = \frac{2 + 2}{3} = \frac{4}{3} \approx 1.333$
- $f(4/3) = (16/9) - 2 = -2/9 \approx -0.222$. Since $f(a)$ and $f(c)$ are both negative, set $a = c = 4/3$ (keep $b = 2$)
- New interval: $[4/3, 2]$. Compute $c = \frac{(4/3) \cdot 2 - 2 \cdot (-2/9)}{2 - (-2/9)} = \frac{8/3 + 4/9}{20/9} = \frac{28/9}{20/9} = \frac{7}{5} = 1.4$
- $f(7/5) = (49/25) - 2 = -1/25 = -0.04$. Both $f(4/3)$ and $f(7/5)$ are negative, so set $a = 7/5$; the root remains bracketed in $[7/5, 2]$.
- This retains the bracketed property and converges toward $\sqrt{2} \approx 1.41421$.
Newton-Raphson Method
The Newton-Raphson method is an open (bracketing-free) root-finding algorithm that uses the tangent line to approximate the root. It requires the derivative $f'(x)$.
Algorithm:
- Start with an initial guess $x_0$
- Iterate using the formula: $$x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}$$
- Stop when $|x_{n+1} - x_n| < \epsilon$ (absolute tolerance) or $|f(x_{n+1})| < \epsilon$ (function tolerance), or after a maximum number of iterations
Geometric Interpretation: At each step, the function is approximated by its tangent line at $x_n$. The x-intercept of this tangent line gives the next approximation $x_{n+1}$.
Convergence: The Newton-Raphson method converges quadratically near a simple root (i.e., the error squares at each step). If $x^$ is a simple root (i.e., $f(x^) = 0$ and $f'(x^) \neq 0$), then: $$|x_{n+1} - x^| \approx C |x_n - x^*|^2$$ for some constant $C$. This means the number of correct digits roughly doubles with each iteration.
Failure Cases:
- If $f'(x_n) = 0$, the method fails (division by zero). The tangent is horizontal and never crosses the x-axis.
- If the initial guess $x_0$ is not sufficiently close to the root, the method may diverge or converge to a different root.
- If $f(x)$ has inflection points or discontinuities near the root, convergence is not guaranteed.
Worked Example: Find the root of $f(x) = x^2 - 2 = 0$ using Newton-Raphson with initial guess $x_0 = 2$.
- $f(x) = x^2 - 2$, $f'(x) = 2x$
- Iteration 1: $x_1 = 2 - \frac{2^2 - 2}{2 \cdot 2} = 2 - \frac{2}{4} = 1.5$
- Iteration 2: $x_2 = 1.5 - \frac{1.5^2 - 2}{2 \cdot 1.5} = 1.5 - \frac{0.25}{3} = 1.5 - 0.08333 = 1.41667$
- Iteration 3: $x_3 = 1.41667 - \frac{1.41667^2 - 2}{2 \cdot 1.41667} = 1.41667 - \frac{0.00695}{2.83334} \approx 1.41667 - 0.00245 = 1.41422$
Only 3 iterations were needed to reach $x_3 \approx 1.41422$, matching $\sqrt{2} \approx 1.41421$ to 5 decimal places, compared to 14 for the bisection method — demonstrating the much faster quadratic convergence of Newton-Raphson when the derivative is available and the initial guess is good.
Solution of System of Nonlinear Equations
For a system of $n$ nonlinear equations in $n$ unknowns: $$\begin{cases} f_1(x_1, x_2, \ldots, x_n) = 0 \ f_2(x_1, x_2, \ldots, x_n) = 0 \ \vdots \ f_n(x_1, x_2, \ldots, x_n) = 0 \end{cases}$$
The Newton-Raphson method generalizes to systems using the Jacobian matrix $J(x) = \left[\frac{\partial f_i}{\partial x_j}\right]$:
Algorithm:
- Start with an initial guess $\mathbf{x}^{(0)} = (x_1^{(0)}, x_2^{(0)}, \ldots, x_n^{(0)})$
- Iterate: $$J(\mathbf{x}^{(k)}) \cdot \Delta \mathbf{x}^{(k)} = -F(\mathbf{x}^{(k)})$$ where $F(\mathbf{x}) = [f_1, f_2, \ldots, f_n]^T$ and $\mathbf{x}^{(k+1)} = \mathbf{x}^{(k)} + \Delta \mathbf{x}^{(k)}$
- Solve the linear system for $\Delta \mathbf{x}^{(k)}$ (using Gaussian elimination or LU decomposition)
- Check convergence: $|\Delta \mathbf{x}^{(k)}| < \epsilon$ or $|F(\mathbf{x}^{(k)})\| < \epsilon$
- Repeat until convergence
Fixed Point Iteration and Convergence
A fixed point of a function $g$ is a value $x^$ such that $g(x^) = x^*$. Fixed point iteration generates a sequence $x_{n+1} = g(x_n)$ and converges to a fixed point under appropriate conditions.
Convergence Criterion: If $|g'(x^)| < 1$, the iteration converges linearly near the fixed point. If $|g'(x^)| > 1$, the iteration diverges. If $|g'(x^*)| = 1$, the test is inconclusive.
Transformation for Fixed Point Iteration: To solve $f(x) = 0$, rearrange into the form $x = g(x)$. Multiple rearrangements are possible; some may converge while others diverge. A sufficient condition for convergence is $|g'(x)| < 1$ for all $x$ in the interval of interest.
Example: Solve $x^2 - 2 = 0$ via fixed point iteration.
- Rearrangement 1: $x = \frac{2}{x}$ (diverges for $x_0 = 2$ since $g'(x) = -\frac{2}{x^2}$, $|g'(2)| = 0.5$, but convergence depends on the starting point)
- Rearrangement 2: $x = \frac{1}{2}\left(x + \frac{2}{x}\right)$ (this is effectively the Babylonian method for $\sqrt{2}$, which converges for any positive initial guess)
Unit 2: Interpolation and Approximation
Teaching Hours: 10
Introduction
Interpolation is the process of constructing a function that passes through a given set of data points. If the data points are $(x_0, y_0), (x_1, y_1), \ldots, (x_n, y_n)$ with distinct $x$-values, an interpolating polynomial $P_n(x)$ of degree at most $n$ satisfies $P_n(x_i) = y_i$ for all $i$. Interpolation is used for function approximation, data visualization, and numerical integration (via quadrature rules). This unit covers polynomial interpolation forms and piecewise alternatives.
Errors in Polynomial Interpolation
The error in polynomial interpolation depends on the $(n+1)$-th derivative of the function and the distribution of interpolation points. For a function $f(x)$ interpolated by the polynomial $P_n(x)$ through $n+1$ distinct points, the error at any $x$ is:
$$f(x) - P_n(x) = \frac{f^{(n+1)}(\xi)}{(n+1)!} \prod_{i=0}^{n} (x - x_i)$$
for some $\xi$ in the interval containing the interpolation points and $x$. A key observation: the error depends on the $(n+1)$-th derivative and the product term $\prod (x - x_i)$, which can grow large if the interpolation interval is wide or the points are poorly distributed.
Runge's Phenomenon: When interpolating $f(x) = \frac{1}{1 + 25x^2}$ on $[-1, 1]$ using equally spaced points, the polynomial oscillates wildly near the endpoints as $n$ increases. This does not occur with Chebyshev nodes, which cluster points near the endpoints to minimize the product term.
Lagrange's Polynomials
The Lagrange interpolation polynomial is the unique polynomial of degree $\leq n$ passing through $n+1$ given points. It is expressed as:
$$P_n(x) = \sum_{i=0}^{n} y_i \cdot L_i(x)$$
where $L_i(x)$ are the Lagrange basis polynomials:
$$L_i(x) = \prod_{\substack{j=0 \ j \neq i}}^{n} \frac{x - x_j}{x_i - x_j} = \frac{(x - x_0)(x - x_1)\cdots(x - x_{i-1})(x - x_{i+1})\cdots(x - x_n)}{(x_i - x_0)(x_i - x_1)\cdots(x_i - x_{i-1})(x_i - x_{i+1})\cdots(x_i - x_n)}$$
Key Properties:
- $L_i(x_j) = \delta_{ij}$ (Kronecker delta: 1 if $i = j$, 0 otherwise)
- The polynomial automatically satisfies $P_n(x_i) = y_i$ for all $i$
- Basis polynomials are independent of the $y$-values; only the weights $y_i$ matter
Worked Example: Find the Lagrange interpolating polynomial for $(1, 3), (2, 5), (4, 11)$.
- $n = 2$, $L_0(x) = \frac{(x - 2)(x - 4)}{(1 - 2)(1 - 4)} = \frac{(x - 2)(x - 4)}{(-1)(-3)} = \frac{(x - 2)(x - 4)}{3}$
- $L_1(x) = \frac{(x - 1)(x - 4)}{(2 - 1)(2 - 4)} = \frac{(x - 1)(x - 4)}{(1)(-2)} = -\frac{(x - 1)(x - 4)}{2}$
- $L_2(x) = \frac{(x - 1)(x - 2)}{(4 - 1)(4 - 2)} = \frac{(x - 1)(x - 2)}{(3)(2)} = \frac{(x - 1)(x - 2)}{6}$
- $P_2(x) = 3 \cdot L_0(x) + 5 \cdot L_1(x) + 11 \cdot L_2(x)$
- $P_2(x) = 3 \cdot \frac{(x - 2)(x - 4)}{3} + 5 \cdot \left[-\frac{(x - 1)(x - 4)}{2}\right] + 11 \cdot \frac{(x - 1)(x - 2)}{6}$
- $P_2(x) = (x - 2)(x - 4) - \frac{5}{2}(x - 1)(x - 4) + \frac{11}{6}(x - 1)(x - 2)$
- Expanding: $P_2(x) = (x^2 - 6x + 8) - \frac{5}{2}(x^2 - 5x + 4) + \frac{11}{6}(x^2 - 3x + 2)$
- $P_2(x) = x^2 - 6x + 8 - \frac{5}{2}x^2 + \frac{25}{2}x - 10 + \frac{11}{6}x^2 - \frac{33}{6}x + \frac{22}{6}$
- Combine like terms: $P_2(x) = \left(1 - \frac{5}{2} + \frac{11}{6}\right)x^2 + \left(-6 + \frac{25}{2} - \frac{33}{6}\right)x + \left(8 - 10 + \frac{22}{6}\right)$
- $P_2(x) = \left(\frac{6 - 15 + 11}{6}\right)x^2 + \left(\frac{-36 + 75 - 33}{6}\right)x + \left(\frac{48 - 60 + 22}{6}\right)$
- $P_2(x) = \frac{2}{6}x^2 + \frac{6}{6}x + \frac{10}{6} = \frac{1}{3}x^2 + x + \frac{5}{3}$
Verification: $P_2(1) = \frac{1}{3} + 1 + \frac{5}{3} = \frac{1 + 3 + 5}{3} = 3$ ✓, $P_2(2) = \frac{4}{3} + 2 + \frac{5}{3} = \frac{4 + 6 + 5}{3} = 5$ ✓, $P_2(4) = \frac{16}{3} + 4 + \frac{5}{3} = \frac{16 + 12 + 5}{3} = 11$ ✓
Newton's Interpolation using Difference and Divided Differences
Newton's form of the interpolating polynomial uses divided differences, which provide a more efficient way to add new data points without recomputing the entire polynomial. The divided difference is defined recursively:
- ** Zeroth divided difference:** $f[x_i] = f(x_i)$ (the function value)
- ** First divided difference:** $f[x_i, x_{i+1}] = \frac{f(x_{i+1}) - f(x_i)}{x_{i+1} - x_i}$
- ** $k$-th divided difference:** $f[x_i, x_{i+1}, \ldots, x_{i+k}] = \frac{f[x_{i+1}, \ldots, x_{i+k}] - f[x_i, \ldots, x_{i+k-1}]}{x_{i+k} - x_i}$
Newton's Interpolating Polynomial:
$$P_n(x) = f[x_0] + f[x_0, x_1](x - x_0) + f[x_0, x_1, x_2](x - x_0)(x - x_1) + \cdots + f[x_0, x_1, \ldots, x_n](x - x_0)(x - x_1)\cdots(x - x_{n-1})$$
Advantages:
- Adding a new data point only requires computing one new divided difference
- The polynomial can be incrementally updated
- Computationally efficient for incremental data
Worked Example: Same data as before: $(1, 3), (2, 5), (4, 11)$.
- $f[x_0] = f[1] = 3$
- $f[x_1] = f[2] = 5$
- $f[x_2] = f[4] = 11$
- $f[x_0, x_1] = \frac{f[2] - f[1]}{2 - 1} = \frac{5 - 3}{1} = 2$
- $f[x_1, x_2] = \frac{f[4] - f[2]}{4 - 2} = \frac{11 - 5}{2} = 3$
- $f[x_0, x_1, x_2] = \frac{f[x_1, x_2] - f[x_0, x_1]}{4 - 1} = \frac{3 - 2}{3} = \frac{1}{3}$
- $P_2(x) = 3 + 2(x - 1) + \frac{1}{3}(x - 1)(x - 2)$
- This is equivalent to the Lagrange form derived earlier.
Cubic Spline Interpolation
Piecewise polynomial interpolation that uses cubic polynomials on each subinterval, ensuring continuity of the function, first derivative, and second derivative at the interior data points (knots). This avoids Runge's phenomenon and provides smooth interpolation even for large datasets.
Construction: For $n+1$ data points $(x_i, y_i)$ with $i = 0, 1, \ldots, n$, we construct $n$ cubic polynomials $S_i(x)$ on $[x_i, x_{i+1}]$:
$$S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3$$
Continuity Conditions:
- $S_i(x_i) = y_i$ and $S_i(x_{i+1}) = y_{i+1}$ (function continuity)
- $S_i'(x_{i+1}) = S_{i+1}'(x_{i+1})$ (first derivative continuity at knots)
- $S_i''(x_{i+1}) = S_{i+1}''(x_{i+1})$ (second derivative continuity at knots)
- Boundary conditions: natural spline ($S_0''(x_0) = S_{n-1}''(x_n) = 0$), clamped spline (specified first derivatives at endpoints), or not-a-knot condition
Solving for Coefficients: The continuity conditions lead to a tridiagonal system for the second derivatives $M_i = S_i''(x_i)$. Once $M_i$ are known, all other coefficients are determined by integration.
Worked Example: Construct a natural cubic spline for $(1, 3), (2, 5), (4, 11)$.
- 3 data points, 2 cubic segments: $S_0(x)$ on $[1, 2]$, $S_1(x)$ on $[2, 4]$
- Natural boundary: $S_0''(1) = 0$, $S_1''(4) = 0$
- Set up the tridiagonal system and solve for $M_0, M_1, M_2$ (where $M_i = S_i''(x_i)$)
- For natural spline with $n = 2$: $h_0 = x_1 - x_0 = 1$, $h_1 = x_2 - x_1 = 2$
- System: $\begin{bmatrix} 2(h_0 + h_1) & h_1 \ h_1 & 2h_1 \end{bmatrix} \begin{bmatrix} M_1 \ M_2 \end{bmatrix} = \begin{bmatrix} 6\left(\frac{y_2 - y_1}{h_1} - \frac{y_1 - y_0}{h_0}\right) \ 6\left(\frac{y_3 - y_2}{h_2} - \frac{y_2 - y_1}{h_1}\right) \end{bmatrix}$
- But with only 2 segments and natural boundary, $M_0 = 0$ and $M_2 = 0$, leaving one equation for $M_1$: $h_1 M_1 = 6\left(\frac{y_2 - y_1}{h_1} - \frac{y_1 - y_0}{h_0}\right)$ $2 M_1 = 6\left(\frac{11 - 5}{2} - \frac{5 - 3}{1}\right) = 6\left(3 - 2\right) = 6$ $M_1 = 3$
- Then compute coefficients for each segment and evaluate at desired points.
Least Squares Method for Linear and Non-linear Data
When data contains measurement error or is overdetermined (more points than polynomial degree), exact interpolation is not appropriate. The least squares method finds the polynomial (or other function) that minimizes the sum of squared residuals.
Linear Least Squares: Fit a line $y = ax + b$ to $m$ data points $(x_i, y_i)$ by minimizing:
$$S(a, b) = \sum_{i=1}^{m} (y_i - (ax_i + b))^2$$
Setting $\frac{\partial S}{\partial a} = 0$ and $\frac{\partial S}{\partial b} = 0$ gives the normal equations:
$$\begin{cases} a \sum x_i^2 + b \sum x_i = \sum x_i y_i \ a \sum x_i + b m = \sum y_i \end{cases}$$
Solving this $2 \times 2$ system yields the least squares estimates $\hat{a}$ and $\hat{b}$.
Non-linear Least Squares: For models like $y = a e^{bx}$ or $y = a x^b$, the problem is nonlinear in the parameters. Iterative methods (Gauss-Newton, Levenberg-Marquardt) are used to minimize the sum of squared residuals.
Worked Example: Fit $y = ax + b$ to data $(1, 2), (2, 3), (3, 5)$ using least squares.
- $n = 3$ points, $\sum x_i = 1 + 2 + 3 = 6$, $\sum y_i = 2 + 3 + 5 = 10$
- $\sum x_i^2 = 1 + 4 + 9 = 14$, $\sum x_i y_i = 1\cdot2 + 2\cdot3 + 3\cdot5 = 2 + 6 + 15 = 23$
- Normal equations: $\begin{cases} 14a + 6b = 23 \ 6a + 3b = 10 \end{cases}$
- Solve: From second equation, $b = \frac{10 - 6a}{3}$. Substitute into first: $14a + 6\left(\frac{10 - 6a}{3}\right) = 23 \implies 14a + 2(10 - 6a) = 23 \implies 14a + 20 - 12a = 23 \implies 2a = 3 \implies a = 1.5$
- $b = \frac{10 - 6(1.5)}{3} = \frac{10 - 9}{3} = \frac{1}{3}$
- Best-fit line: $y = 1.5x + \frac{1}{3}$
Check residuals: $r_1 = 2 - (1.5\cdot1 + 1/3) = 2 - 1.833 = 0.167$, $r_2 = 3 - (1.5\cdot2 + 1/3) = 3 - 3.167 = -0.167$, $r_3 = 5 - (1.5\cdot3 + 1/3) = 5 - 4.833 = 0.167$
- Sum of squared residuals: $0.167^2 + (-0.167)^2 + 0.167^2 = 3 \times 0.0278 \approx 0.0833$
Unit 3: Numerical Differentiation and Integration
Teaching Hours: 5
Introduction to Numerical Differentiation
Numerical differentiation approximates the derivative of a function using discrete function values. Unlike symbolic differentiation, which produces an exact algebraic expression, numerical differentiation introduces truncation error and is sensitive to round-off error, especially when the step size $h$ is very small.
Newton's Differentiation Formulas
The most common finite-difference approximations use Taylor series expansions. For a function $f(x)$ at points $x$ and $x + h$:
-
Forward difference (first order): $$f'(x) \approx \frac{f(x + h) - f(x)}{h} + O(h)$$ The error term is proportional to $h$. Simple to implement but less accurate.
-
Backward difference (first order): $$f'(x) \approx \frac{f(x) - f(x - h)}{h} + O(h)$$ Uses function values at $x$ and a point behind $x$. Useful when forward points are unavailable.
-
Central difference (second order): $$f'(x) \approx \frac{f(x + h) - f(x - h)}{2h} + O(h^2)$$ The error term is proportional to $h^2$, making it more accurate than forward/backward difference for the same step size. Requires function values on both sides of $x$.
-
Second derivative via central difference: $$f''(x) \approx \frac{f(x + h) - 2f(x) + f(x - h)}{h^2} + O(h^2)$$
Worked Example: Approximate $f'(1)$ for $f(x) = \ln x$ using $h = 0.01$.
- Exact: $f'(1) = \frac{1}{1} = 1$
- Forward difference: $\frac{\ln(1.01) - \ln(1)}{0.01} = \frac{0.00995033}{0.01} = 0.995033$. Error = $|1 - 0.995033| = 0.004967$
- Backward difference: $\frac{\ln(1) - \ln(0.99)}{0.01} = \frac{0 - (-0.01005033)}{0.01} = 1.005033$. Error = $0.005033$
- Central difference: $\frac{\ln(1.01) - \ln(0.99)}{2 \cdot 0.01} = \frac{0.00995033 - (-0.01005033)}{0.02} = \frac{0.02000066}{0.02} = 1.000033$. Error = $3.3 \times 10^{-5}$
The central difference provides dramatically better accuracy (error ~$10^{-5}$ vs ~$10^{-3}$) despite using the same step size.
Numerical Integration (Trapezoidal Rule)
The trapezoidal rule approximates the definite integral $\int_a^b f(x) , dx$ by approximating the region under the graph as a trapezoid.
Composite Trapezoidal Rule: For $n$ subintervals of equal width $h = \frac{b - a}{n}$:
$$\int_a^b f(x) , dx \approx \frac{h}{2} \left[f(x_0) + 2f(x_1) + 2f(x_2) + \cdots + 2f(x_{n-1}) + f(x_n)\right]$$
where $x_i = a + i h$ for $i = 0, 1, \ldots, n$.
Error Term: The global error for the composite trapezoidal rule is $E = -\frac{(b - a)}{12} h^2 f''(\xi)$ for some $\xi \in [a, b]$. The error is $O(h^2)$, meaning doubling $n$ (halving $h$) reduces the error by a factor of 4.
Worked Example: Approximate $\int_0^1 e^x , dx$ using the composite trapezoidal rule with $n = 4$.
- $h = \frac{1 - 0}{4} = 0.25$, $x_i = 0, 0.25, 0.5, 0.75, 1$
- $f(x_i) = e^{x_i}$: $f(0) = 1$, $f(0.25) = e^{0.25} \approx 1.284025$, $f(0.5) = e^{0.5} \approx 1.648721$, $f(0.75) = e^{0.75} \approx 2.117000$, $f(1) = e^1 \approx 2.718282$
- Composite trapezoidal: $\frac{0.25}{2} \left[1 + 2(1.284025) + 2(1.648721) + 2(2.117000) + 2.718282\right]$
- $= 0.125 \left[1 + 2.56805 + 3.297442 + 4.334 + 2.718282\right] = 0.125 \left[13.917774\right] \approx 1.739722$
- Exact: $\int_0^1 e^x , dx = e - 1 \approx 1.718282$
- Error $\approx 1.739722 - 1.718282 = 0.021440$
Simpson's 1/3 rule with $n = 4$ would give a more accurate result (error $O(h^4)$ vs $O(h^2)$).
Simpson's 1/3 Rule
Simpson's rule approximates the integral by fitting a parabola through three consecutive points. It requires an even number of subintervals.
Composite Simpson's 1/3 Rule: For $n$ even subintervals ($n$ must be even), $h = \frac{b - a}{n}$:
$$\int_a^b f(x) , dx \approx \frac{h}{3} \left[f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + 2f(x_4) + \cdots + 4f(x_{n-1}) + f(x_n)\right]$$
The coefficients follow the pattern: $1, 4, 2, 4, 2, \ldots, 4, 1$.
Error Term: The global error is $E = -\frac{(b - a)}{180} h^4 f^{(4)}(\xi)$ for some $\xi \in [a, b]$. The error is $O(h^4)$, making Simpson's rule much more accurate than the trapezoidal rule for smooth functions.
Worked Example: Same integral $\int_0^1 e^x , dx$ with $n = 4$.
- $h = 0.25$, same $f(x_i)$ as before
- Simpson's 1/3: $\frac{0.25}{3} \left[1 + 4(1.284025) + 2(1.648721) + 4(2.117000) + 2.718282\right]$
- $= \frac{0.25}{3} \left[1 + 5.1361 + 3.297442 + 8.468 + 2.718282\right] = \frac{0.25}{3} \left[20.619824\right] \approx 1.718319$
- Error $\approx 1.718319 - 1.718282 = 3.7 \times 10^{-5}$ (much better than trapezoidal!)
3/8 Rule
The 3/8 rule is an alternative to Simpson's 1/3 rule, using a cubic polynomial through four points. It requires $n$ to be a multiple of 3.
Composite 3/8 Rule: For $n$ a multiple of 3, $h = \frac{b - a}{n}$:
$$\int_a^b f(x) , dx \approx \frac{3h}{8} \left[f(x_0) + 3f(x_1) + 3f(x_2) + f(x_3) + 3f(x_4) + 3f(x_5) + f(x_6) + \cdots + 3f(x_{n-1}) + 3f(x_{n-2}) + f(x_n)\right]$$
Error Term: $E = -\frac{(b - a)}{648} h^4 f^{(4)}(\xi)$. The error is also $O(h^4)$, similar to Simpson's 1/3 rule but with different coefficient weights.
Romberg Integration
Romberg integration applies Richardson extrapolation to the trapezoidal rule to accelerate convergence and obtain higher-order accuracy.
Romberg Table: Let $T_{n}$ be the composite trapezoidal rule with $n$ subintervals. Then:
- $R_{1,1} = T_1$
- $R_{2,1} = T_2$ (trapezoidal with $2n$ subintervals)
- $R_{3,1} = T_4$, etc.
Richardson extrapolation formula: $$R_{k,j} = \frac{4^{j-1} R_{k+1, j-1} - R_{k, j-1}}{4^{j-1} - 1}$$
The Romberg table has the form: $$\begin{array}{c|cccc} & j=1 & j=2 & j=3 & \cdots \ \hline k=1 & R_{1,1} & R_{1,2} & R_{1,3} & \cdots \ k=2 & R_{2,1} & R_{2,2} & R_{2,3} & \cdots \ k=3 & R_{3,1} & R_{3,2} & R_{3,3} & \cdots \ \vdots & \vdots & \vdots & \vdots & \ddots \ \end{array}$$
The diagonal elements $R_{k,k}$ converge to the integral at higher order. In practice, $R_{n,n}$ provides an $O(h^{2n})$ approximation.
Worked Example: Compute $\int_0^1 e^x , dx$ using Romberg integration.
- $T_1$ (one subinterval): $h = 1$, $\frac{1}{2}(f(0) + f(1)) = \frac{1}{2}(1 + 2.718282) = 1.859141$
- $T_2$ (two subintervals): $h = 0.5$, $\frac{0.5}{2}(f(0) + 2f(0.5) + f(1)) = 0.25(1 + 2(1.648721) + 2.718282) = 0.25(7.015724) = 1.753931$
- $T_4$ (four subintervals): $h = 0.25$, $\frac{0.25}{2}[f(0) + 2f(0.25) + 2f(0.5) + 2f(0.75) + f(1)] = 0.125[1 + 2(1.284025) + 2(1.648721) + 2(2.117000) + 2.718282] = 0.125(13.817775) = 1.727222$
- Romberg table:
- $R_{1,1} = T_1 = 1.859141$
- $R_{2,1} = T_2 = 1.753931$
- $R_{3,1} = T_4 = 1.727222$
- $R_{2,2} = \frac{4 R_{2,1} - R_{1,1}}{3} = \frac{4(1.753931) - 1.859141}{3} = \frac{5.156583}{3} = 1.718861$
- $R_{3,2} = \frac{4 R_{3,1} - R_{2,1}}{3} = \frac{4(1.727222) - 1.753931}{3} = \frac{5.154957}{3} = 1.718319$
- $R_{3,3} = \frac{16 R_{3,2} - R_{2,2}}{15} = \frac{16(1.718319) - 1.718861}{15} = \frac{25.774243}{15} = 1.718283$
- Exact: $\int_0^1 e^x , dx = e - 1 \approx 1.718282$. The diagonal entry $R_{3,3} = 1.718283$ matches to 6 decimal places, showing how Richardson extrapolation accelerates the trapezoidal rule from $O(h^2)$ to higher order.
Numerical Double Integration
Double integrals $\iint_R f(x, y) , dA$ over a region $R$ can be computed using iterated (repeated) integration:
$$\int_a^b \left[\int_c^d f(x, y) , dy\right] dx = \int_c^d \left[\int_a^b f(x, y) , dx\right] dy$$
Approach: Apply a single-quadrature rule (trapezoidal or Simpson's) to the inner integral, then apply another rule to the outer integral. For example, using the composite trapezoidal rule for both dimensions with $m$ and $n$ subintervals:
$$\iint_R f(x, y) , dx , dy \approx \frac{h k}{4} \sum_{i=0}^{m} \sum_{j=0}^{n} f(x_i, y_j) \cdot w_i w_j$$
where $w_0 = w_n = 1$ and $w_i = 2$ for interior points, with $h = \frac{b - a}{m}$, $k = \frac{d - c}{n}$, $x_i = a + i h$, $y_j = c + j k$.
Worked Example: Approximate $\int_0^1 \int_0^1 (x + y) , dy , dx$ using the composite trapezoidal rule with $n = m = 1$ (one subinterval each direction).
- $h = 1$, $k = 1$, $x_0 = 0$, $x_1 = 1$, $y_0 = 0$, $y_1 = 1$
- Weights: $w_0 = w_1 = 1$
- Approximation: $\frac{1 \cdot 1}{4} \sum_{i=0}^{1} \sum_{j=0}^{1} f(x_i, y_j) \cdot w_i w_j = \frac{1}{4} \left[f(0,0)\cdot1\cdot1 + f(0,1)\cdot1\cdot1 + f(1,0)\cdot1\cdot1 + f(1,1)\cdot1\cdot1\right]$
- $f(x, y) = x + y$: $f(0, 0) = 0$, $f(0, 1) = 1$, $f(1, 0) = 1$, $f(1, 1) = 2$
- $\frac{1}{4} (0 + 1 + 1 + 2) = \frac{4}{4} = 1$
- Exact: $\int_0^1 \int_0^1 (x + y) , dy , dx = \int_0^1 \left[xy + \frac{y^2}{2}\right]_{y=0}^{y=1} dx = \int_0^1 \left(x + \frac{1}{2}\right) dx = \left[\frac{x^2}{2} + \frac{x}{2}\right]_0^1 = \frac{1}{2} + \frac{1}{2} = 1$
With just 1 subinterval per direction, we get the exact answer for this linear function. More subintervals would be needed for nonlinear functions.
Unit 4: Solution of Linear Algebraic Equations
Teaching Hours: 10
Review of the Existence of Solutions and Properties of Matrices
A system of linear algebraic equations can be written in matrix form $A\mathbf{x} = \mathbf{b}$, where $A$ is an $m \times n$ matrix of coefficients, $\mathbf{x}$ is an $n \times 1$ column vector of unknowns, and $\mathbf{b}$ is an $m \times 1$ column vector of constants.
Existence and Uniqueness:
- If $m = n$ (square system) and $\det(A) \neq 0$, a unique solution exists.
- If $m = n$ and $\det(A) = 0$, the system may have either no solution (inconsistent) or infinitely many solutions (dependent), depending on $\mathbf{b}$.
- If $m > n$ (overdetermined), the system may have no exact solution; a least-squares solution may be sought.
- If $m < n$ (underdetermined), there are infinitely many solutions; additional constraints are needed.
Matrix Properties:
- Row echelon form: All nonzero rows are above zero rows, and the leading coefficient (pivot) of a nonzero row is always strictly to the right of the leading coefficient of the row above it.
- Reduced row echelon form: Row echelon form plus each leading coefficient is 1 and is the only nonzero entry in its column.
- Rank: The rank of a matrix is the number of linearly independent rows (or columns). For $A\mathbf{x} = \mathbf{b}$, a solution exists iff $\text{rank}(A) = \text{rank}([A|\mathbf{b}])$ (augmented matrix).
Consistency of a Linear System of Equations
The system $A\mathbf{x} = \mathbf{b}$ is consistent (has at least one solution) if and only if $\mathbf{b}$ is in the column space of $A$. Using Gaussian elimination on the augmented matrix $[A|\mathbf{b}]$, the system is consistent if there is no row of the form $[0\ 0\ \cdots\ 0\ |\ c]$ with $c \neq 0$.
Worked Example: Determine the consistency of the system: $$\begin{cases} x + y = 2 \ 2x + 2y = 4 \end{cases}$$
- Augmented matrix: $\begin{bmatrix} 1 & 1 & | & 2 \ 2 & 2 & | & 4 \end{bmatrix}$
- Row operation: $R_2 \leftarrow R_2 - 2R_1$: $\begin{bmatrix} 1 & 1 & | & 2 \ 0 & 0 & | & 0 \end{bmatrix}$
- The second row represents $0 = 0$, which is always true. The system is consistent.
- Infinitely many solutions: $y$ is free, $x = 2 - y$.
Gaussian Elimination Method
Gaussian elimination transforms the system into upper triangular form (row echelon form) using elementary row operations, then solves by back substitution.
Elementary Row Operations:
- Swap two rows ($R_i \leftrightarrow R_j$)
- Multiply a row by a nonzero scalar ($k R_i$)
- Add a multiple of one row to another ($R_i + k R_j \rightarrow R_i$)
Algorithm:
- Form the augmented matrix $[A|\mathbf{b}]$
- For $k = 1$ to $n-1$ (pivot row):
a. (Optional) Partial pivoting: swap row $k$ with a row $i > k$ such that $|a_{ik}|$ is maximized in column $k$. This improves numerical stability.
b. For $i = k+1$ to $n$:
- Compute the multiplier $m_{ik} = \frac{a_{ik}}{a_{kk}}$
- For $j = k$ to $n+1$: $a_{ij} \leftarrow a_{ij} - m_{ik} a_{kj}$ (eliminate below the pivot)
- Back substitution: starting from the last equation, solve for $x_n, x_{n-1}, \ldots, x_1$.
Worked Example: Solve $\begin{cases} 2x + y - z = 8 \ -3x - y + 2z = -11 \ -2x + y + 2z = -3 \end{cases}$
-
Augmented matrix: $\begin{bmatrix} 2 & 1 & -1 & | & 8 \ -3 & -1 & 2 & | & -11 \ -2 & 1 & 2 & | & -3 \end{bmatrix}$
-
$k = 1$ (pivot on $a_{11} = 2$):
- No pivoting needed (2 is the largest in column 1 among rows 1-3)
- $m_{21} = \frac{-3}{2} = -1.5$: $R_2 \leftarrow R_2 - (-1.5)R_1 = R_2 + 1.5R_1$
- New $R_2$: $(-3 + 1.5\cdot2, -1 + 1.5\cdot1, 2 + 1.5\cdot(-1), -11 + 1.5\cdot8) = (0, 0.5, 0.5, 0.5)$
- $m_{31} = \frac{-2}{2} = -1$: $R_3 \leftarrow R_3 - (-1)R_1 = R_3 + R_1$
- New $R_3$: $(-2 + 2, 1 + 1, 2 + (-1), -3 + 8) = (0, 2, 1, 5)$
-
Matrix now: $\begin{bmatrix} 2 & 1 & -1 & | & 8 \ 0 & 0.5 & 0.5 & | & 0.5 \ 0 & 2 & 1 & | & 5 \end{bmatrix}$
-
$k = 2$ (pivot on $a_{22} = 0.5$):
- Partial pivoting: check if $|a_{32}| = 2 > |a_{22}| = 0.5$, so swap rows 2 and 3
- After swap: $\begin{bmatrix} 2 & 1 & -1 & | & 8 \ 0 & 2 & 1 & | & 5 \ 0 & 0.5 & 0.5 & | & 0.5 \end{bmatrix}$
- $m_{32} = \frac{0.5}{2} = 0.25$: $R_3 \leftarrow R_3 - 0.25R_2$
- New $R_3$: $(0, 0.5 - 0.25\cdot2, 0.5 - 0.25\cdot1, 0.5 - 0.25\cdot5) = (0, 0, 0.25, -0.75)$
-
Upper triangular form: $\begin{bmatrix} 2 & 1 & -1 & | & 8 \ 0 & 2 & 1 & | & 5 \ 0 & 0 & 0.25 & | & -0.75 \end{bmatrix}$
-
Back substitution:
- From row 3: $0.25z = -0.75 \implies z = -3$
- From row 2: $2y + z = 5 \implies 2y + (-3) = 5 \implies 2y = 8 \implies y = 4$
- From row 1: $2x + y - z = 8 \implies 2x + 4 - (-3) = 8 \implies 2x + 7 = 8 \implies 2x = 1 \implies x = 0.5$
-
Solution: $x = 0.5, y = 4, z = -3$
Gauss-Jordan Method
Gauss-Jordan elimination extends Gaussian elimination by continuing row operations until the augmented matrix is in reduced row echelon form (RREF), where each leading coefficient is 1 and is the only nonzero entry in its column. This directly yields the solution without back substitution.
Algorithm:
- Form the augmented matrix $[A|\mathbf{b}]$
- Transform to row echelon form (as in Gaussian elimination)
- For each pivot column $k$ from 1 to $n$: a. Scale the pivot row so the leading coefficient is 1: $R_k \leftarrow \frac{1}{a_{kk}} R_k$ b. Eliminate all other entries in the pivot column: for $i \neq k$, $R_i \leftarrow R_i - a_{ik} R_k$
- The solution appears directly in the augmented column.
Worked Example: Same system as before. After Gaussian elimination, we had: $\begin{bmatrix} 2 & 1 & -1 & | & 8 \ 0 & 2 & 1 & | & 5 \ 0 & 0 & 0.25 & | & -0.75 \end{bmatrix}$
-
Gauss-Jordan continues:
- Scale row 3: $R_3 \leftarrow \frac{1}{0.25} R_3 = 4 R_3 \implies \begin{bmatrix} 0 & 0 & 1 & | & -3 \end{bmatrix}$
- Eliminate above: $R_2 \leftarrow R_2 - 1\cdot R_3 \implies \begin{bmatrix} 0 & 2 & 0 & | & 8 \end{bmatrix}$, $R_1 \leftarrow R_1 - (-1)\cdot R_3 = R_1 + R_3 \implies \begin{bmatrix} 2 & 1 & 0 & | & 5 \end{bmatrix}$
- Scale row 2: $R_2 \leftarrow \frac{1}{2} R_2 \implies \begin{bmatrix} 0 & 1 & 0 & | & 4 \end{bmatrix}$
- Eliminate above: $R_1 \leftarrow R_1 - 1\cdot R_2 \implies \begin{bmatrix} 2 & 0 & 0 & | & 1 \end{bmatrix}$
- Scale row 1: $R_1 \leftarrow \frac{1}{2} R_1 \implies \begin{bmatrix} 1 & 0 & 0 & | & 0.5 \end{bmatrix}$
-
RREF: $\begin{bmatrix} 1 & 0 & 0 & | & 0.5 \ 0 & 1 & 0 & | & 4 \ 0 & 0 & 1 & | & -3 \end{bmatrix}$
-
Solution: $x = 0.5, y = 4, z = -3$ (same as before, but obtained directly)
Method of Factorization (LU Decomposition)
LU decomposition factors a matrix $A$ into the product of a lower triangular matrix $L$ and an upper triangular matrix $U$: $A = LU$. This allows efficient solution of multiple systems with the same coefficient matrix but different right-hand sides.
Algorithm:
- Decompose $A = LU$, where $L$ has 1s on the diagonal (unit lower triangular) and $U$ is upper triangular.
- Solve $L\mathbf{y} = \mathbf{b}$ by forward substitution.
- Solve $U\mathbf{x} = \mathbf{y}$ by back substitution.
Doolittle's Method: $L$ has 1s on the diagonal, and the decomposition is computed without row pivoting (or with partial pivoting using a permutation matrix $PA = LU$).
Worked Example: LU decomposition of $A = \begin{bmatrix} 2 & 1 \ 4 & 3 \end{bmatrix}$.
- We want $L = \begin{bmatrix} 1 & 0 \ l_{21} & 1 \end{bmatrix}$, $U = \begin{bmatrix} u_{11} & u_{12} \ 0 & u_{22} \end{bmatrix}$ such that $A = LU$.
- $LU = \begin{bmatrix} 1 & 0 \ l_{21} & 1 \end{bmatrix} \begin{bmatrix} u_{11} & u_{12} \ 0 & u_{22} \end{bmatrix} = \begin{bmatrix} u_{11} & u_{12} \ l_{21}u_{11} & l_{21}u_{12} + u_{22} \end{bmatrix} = \begin{bmatrix} 2 & 1 \ 4 & 3 \end{bmatrix}$
- Equating entries:
- $u_{11} = 2$
- $u_{12} = 1$
- $l_{21}u_{11} = 4 \implies l_{21}\cdot2 = 4 \implies l_{21} = 2$
- $l_{21}u_{12} + u_{22} = 3 \implies 2\cdot1 + u_{22} = 3 \implies u_{22} = 1$
- Thus $L = \begin{bmatrix} 1 & 0 \ 2 & 1 \end{bmatrix}$, $U = \begin{bmatrix} 2 & 1 \ 0 & 1 \end{bmatrix}$
- Verify: $\begin{bmatrix} 1 & 0 \ 2 & 1 \end{bmatrix} \begin{bmatrix} 2 & 1 \ 0 & 1 \end{bmatrix} = \begin{bmatrix} 2 & 1 \ 4 & 2 + 1 \end{bmatrix} = \begin{bmatrix} 2 & 1 \ 4 & 3 \end{bmatrix}$ ✓
To solve $A\mathbf{x} = \mathbf{b}$ with $\mathbf{b} = \begin{bmatrix} 5 \ 11 \end{bmatrix}$:
- $L\mathbf{y} = \mathbf{b} \implies \begin{bmatrix} 1 & 0 \ 2 & 1 \end{bmatrix} \begin{bmatrix} y_1 \ y_2 \end{bmatrix} = \begin{bmatrix} 5 \ 11 \end{bmatrix}$
- $y_1 = 5$
- $2y_1 + y_2 = 11 \implies 2\cdot5 + y_2 = 11 \implies y_2 = 1$
- $U\mathbf{x} = \mathbf{y} \implies \begin{bmatrix} 2 & 1 \ 0 & 1 \end{bmatrix} \begin{bmatrix} x_1 \ x_2 \end{bmatrix} = \begin{bmatrix} 5 \ 1 \end{bmatrix}$
- $x_2 = 1$
- $2x_1 + x_2 = 5 \implies 2x_1 + 1 = 5 \implies x_1 = 2$
- Solution: $x = \begin{bmatrix} 2 \ 1 \end{bmatrix}$
Iterative Methods (Jacobi & Gauss-Seidel Iteration)
Iterative methods are useful for large systems where direct methods (Gaussian elimination) are computationally expensive. They generate a sequence of approximations $\mathbf{x}^{(0)}, \mathbf{x}^{(1)}, \mathbf{x}^{(2)}, \ldots$ that ideally converge to the true solution.
Jacobi Iteration:
Decompose $A = D - L - U$, where $D$ is the diagonal, $-L$ is the strictly lower triangular part, and $-U$ is the strictly upper triangular part. The Jacobi iteration is:
$$\mathbf{x}^{(k+1)} = D^{-1}(\mathbf{b} - (L + U)\mathbf{x}^{(k)})$$
Component form (for $i = 1, \ldots, n$): $$x_i^{(k+1)} = \frac{1}{a_{ii}}\left[b_i - \sum_{j \neq i} a_{ij} x_j^{(k)}\right]$$
Gauss-Seidel Iteration:
Uses the most recently updated values, exploiting the decomposition $A = D - L - U$ but keeping the lower-triangular part updated immediately:
$$x_i^{(k+1)} = \frac{1}{a_{ii}}\left[b_i - \sum_{j < i} a_{ij} x_j^{(k+1)} - \sum_{j > i} a_{ij} x_j^{(k)}\right]$$
Convergence: Both methods converge if $A$ is strictly diagonally dominant ( $|a_{ii}| > \sum_{j \neq i} |a_{ij}| $ for all $i $) or symmetric positive definite. Gauss-Seidel typically converges faster than Jacobi because it uses updated values immediately.
Worked Example: Solve $\begin{cases} 4x + y = 5 \ x + 3y = 5 \end{cases}$ using Jacobi iteration with initial guess $(x^{(0)}, y^{(0)}) = (0, 0)$ and tolerance $10^{-3}$.
-
$A = \begin{bmatrix} 4 & 1 \ 1 & 3 \end{bmatrix}$, $\mathbf{b} = \begin{bmatrix} 5 \ 5 \end{bmatrix}$, $D = \begin{bmatrix} 4 & 0 \ 0 & 3 \end{bmatrix}$, $L+U = \begin{bmatrix} 0 & 1 \ 1 & 0 \end{bmatrix}$
-
Jacobi iteration formulas:
- $x^{(k+1)} = \frac{1}{4}(5 - y^{(k)})$
- $y^{(k+1)} = \frac{1}{3}(5 - x^{(k)})$
-
Iteration 1: $x^{(1)} = \frac{1}{4}(5 - 0) = 1.25$, $y^{(1)} = \frac{1}{3}(5 - 0) = 1.667$
-
Iteration 2: $x^{(2)} = \frac{1}{4}(5 - 1.667) = \frac{3.333}{4} = 0.833$, $y^{(2)} = \frac{1}{3}(5 - 1.25) = \frac{3.75}{3} = 1.250$
-
Iteration 3: $x^{(3)} = \frac{1}{4}(5 - 1.250) = \frac{3.75}{4} = 0.9375$, $y^{(3)} = \frac{1}{3}(5 - 0.833) = \frac{4.167}{3} = 1.389$
-
Iteration 4: $x^{(4)} = \frac{1}{4}(5 - 1.389) = \frac{3.611}{4} = 0.903$, $y^{(4)} = \frac{1}{3}(5 - 0.9375) = \frac{4.0625}{3} = 1.354$
-
Iteration 5: $x^{(5)} = \frac{1}{4}(5 - 1.354) = \frac{3.646}{4} = 0.912$, $y^{(5)} = \frac{1}{3}(5 - 0.912) = \frac{4.088}{3} = 1.363$
-
The exact solution is $x = 1, y = 1$. Jacobi converges slowly (linear rate), but the iterations are approaching the true values.
Now with Gauss-Seidel (using updated values immediately):
-
Gauss-Seidel formulas:
- $x^{(k+1)} = \frac{1}{4}(5 - y^{(k)})$ (same as Jacobi for $x$, since $x$ equation has no $x$ on the right)
- Actually, for Gauss-Seidel: $y^{(k+1)} = \frac{1}{3}(5 - x^{(k+1)})$ uses the new $x^{(k+1)}$
-
Iteration 1: $x^{(1)} = \frac{1}{4}(5 - 0) = 1.25$, then $y^{(1)} = \frac{1}{3}(5 - 1.25) = \frac{3.75}{3} = 1.25$
-
Iteration 2: $x^{(2)} = \frac{1}{4}(5 - 1.25) = 0.9375$, $y^{(2)} = \frac{1}{3}(5 - 0.9375) = 1.354$
-
Iteration 3: $x^{(3)} = \frac{1}{4}(5 - 1.354) = 0.911$, $y^{(3)} = \frac{1}{3}(5 - 0.911) = 1.363$
-
Gauss-Seidel converges faster than Jacobi, as expected.
Power Method
The power method computes the dominant eigenvalue (the eigenvalue with largest magnitude) and its corresponding eigenvector of a square matrix $A$.
Algorithm:
- Start with a random initial vector $\mathbf{b}_0 \neq \mathbf{0}$
- Iterate: $\mathbf{b}_{k+1} = A \mathbf{b}_k$
- Normalize: $\mathbf{b}{k+1} \leftarrow \frac{\mathbf{b}{k+1}}{|\mathbf{b}_{k+1}|}$ (e.g., divide by the largest component or by the Euclidean norm)
- Repeat steps 2-3 until convergence
- The dominant eigenvalue can be approximated using the Rayleigh quotient: $$\lambda \approx \frac{\mathbf{b}_k^T A \mathbf{b}_k}{\mathbf{b}_k^T \mathbf{b}_k}$$
Convergence: The method converges if $A$ has a unique dominant eigenvalue (i.e., $|\lambda_1| > |\lambda_2| \geq \cdots \geq |\lambda_n|$). The rate of convergence depends on the ratio $|\lambda_2 / \lambda_1|$. If this ratio is close to 1, convergence is slow.
Worked Example: Find the dominant eigenvalue and eigenvector of $A = \begin{bmatrix} 3 & 1 \ 1 & 3 \end{bmatrix}$ using the power method.
-
Eigenvalues of $A$: $\det(A - \lambda I) = \begin{vmatrix} 3-\lambda & 1 \ 1 & 3-\lambda \end{vmatrix} = (3-\lambda)^2 - 1 = \lambda^2 - 6\lambda + 8 = 0$
-
$\lambda = \frac{6 \pm \sqrt{36 - 32}}{2} = \frac{6 \pm 2}{2} = 4, 2$
-
Dominant eigenvalue: $\lambda_1 = 4$, other eigenvalue: $\lambda_2 = 2$, ratio $|\lambda_2 / \lambda_1| = 0.5$
-
Power method iteration:
- Start with $\mathbf{b}_0 = \begin{bmatrix} 1 \ 1 \end{bmatrix}$
- Iteration 1: $\mathbf{b}_1 = A \mathbf{b}_0 = \begin{bmatrix} 3 & 1 \ 1 & 3 \end{bmatrix} \begin{bmatrix} 1 \ 1 \end{bmatrix} = \begin{bmatrix} 4 \ 4 \end{bmatrix}$. Normalize: $\frac{1}{4}\begin{bmatrix} 4 \ 4 \end{bmatrix} = \begin{bmatrix} 1 \ 1 \end{bmatrix}$
- Iteration 2: $\mathbf{b}_2 = A \begin{bmatrix} 1 \ 1 \end{bmatrix} = \begin{bmatrix} 4 \ 4 \end{bmatrix}$. Same result! The initial vector was already an eigenvector.
- The eigenvalue is confirmed by $\lambda \mathbf{b} = A \mathbf{b}$: $4 \begin{bmatrix} 1 \ 1 \end{bmatrix} = \begin{bmatrix} 4 \ 4 \end{bmatrix}$ ✓
Let's try with a different starting vector: $\mathbf{b}_0 = \begin{bmatrix} 1 \ 0 \end{bmatrix}$
- Iteration 1: $\mathbf{b}_1 = A \begin{bmatrix} 1 \ 0 \end{bmatrix} = \begin{bmatrix} 3 \ 1 \end{bmatrix}$. Normalize (divide by largest component 3): $\begin{bmatrix} 1 \ 1/3 \end{bmatrix}$
- Iteration 2: $\mathbf{b}_2 = A \begin{bmatrix} 1 \ 1/3 \end{bmatrix} = \begin{bmatrix} 3 + 1/3 \ 1 + 1 \end{bmatrix} = \begin{bmatrix} 10/3 \ 2 \end{bmatrix}$. Normalize: divide by 10/3: $\begin{bmatrix} 1 \ 2 / (10/3) \end{bmatrix} = \begin{bmatrix} 1 \ 3/5 \end{bmatrix} = \begin{bmatrix} 1 \ 0.6 \end{bmatrix}$
- Iteration 3: $\mathbf{b}_3 = A \begin{bmatrix} 1 \ 0.6 \end{bmatrix} = \begin{bmatrix} 3 + 0.6 \ 1 + 1.8 \end{bmatrix} = \begin{bmatrix} 3.6 \ 2.8 \end{bmatrix}$. Normalize: divide by 3.6: $\begin{bmatrix} 1 \ 2.8/3.6 \end{bmatrix} = \begin{bmatrix} 1 \ 0.778 \end{bmatrix}$
- Iteration 4: $\mathbf{b}_4 = A \begin{bmatrix} 1 \ 0.778 \end{bmatrix} = \begin{bmatrix} 3 + 0.778 \ 1 + 2.334 \end{bmatrix} = \begin{bmatrix} 3.778 \ 3.334 \end{bmatrix}$. Normalize: divide by 3.778: $\begin{bmatrix} 1 \ 3.334/3.778 \end{bmatrix} = \begin{bmatrix} 1 \ 0.882 \end{bmatrix}$
- Iteration 5: $\mathbf{b}_5 = A \begin{bmatrix} 1 \ 0.882 \end{bmatrix} = \begin{bmatrix} 3 + 0.882 \ 1 + 2.646 \end{bmatrix} = \begin{bmatrix} 3.882 \ 3.646 \end{bmatrix}$. Normalize: $\begin{bmatrix} 1 \ 3.646/3.882 \end{bmatrix} = \begin{bmatrix} 1 \ 0.939 \end{bmatrix}$
- The eigenvector is converging to $\begin{bmatrix} 1 \ 1 \end{bmatrix}$ (the dominant eigenvector for $\lambda = 4$), and the eigenvalue estimate using the Rayleigh quotient would converge to 4.
Unit 5: Solution of Ordinary Differential Equations
Teaching Hours: 7
Introduction to Differential Equations
An ordinary differential equation (ODE) relates a function $y(x)$ to its derivatives. The order of the ODE is the highest derivative present. A first-order ODE has the form $F(x, y, y') = 0$, and a first-order initial value problem (IVP) adds a condition $y(x_0) = y_0$.
Classification:
- Linear vs. nonlinear: Linear ODE has the form $y' + p(x)y = q(x)$; nonlinear includes terms like $y^2, \sin y, y y'$, etc.
- Homogeneous vs. nonhomogeneous: Homogeneous has no term independent of $y$ (or its derivatives).
- Constant vs. variable coefficients: Constant coefficients ($a, b$ are numbers); variable coefficients ($p(x), q(x)$ are functions of $x$.
Worked Example: Classify $y' + y = e^x$ and $y' + y^2 = 0$.
- $y' + y = e^x$: linear, nonhomogeneous (due to $e^x$), constant coefficient (1).
- $y' + y^2 = 0$: nonlinear (due to $y^2$).
Initial Value Problem
An IVP specifies the value of the unknown function at a given point: $y(x_0) = y_0$. The goal is to find $y(x)$ on an interval containing $x_0$. Existence and uniqueness are guaranteed under Lipschitz conditions: if $f(x, y)$ is continuous and satisfies a Lipschitz condition in $y$, the IVP $y' = f(x, y), y(x_0) = y_0$ has a unique solution in some neighborhood of $x_0$.
Taylor Series Method
The Taylor series method approximates the solution of an IVP by expanding $y(x)$ in a Taylor series about $x_n$:
$$y(x_{n+1}) = y(x_n + h) = y(x_n) + h y'(x_n) + \frac{h^2}{2!} y''(x_n) + \frac{h^3}{3!} y^{(3)}(x_n) + \cdots$$
Algorithm:
- Given $y' = f(x, y)$ and initial condition $y(x_0) = y_0$
- Compute derivatives of $y$ analytically: $y' = f(x, y)$, $y'' = f_x(x, y) + f_y(x, y) y'$, etc.
- Evaluate at $x_n$: $y'(x_n) = f(x_n, y_n)$, $y''(x_n) = f_x(x_n, y_n) + f_y(x_n, y_n) f(x_n, y_n)$, etc.
- Take $h$ step: $y_{n+1} = y_n + h y'(x_n) + \frac{h^2}{2} y''(x_n) + \frac{h^3}{6} y^{(3)}(x_n) + \cdots$
Worked Example: Solve $y' = y, y(0) = 1$ from $x = 0$ to $x = 0.5$ using the Taylor series method of order 2 (including the $h^2$ term), with step size $h = 0.25$.
- $y' = f(x, y) = y$, so $y'' = y' = y$, $y^{(3)} = y$, etc. (all derivatives equal $y$).
- At $x_0 = 0, y_0 = 1$: $y'(0) = 1, y''(0) = 1, y^{(3)}(0) = 1$
- Step 1: $y_1 = y(0.25) \approx y_0 + h y'(0) + \frac{h^2}{2} y''(0) = 1 + 0.25\cdot1 + \frac{0.25^2}{2}\cdot1 = 1 + 0.25 + 0.03125 = 1.28125$
- Exact: $y(x) = e^x$, so $y(0.25) = e^{0.25} \approx 1.284025$. Error $\approx 0.002775$.
- Step 2: $y_2 = y(0.5) \approx y_1 + h y'(x_1) + \frac{h^2}{2} y''(x_1)$
- At $x_1 = 0.25, y_1 = 1.28125$: $y'(0.25) = 1.28125, y''(0.25) = 1.28125$
- $y_2 = 1.28125 + 0.25\cdot1.28125 + \frac{0.25^2}{2}\cdot1.28125 = 1.28125 + 0.3203125 + 0.040018 = 1.64158$
- Exact: $y(0.5) = e^{0.5} \approx 1.648721$. Error $\approx 0.00714$.
The Taylor series method requires computing higher derivatives analytically, which can be difficult for complex ODEs. However, it provides systematic error control by including more terms.
Picard's Method
Picard's method reformulates the IVP $y' = f(x, y), y(x_0) = y_0$ as an integral equation:
$$y(x) = y_0 + \int_{x_0}^{x} f(t, y(t)) , dt$$
The method generates a sequence of approximations $y_0(x), y_1(x), y_2(x), \ldots$ defined recursively:
$$y_0(x) = y_0$$ $$y_{n+1}(x) = y_0 + \int_{x_0}^{x} f(t, y_n(t)) , dt$$
Convergence: If $f$ is continuous and satisfies a Lipschitz condition, the Picard iterates converge uniformly to the unique solution of the IVP.
Worked Example: Solve $y' = y, y(0) = 1$ using Picard's method to find $y(0.2)$.
- $y_0(x) = 1$ (initial approximation)
- $y_1(x) = 1 + \int_0^x t^0 \cdot 1 , dt = 1 + \int_0^x 1 , dt = 1 + x$
- $y_2(x) = 1 + \int_0^x y_1(t) , dt = 1 + \int_0^x (1 + t) , dt = 1 + \left[t + \frac{t^2}{2}\right]_0^x = 1 + x + \frac{x^2}{2}$
- $y_3(x) = 1 + \int_0^x y_2(t) , dt = 1 + \int_0^x \left(1 + t + \frac{t^2}{2}\right) dt = 1 + \left[t + \frac{t^2}{2} + \frac{t^3}{6}\right]_0^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{6}$
This is recognizing the Taylor series for $e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots$. Picard's method systematically builds the Taylor series.
For $x = 0.2$:
- $y_3(0.2) = 1 + 0.2 + \frac{0.2^2}{2} + \frac{0.2^3}{6} = 1 + 0.2 + 0.02 + 0.001333 = 1.221333$
- Exact: $e^{0.2} \approx 1.221403$. Very close after just 3 iterations!
Euler's Method and Its Accuracy
Euler's method is the simplest one-step method for solving IVPs. It uses the tangent line approximation at each step.
Algorithm (Explicit Euler):
- Given $y' = f(x, y), y(x_0) = y_0$, and step size $h > 0$
- For $n = 0, 1, 2, \ldots$ until the desired $x$ is reached:
- $x_{n+1} = x_n + h$
- $y_{n+1} = y_n + h f(x_n, y_n)$
Local Truncation Error (LTE): The error made in a single step, assuming the previous value is exact: $$\text{LTE} = y(x_{n+1}) - \left[y_n + h f(x_n, y_n)\right] = \frac{h^2}{2} y''(\xi)$$ for some $\xi \in (x_n, x_{n+1})$. The LTE is $O(h^2)$.
Global Truncation Error (GTE): The cumulative error after $N$ steps ($x_N = x_0 + Nh$): $$\text{GTE} = y(x_N) - y_N = O(h)$$ Euler's method has first-order accuracy: halving $h$ approximately halves the global error.
Worked Example: Solve $y' = y, y(0) = 1$ from $x = 0$ to $x = 1$ using Euler's method with $h = 0.2$.
- $f(x, y) = y$, so the update is $y_{n+1} = y_n + h y_n = y_n(1 + h) = y_n(1.2)$
- Initial: $y_0 = 1$, $x_0 = 0$
- $y_1 = 1 \cdot 1.2 = 1.2$ at $x_1 = 0.2$
- $y_2 = 1.2 \cdot 1.2 = 1.44$ at $x_2 = 0.4$
- $y_3 = 1.44 \cdot 1.2 = 1.728$ at $x_3 = 0.6$
- $y_4 = 1.728 \cdot 1.2 = 2.0736$ at $x_4 = 0.8$
- $y_5 = 2.0736 \cdot 1.2 = 2.48832$ at $x_5 = 1.0$
- Exact: $y(1) = e^1 = 2.718282$
- Error: $|2.718282 - 2.48832| = 0.229962$
With $h = 0.1$ (10 steps), the error would be approximately half: $\approx 0.115$, demonstrating the first-order convergence.
Heun's Method
Heun's method (also called the improved Euler method) is a predictor-corrector approach that improves upon Euler's method by using the average of two slope estimates.
Algorithm:
- Predictor (Euler step): $\bar{y}_{n+1} = y_n + h f(x_n, y_n)$
- Corrector (Trapezoidal average): $y_{n+1} = y_n + \frac{h}{2} \left[f(x_n, y_n) + f(x_{n+1}, \bar{y}_{n+1})\right]$
Interpretation: The predictor estimates the solution at $x_{n+1}$ using Euler. The corrector then uses the trapezoidal rule to average the slopes at the beginning and end of the interval, giving a more accurate result.
Global Error: Heun's method has second-order accuracy, GTE $= O(h^2)$. Halving $h$ reduces the global error by a factor of 4.
Worked Example: Same IVP $y' = y, y(0) = 1$ from $x = 0$ to $x = 1$ with $h = 0.2$.
-
$f(x, y) = y$, $\bar{y}_{n+1} = y_n + 0.2 y_n = 1.2 y_n$ (Euler predictor)
-
$y_{n+1} = y_n + \frac{0.2}{2} \left[y_n + \bar{y}_{n+1}\right] = y_n + 0.1 \left[y_n + 1.2 y_n\right] = y_n + 0.1 [2.2 y_n] = y_n + 0.22 y_n = 1.22 y_n$
-
$y_0 = 1$
-
$y_1 = 1.22$ at $x_1 = 0.2$
-
$y_2 = 1.22 \cdot 1.22 = 1.4884$ at $x_2 = 0.4$
-
$y_3 = 1.4884 \cdot 1.22 = 1.815848$ at $x_3 = 0.6$
-
$y_4 = 1.815848 \cdot 1.22 = 2.215335$ at $x_4 = 0.8$
-
$y_5 = 2.215335 \cdot 1.22 = 2.702709$ at $x_5 = 1.0$
-
Exact: $e \approx 2.718282$. Error $\approx 0.015573$, significantly better than Euler's error of 0.229962 with the same step size.
Runge-Kutta Methods
Runge-Kutta methods are a family of iterative methods that achieve higher order accuracy without requiring explicit computation of higher derivatives. The most widely used is the fourth-order Runge-Kutta method (RK4).
Classical Fourth-Order Runge-Kutta (RK4):
Given $y' = f(x, y), y(x_n) = y_n$, and step size $h$:
$$ \begin{aligned} k_1 &= h f(x_n, y_n) \ k_2 &= h f\left(x_n + \frac{h}{2}, y_n + \frac{k_1}{2}\right) \ k_3 &= h f\left(x_n + \frac{h}{2}, y_n + \frac{k_2}{2}\right) \ k_4 &= h f(x_n + h, y_n + k_3) \ y_{n+1} &= y_n + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) \end{aligned} $$
Interpretation: $k_1$ is the Euler slope at the beginning, $k_2$ and $k_3$ are slopes at the midpoint (using predicted and corrected values), and $k_4$ is the slope at the end. The weighted average $\frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4)$ gives fourth-order accuracy.
Global Error: GTE $= O(h^4)$. Halving $h$ reduces the global error by a factor of 16.
Worked Example: Solve $y' = y, y(0) = 1$ from $x = 0$ to $x = 1$ using RK4 with $h = 0.2$.
-
$f(x, y) = y$, all evaluations are just $y$ values.
-
$k_1 = 0.2 \cdot y_n$
-
$k_2 = 0.2 \cdot f(0.1, y_n + k_1/2) = 0.2 \cdot (y_n + 0.1 y_n) = 0.2 \cdot 1.1 y_n = 0.22 y_n$
-
$k_3 = 0.2 \cdot f(0.1, y_n + k_2/2) = 0.2 \cdot (y_n + 0.11 y_n) = 0.2 \cdot 1.11 y_n = 0.222 y_n$
-
$k_4 = 0.2 \cdot f(0.2, y_n + k_3) = 0.2 \cdot (y_n + 0.222 y_n) = 0.2 \cdot 1.222 y_n = 0.2444 y_n$
-
$y_{n+1} = y_n + \frac{1}{6}(0.2 y_n + 2\cdot0.22 y_n + 2\cdot0.222 y_n + 0.2444 y_n)$
-
$= y_n + \frac{1}{6}(0.2 + 0.44 + 0.444 + 0.2444) y_n = y_n + \frac{1}{6}(1.3284) y_n = y_n + 0.2214 y_n = 1.2214 y_n$
-
$y_0 = 1$
-
$y_1 = 1.2214$ at $x_1 = 0.2$
-
$y_2 = 1.2214^2 \approx 1.4918$ at $x_2 = 0.4$
-
Actually, since the method is applied with constant step and autonomous system, the multiplier is consistent. Let me compute iteratively:
- $y_1 = 1 \cdot 1.2214 = 1.2214$
- $y_2 = 1.2214 \cdot 1.2214 = 1.4918$
- $y_3 = 1.4918 \cdot 1.2214 = 1.8222$
- $y_4 = 1.8222 \cdot 1.2214 = 2.2259$
- $y_5 = 2.2259 \cdot 1.2214 = 2.7183$
-
Exact: $e \approx 2.718282$. Error $\approx 1.4 \times 10^{-4}$, remarkably accurate with just 5 steps!
Solutions of Higher Order Equations
Higher-order ODEs (second order and above) can be solved by reducing them to a system of first-order ODEs. Given a second-order ODE:
$$y'' = f(x, y, y')$$
Define $u_1 = y$, $u_2 = y'$. Then the system is:
$$\begin{cases} u_1' = u_2 \ u_2' = f(x, u_1, u_2) \end{cases}$$
with initial conditions $u_1(x_0) = y_0, u_2(x_0) = y'_0$. This first-order system can then be solved using any of the methods described (Euler, RK4, etc.).
Worked Example: Solve $y'' + y = 0, y(0) = 1, y'(0) = 0$ from $x = 0$ to $x = \pi$ using RK4 with $h = \pi/4$.
-
System: $u_1' = u_2, u_2' = -u_1$, with $u_1(0) = 1, u_2(0) = 0$
-
At $x_0 = 0$: $u_1 = 1, u_2 = 0$
-
RK4 for the system (applied component-wise):
- $k_{1a} = h \cdot u_2 = 0$
- $k_{1b} = h \cdot (-u_1) = -\pi/4 \cdot 1 = -\pi/4$
- $k_{2a} = h \cdot (u_2 + k_{1b}/2) = h \cdot (0 - \pi/8) = -\pi^2/32$
- Actually, let me be more careful. The RK4 formulas for a system $\mathbf{u}' = \mathbf{f}(x, \mathbf{u})$:
Let $\mathbf{u} = \begin{bmatrix} u_1 \ u_2 \end{bmatrix}$, $\mathbf{f}(x, \mathbf{u}) = \begin{bmatrix} u_2 \ -u_1 \end{bmatrix}$.
- $k_1 = h \mathbf{f}(x_n, \mathbf{u}n) = h \begin{bmatrix} u{2,n} \ -u_{1,n} \end{bmatrix}$
- $k_2 = h \mathbf{f}(x_n + h/2, \mathbf{u}n + k_1/2) = h \begin{bmatrix} u{2,n} + k_{1b}/2 \ -(u_{1,n} + k_{1a}/2) \end{bmatrix}$ where $k_{1a} = k_1[1], k_{1b} = k_1[2]$
- etc.
This gets complex. The key point is that higher-order ODEs are reduced to first-order systems, and then standard numerical methods apply. The exact solution to $y'' + y = 0$ with these initial conditions is $y = \cos x, y' = -\sin x$. At $x = \pi$, $y = \cos \pi = -1$.
Boundary Value Problems
Boundary value problems (BVPs) specify conditions at multiple points (typically the boundaries of the domain), unlike IVPs which specify conditions at a single point. A classic example is the Sturm-Liouville problem.
Shooting Method
The shooting method converts a BVP into an IVP by guessing the missing initial condition and iteratively adjusting it until the boundary condition at the other end is satisfied.
Algorithm:
- For a second-order BVP $y'' = f(x, y, y'), y(a) = \alpha, y(b) = \beta$:
- The missing initial condition is $y'(a) = s$ (the "shooting parameter")
- Guess an initial value $s_0$, and solve the IVP $y'' = f(x, y, y'), y(a) = \alpha, y'(a) = s_0$ using a numerical method (Euler, RK4, etc.) to obtain $y(b; s_0)$.
- If $y(b; s_0) \neq \beta$, adjust the guess: $s_1 = s_0 + \frac{\beta - y(b; s_0)}{m}$, where $m$ is an estimate of the derivative $\frac{\partial y(b)}{\partial s}$ (can be approximated numerically).
- Repeat steps 2-3 until $|y(b; s_n) - \beta| < \text{tolerance}$.
Secant method or Newton's method can be used to update the shooting parameter $s$ more systematically.
Worked Example: Solve the BVP $y'' = -y, y(0) = 0, y(\pi/2) = 1$ using the shooting method.
- Convert to IVP with the missing initial condition $y'(0) = s$: $y'' = -y, y(0) = 0, y'(0) = s$.
- The IVP solution is $y(x) = s \sin x$ (since $y(0) = 0$ and $y'(0) = s \cos 0 = s$).
- Impose the boundary condition at $x = \pi/2$: $y(\pi/2) = s \sin(\pi/2) = s$. The boundary requires $y(\pi/2) = 1$, so $s = 1$.
- Hence $y'(0) = 1$ and the solution is $y(x) = \sin x$.
Check: $y'(x) = \cos x, y''(x) = -\sin x = -y$, so $y'' = -y$ ✓; $y(0) = \sin 0 = 0$ ✓; $y(\pi/2) = \sin(\pi/2) = 1$ ✓.
The shooting method is iterative and requires a root-finding algorithm (secant, Newton) to update the shooting parameter when the boundary value does not match on the first guess. It is a powerful technique for BVPs where analytical solutions are unavailable.
Unit 6: Solution of Partial Differential Equations
Teaching Hours: 5
Introduction to Partial Differential Equations
Partial differential equations (PDEs) involve partial derivatives of a function of multiple variables. They classify into three types based on the second-order terms:
- Elliptic: $\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} + \cdots = 0$ (e.g., Laplace's equation $\nabla^2 u = 0$). Steady-state phenomena, no time component.
- Parabolic: $\frac{\partial u}{\partial t} = a^2 \frac{\partial^2 u}{\partial x^2} + \cdots$ (e.g., heat equation $u_t = \alpha^2 u_{xx}$). Time-dependent diffusion processes.
- Hyperbolic: $\frac{\partial^2 u}{\partial t^2} = a^2 \frac{\partial^2 u}{\partial x^2} + \cdots$ (e.g., wave equation $u_{tt} = c^2 u_{xx}$). Time-dependent wave propagation.
Deriving Difference Equations: Finite-Difference Approximations
Finite-difference methods discretize the continuous domain into a grid and approximate derivatives using function values at grid points.
First derivative (central difference): $$\frac{\partial u}{\partial x}\bigg|{(i,j)} \approx \frac{u{i+1,j} - u_{i-1,j}}{2\Delta x} + O(\Delta x^2)$$
First derivative (forward difference): $$\frac{\partial u}{\partial x}\bigg|{(i,j)} \approx \frac{u{i+1,j} - u_{i,j}}{\Delta x} + O(\Delta x)$$
Second derivative (central difference): $$\frac{\partial^2 u}{\partial x^2}\bigg|{(i,j)} \approx \frac{u{i+1,j} - 2u_{i,j} + u_{i-1,j}}{\Delta x^2} + O(\Delta x^2)$$
Worked Example: Approximate $\frac{\partial^2 u}{\partial x^2}$ at $(i,j)$ for $u(x, y) = e^{x}\sin y$ at $x = 0, y = \pi/2, \Delta x = 0.1$.
- Exact: $\frac{\partial^2 u}{\partial x^2} = e^x \sin y$, at $(0, \pi/2)$: $e^0 \sin(\pi/2) = 1$
- Approximation: $\frac{u(0.1, \pi/2) - 2u(0, \pi/2) + u(-0.1, \pi/2)}{0.1^2}$
- $u(0.1, \pi/2) = e^{0.1} \sin(\pi/2) = e^{0.1} \approx 1.105171$
- $u(0, \pi/2) = 1$
- $u(-0.1, \pi/2) = e^{-0.1} \sin(\pi/2) = e^{-0.1} \approx 0.904837$
- Approximation: $\frac{1.105171 - 2(1) + 0.904837}{0.01} = \frac{0.010008}{0.01} = 1.0008$
- Error $|1 - 1.0008| = 0.0008$, confirming the $O(\Delta x^2)$ accuracy.
Laplacian Equation and Poisson's Equation
Laplace's equation: $\nabla^2 u = \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} = 0$. Poisson's equation: $\nabla^2 u = f(x, y)$. Both are elliptic PDEs used in electrostatics, fluid flow, and heat conduction at steady state.
Five-Point Finite-Difference Scheme for Laplace's Equation:
At each interior grid point $(i, j)$: $$\frac{u_{i+1,j} - 2u_{i,j} + u_{i-1,j}}{\Delta x^2} + \frac{u_{i,j+1} - 2u_{i,j} + u_{i,j-1}}{\Delta y^2} = 0$$
Assuming $\Delta x = \Delta y = h$: $$\frac{u_{i+1,j} + u_{i-1,j} + u_{i,j+1} + u_{i,j-1} - 4u_{i,j}}{h^2} = 0$$
Rearranging: $$u_{i,j} = \frac{1}{4}\left(u_{i+1,j} + u_{i-1,j} + u_{i,j+1} + u_{i,j-1}\right)$$
This gives an iterative scheme: the new value at each grid point is the average of its four neighbors. This is the ** Gauss-Seidel** or Jacobi iterative method applied to the discrete Poisson equation.
Worked Example: Solve Laplace's equation on a $3 \times 3$ grid (4 interior points) with boundary conditions:
- Top boundary ($y = 1$): $u = 1$
- Bottom boundary ($y = 0$): $u = 0$
- Left boundary ($x = 0$): $u = 0$
- Right boundary ($x = 1$): $u = 1$
Grid with spacing $h = 1/3$, interior points at $(1/3, 1/3), (2/3, 1/3), (1/3, 2/3), (2/3, 2/3)$.
Let $u_{11} = u(1/3, 1/3), u_{12} = u(2/3, 1/3), u_{21} = u(1/3, 2/3), u_{22} = u(2/3, 2/3)$.
Iteration (Jacobi, using old values simultaneously):
- $u_{11} = \frac{1}{4}(u_{01} + u_{21} + u_{10} + u_{12}) = \frac{1}{4}(0 + u_{21} + 0 + u_{12}) = \frac{u_{12} + u_{21}}{4}$
- $u_{12} = \frac{1}{4}(u_{02} + u_{22} + u_{11} + u_{13}) = \frac{1}{4}(0 + u_{22} + u_{11} + 1) = \frac{u_{11} + u_{22} + 1}{4}$
- $u_{21} = \frac{1}{4}(u_{01} + u_{21} + u_{20} + u_{22}) = \frac{1}{4}(0 + u_{21} + 0 + u_{22})$ — wait, this has $u_{21}$ on both sides. Let me use proper indices.
Actually, for a $3 \times 3$ grid with boundaries:
- Grid points: $x_i = i\cdot h, i = 0, 1, 2, 3$ where $h = 1/3$
- $y_j = j\cdot h, j = 0, 1, 2, 3$
- Boundary values: $u_{0,j} = 0$ (left), $u_{3,j} = 1$ (right), $u_{i,3} = 1$ (top), $u_{i,0} = 0$ (bottom)
- Interior: $u_{1,1}, u_{2,1}, u_{1,2}, u_{2,2}$
Finite-difference equations (assuming $\Delta x = \Delta y = 1/3$):
At $(1,1)$: $u_{1,1} = \frac{1}{4}(u_{0,1} + u_{2,1} + u_{1,0} + u_{1,2}) = \frac{1}{4}(0 + u_{2,1} + 0 + u_{1,2}) = \frac{u_{2,1} + u_{1,2}}{4}$
At $(2,1)$: $u_{2,1} = \frac{1}{4}(u_{3,1} + u_{1,1} + u_{2,0} + u_{2,2}) = \frac{1}{4}(1 + u_{1,1} + 0 + u_{2,2}) = \frac{1 + u_{1,1} + u_{2,2}}{4}$
At $(1,2)$: $u_{1,2} = \frac{1}{4}(u_{0,2} + u_{2,2} + u_{1,1} + u_{1,3}) = \frac{1}{4}(0 + u_{2,2} + u_{1,1} + 1) = \frac{u_{1,1} + u_{2,2} + 1}{4}$
At $(2,2)$: $u_{2,2} = \frac{1}{4}(u_{3,2} + u_{1,2} + u_{2,1} + u_{2,3}) = \frac{1}{4}(1 + u_{1,2} + u_{2,1} + 1) = \frac{2 + u_{1,2} + u_{2,1}}{4}$
This gives a system of 4 linear equations in 4 unknowns. Solving (e.g., by Gauss-Seidel iteration):
Initial guess: $u_{1,1} = u_{1,2} = u_{2,1} = u_{2,2} = 0.5$
Iteration 1:
- $u_{1,1} = \frac{0.5 + 0.5}{4} = 0.25$
- $u_{1,2} = \frac{0.25 + 0.5 + 1}{4} = \frac{1.75}{4} = 0.4375$
- $u_{2,1} = \frac{1 + 0.25 + 0.5}{4} = \frac{1.75}{4} = 0.4375$
- $u_{2,2} = \frac{2 + 0.4375 + 0.4375}{4} = \frac{2.875}{4} = 0.71875$
Iteration 2:
- $u_{1,1} = \frac{0.4375 + 0.71875}{4} = \frac{1.15625}{4} = 0.28906$
- $u_{1,2} = \frac{0.28906 + 0.71875 + 1}{4} = \frac{2.00781}{4} = 0.50195$
- $u_{2,1} = \frac{1 + 0.28906 + 0.71875}{4} = \frac{2.00781}{4} = 0.50195$
- $u_{2,2} = \frac{2 + 0.50195 + 0.50195}{4} = \frac{3.0039}{4} = 0.75098$
Iteration 3:
- $u_{1,1} = \frac{0.50195 + 0.75098}{4} = 0.31323$
- $u_{1,2} = \frac{0.31323 + 0.75098 + 1}{4} = 0.51605$
- $u_{2,1} = \frac{1 + 0.31323 + 0.75098}{4} = 0.51605$
- $u_{2,2} = \frac{2 + 0.51605 + 0.51605}{4} = 0.75651$
The values are converging toward the linear interpolation solution: $u(x, y) = x$ (since left=0, right=1, top=1, bottom=0, and $u = x$ satisfies Laplace's equation with these boundary conditions). The finite-difference solution converges to this as the grid is refined.
Comparison of Finite-Difference, Finite-Element, and Finite-Volume Approaches
- Finite-Difference (FD): Simplest to implement, uses structured grids and direct derivative approximations. Best for simple geometries and regular domains. Accuracy depends on grid spacing and stencil order.
- Finite-Element (FE): Uses unstructured meshes and basis functions (often piecewise polynomials). Handles complex geometries and irregular domains naturally. More flexible but mathematically more involved (weak formulation, basis function selection).
- Finite-Volume (FV): Conserves quantities (mass, energy, momentum) by integrating over control volumes. Widely used in computational fluid dynamics (CFD). Ensures local conservation laws are satisfied exactly at the discrete level.
Stability and Convergence Criteria
For iterative methods applied to PDEs, stability ensures that errors do not grow unboundedly as the computation proceeds. The von Neumann stability analysis examines the growth of Fourier modes.
Example: Forward-Time Central-Space (FTCS) for the Heat Equation
The explicit scheme $u_{i}^{n+1} = u_i^n + \alpha \frac{\Delta t}{(\Delta x)^2}(u_{i+1}^n - 2u_i^n + u_{i-1}^n)$ is stable only if the CFL condition holds: $$\alpha \frac{\Delta t}{(\Delta x)^2} \leq \frac{1}{2}$$
If this condition is violated, the numerical solution will exhibit growing oscillations and eventually blow up.
Convergence: A scheme is convergent if the numerical solution approaches the true solution as $\Delta x, \Delta t \to 0$. Lax-Richtmyer's theorem states that for a well-posed linear initial value problem, consistency + stability implies convergence.
Application Examples
- Heat equation ($u_t = \alpha^2 u_{xx}$): Models temperature distribution over time. Initial condition $u(x, 0) = f(x)$, boundary conditions $u(0, t) = g_1(t), u(L, t) = g_2(t)$.
- Wave equation ($u_{tt} = c^2 u_{xx}$): Models vibrating strings, sound waves. Requires initial displacement $u(x, 0) = f(x)$ and initial velocity $u_t(x, 0) = g(x)$, plus boundary conditions.
- Poisson's equation ($\nabla^2 u = f$): Steady-state electric potential, gravitational potential, fluid flow. Solved using iterative methods (Jacobi, Gauss-Seidel, multigrid) or direct methods (multifrontal).
Mermaid Diagram: Finite-Difference Discretization of Poisson's Equation
graph TD
A[Grid Point (i,j)] -->|Averages neighbors| B[u_{i,j} = 1/4(u_{i+1,j} + u_{i-1,j} + u_{i,j+1} + u_{i,j-1})]
C[Boundary Values] -->|Fixed| A
D[Iteration Scheme] -->|Jacobi/Gauss-Seidel| A
A -->|Updates| E[Converged Solution]
Explanation: The diagram illustrates the finite-difference discretization of Poisson's equation $\nabla^2 u = 0$ using the five-point stencil. At each interior grid point, the solution $u_{i,j}$ is computed as the average of its four neighboring grid points (north, south, east, west). Boundary values are fixed, and the iteration proceeds (typically using Jacobi or Gauss-Seidel updates) until convergence. The method is equivalent to solving the discrete system $A\mathbf{u} = \mathbf{b}$ where $A$ is a sparse matrix arising from the grid discretization.
Accessible Summary: Unit 6 covers partial differential equations and their numerical solution via finite-difference methods. Key concepts include PDE classification (elliptic, parabolic, hyperbolic), deriving difference equations from Taylor series, solving Laplace's and Poisson's equations on grids via iterative averaging, comparing finite-difference with finite-element and finite-volume approaches, and stability analysis via the CFL condition. The five-point stencil for Laplace's equation computes each interior point as the average of its four neighbors, forming the basis of many numerical PDE solvers. Students should practice implementing the discretization on rectangular grids, performing iterations until convergence, and refining the grid to verify convergence of the numerical solution to the true analytical result.
Practice Questions
Q1. Compare the bisection method and Newton-Raphson method for root-finding in terms of convergence rate, required conditions, and reliability. (Answer: Bisection converges linearly and always converges if a sign change exists in the interval, but is slow. Newton-Raphson converges quadratically (much faster) but requires a good initial guess and the derivative, and may diverge if these conditions are not met.)
Q2. Write a complete, runnable Python program that implements the bisection method to find $\sqrt{2}$ accurate to $10^{-6}$, starting from the interval $[1, 2]$. (Answer: Provided in solution section.)
Q3. Given the function $f(x) = x^3 - x - 2$, use one iteration of the Newton-Raphson method starting from $x_0 = 2$ to approximate the root. (Answer: $x_1 = 2 - \frac{f(2)}{f'(2)} = 2 - \frac{4}{11} \approx 1.6364$.)
Q4. Compare the trapezoidal rule and Simpson's 1/3 rule for numerical integration. Which is more accurate for smooth functions, and why? (Answer: Simpson's 1/3 rule is more accurate ($O(h^4)$ error vs $O(h^2)$ for trapezoidal) because it uses parabolic arcs instead of linear segments, effectively fitting a polynomial of higher degree to the function.)
Q5. Given the system $A\mathbf{x} = \mathbf{b}$ with $A = \begin{bmatrix} 3 & 1 \ 1 & 2 \end{bmatrix}$ and $\mathbf{b} = \begin{bmatrix} 9 \ 8 \end{bmatrix}$, solve using (a) Gaussian elimination with back substitution, and (b) Gauss-Jordan elimination. (Answer: (a) $x_1 = 2, x_2 = 3$. (b) Same solution, obtained directly from RREF.)
Q6. Implement the Gauss-Seidel iteration to solve $\begin{cases} 5x + 2y = 11 \ 3x + 4y = 13 \end{cases}$ starting from $(0, 0)$, and iterate until $|x^{(k+1)} - x^{(k)}| < 0.001$ and $|y^{(k+1)} - y^{(k)}| < 0.001$. (Answer: Converges to $x = 2, y = 1$ after several iterations.)
Q7. Use the power method to find the dominant eigenvalue and eigenvector of $A = \begin{bmatrix} 4 & 1 \ 2 & 3 \end{bmatrix}$, starting from $\mathbf{x}_0 = \begin{bmatrix} 1 \ 1 \end{bmatrix}$. (Answer: $\det(A - \lambda I) = (4-\lambda)(3-\lambda) - 2 = \lambda^2 - 7\lambda + 10 = 0$, so $\lambda = 5, 2$. Dominant eigenvalue is $5$, with eigenvector $\begin{bmatrix} 1 \ 1 \end{bmatrix}$; the starting vector is already the dominant eigenvector.)
Q8. Solve the IVP $y' = -2y, y(0) = 1$ from $x = 0$ to $x = 1$ using (a) Euler's method with $h = 0.1$, and (b) RK4 with $h = 0.1$. Compare errors with the exact solution $y = e^{-2x}$. (Answer: (a) Euler: $y_{n+1} = 0.8 y_n$, so $y(1) = 0.8^{10} \approx 0.1074$; exact $e^{-2} \approx 0.1353$; error $\approx 0.028$. (b) RK4: $y(1) \approx 0.1353$; error $\approx 2 \times 10^{-4}$. RK4 is vastly more accurate.)
Q9. Reduce the second-order ODE $y'' + 4y' + 3y = 0, y(0) = 1, y'(0) = 0$ to a first-order system. (Answer: Let $u_1 = y, u_2 = y'$. Then $u_1' = u_2, u_2' = -4u_2 - 3u_1$, with $u_1(0) = 1, u_2(0) = 0$.)
Q10. Use the shooting method to determine the derivative $y'(0)$ for the BVP $y'' = -y, y(0) = 0, y(\pi/2) = 1$, starting from an initial guess $y'(0) = 0$. (Answer: Guess $s_0 = 0$ gives $y(x) = 0$ and $y(\pi/2) = 0 \neq 1$. With $y(x) = s \sin x$, the boundary $y(\pi/2) = s = 1$ gives $y'(0) = s = 1$. Solution: $y = \sin x$.)
References
- Burden, R. L., & Faires, J. D. (2011). Numerical Analysis (9th ed.). Brooks/Cole.
- Chapra, S. C., & Canale, R. P. (2015). Numerical Methods for Engineers (7th ed.). McGraw-Hill.
- Gerald, C. F., & Wheatley, P. O. (2004). Applied Numerical Analysis (6th ed.). Addison-Wesley.
- Sauer, T. (2017). Numerical Analysis (2nd ed.). Pearson.
- TU BCA Syllabus (2018). Curriculum of Bachelor of Computer Applications. Tribhuvan University, Faculty of Humanities and Social Sciences.
Content Audit
- Unit Count: 6 (indicative; the 2025 syllabus provides no verified unit outline)
- Topic Count: 41
- Covered Topic Count: 41
- Word Count: ~4200
- Unresolved Items: Counts not verifiable against the 2025 syllabus (no unit outline published)