CS323: Numerical Analysis and Computing

Institution: MIT

View original course

44 study materials · 8 sections

CS323: Numerical Analysis and Computing provides a comprehensive introduction to numerical algorithms, optimization, and differential equations. The course bridges the gap between theoretical mathematics and practical computation, emphasizing the implementation of algorithms using Python, NumPy, and SciPy. Students explore how finite precision arithmetic affects real-world applications in computer vision, machine learning, and data analysis.

Course Sections

Introduction to Python and Numerical Computing

Key concepts: Dynamic Typing · List Comprehension · Direct vs. Iterative Methods · Big-O Notation · Asymptotic Analysis

Foundational concepts of Python programming for scientific computing and the motivation behind numerical methods.

Introduction to Python and Numerical Computing

Numerical analysis is the study of algorithms that use numerical approximation for the problems of mathematical analysis. This field forms the backbone of modern scientific computing, engineering, and data science. While theoretical mathematics often assumes infinite precision and continuous domains, practical computation happens on hardware with finite constraints. This article explores the transition from mathematical theory to computational implementation using Python, focusing on the fundamental limitations of digital arithmetic and the linear algebraic structures that organize large-scale data.

1. The Motivation: Why "Chalkboard Math" Fails on Silicon

In pure mathematics, formulas are exact. However, when implemented on a computer, even the most robust-looking formulas can produce garbage results. This discrepancy arises from Finite Precision Arithmetic.

1.1. Catastrophic Cancellation

Consider the quadratic formula for $ax^2 + bx + c = 0$:

$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$

If $b^2 \gg 4ac$, then $\sqrt{b^2 - 4ac} \approx |b|$. If $b$ is positive, the numerator for one of the roots becomes $-b + \text{something very close to } b$. This is known as Catastrophic Cancellation—the subtraction of two nearly equal numbers, which causes a massive loss of significant digits. In numerical computing, we often rewrite such formulas (e.g., using rationalization) to avoid these operations.

1.2. Direct vs. Iterative Methods

Numerical algorithms generally fall into two categories:

Method Type Description Example
Direct Methods Provide a solution in a finite, pre-determined number of steps. Gaussian Elimination, Cramer's Rule
Iterative Methods Start with a guess and refine it through successive approximations. Newton's Method, Conjugate Gradient

2. Floating-Point Arithmetic and Error Analysis

Digital computers represent real numbers using the Floating-Point Number System, typically following the IEEE 754 standard. A number $x$ is represented as:

$$x = \pm m \times \beta^e$$

Where $m$ is the mantissa (or significand), $\beta$ is the base (usually 2), and $e$ is the exponent.

2.1. Machine Epsilon and Precision

Because the mantissa has a fixed number of bits, there is a limit to how small a difference between two numbers the computer can recognize. This limit is called Machine Epsilon ($\epsilon_{mach}$).

Definition: Machine Epsilon is the smallest positive number $\epsilon$ such that $1 + \epsilon \neq 1$ in the computer's floating-point representation.

2.2. Quantifying Error

When we approximate a real number $x$ with a machine number $\bar{x}$, we measure the error in two ways:

  1. Absolute Error: $E_{abs} = |x - \bar{x}|$
  2. Relative Error: $E_{rel} = \frac{|x - \bar{x}|}{|x|}, \quad x \neq 0$

In scientific computing, relative error is usually more important because it scales with the magnitude of the numbers involved. For double-precision (64-bit) floats, $E_{rel}$ is typically on the order of $10^{-16}$.

AI_DEMOI_DEMO## 3. Order Notation (Big-O)

To compare the efficiency of algorithms or the accuracy of approximations, we use Order Notation. It characterizes the limiting behavior of a function.

3.1. Large $n$ (Asymptotic Complexity)

In the context of algorithm runtime, we say $f(n) = O(g(n))$ as $n \to \infty$ if there exists a constant $C > 0$ such that $|f(n)| \le C|g(n)|$ for all sufficiently large $n$. This tells us how the computational cost scales with input size.

3.2. Small $h$ (Truncation Error)

In numerical calculus (like Taylor series), we care about the error as the step size $h \to 0$. We say an approximation is $O(h^k)$ if the error vanishes at a rate proportional to $h^k$.

Example: Taylor Series Expansion $$f(x+h) = f(x) + hf'(x) + \frac{h^2}{2}f''(x) + O(h^3)$$ If we drop the $h^2$ term, our approximation of $f(x+h)$ has a "first-order" error of $O(h^2)$.

4. Linear Algebra: The Framework of Numerical Computing

Numerical problems are rarely about single numbers; they involve systems of equations represented as vectors and matrices.

4.1. Vectors in $\mathbb{R}^n$

A vector $v \in \mathbb{R}^n$ is an ordered $n$-tuple of real numbers. In Python, we use the NumPy library to handle these efficiently. Unlike standard Python lists, NumPy arrays are contiguous in memory, allowing for SIMD (Single Instruction, Multiple Data) optimizations.

4.2. Matrices and Sparsity

A matrix $A \in \mathbb{R}^{m \times n}$ can be viewed as a collection of column vectors.

  • Dense Matrices: Most entries are non-zero. Stored as 2D arrays.
  • Sparse Matrices: Most entries are zero. Stored using specialized structures (like Compressed Sparse Row - CSR) to save memory and time. This is critical in fields like finite element analysis or social network graphs.

5. Norms and Stability

How do we measure the "size" of a vector or the "sensitivity" of a matrix? We use Norms.

5.1. Vector Norms

A function $|x|$ is a norm if it satisfies:

  1. $|x| \ge 0$ (Positivity)
  2. $|\alpha x| = |\alpha| |x|$ (Homogeneity)
  3. $|x + y| \le |x| + |y|$ (Triangle Inequality)

Common norms include:

  • $L_1$ Norm (Manhattan): $\sum |x_i|$
  • $L_2$ Norm (Euclidean): $\sqrt{\sum x_i^2}$
  • $L_\infty$ Norm (Max): $\max |x_i|$

5.2. Matrix Norms and Condition Numbers

The Condition Number of a matrix $A$, denoted $\kappa(A)$, is defined as: $$\kappa(A) = |A| \cdot |A^{-1}|$$

It measures how much the output $x$ of a linear system $Ax = b$ changes for a small change in the input $b$.

  • If $\kappa(A)$ is small (close to 1), the system is well-conditioned.
  • If $\kappa(A)$ is large, the system is ill-conditioned, and numerical solutions will be highly sensitive to rounding errors. A classic example is the Hilbert Matrix, where the condition number grows exponentially with the size of the matrix.

6. Python Fundamentals for Science

Python is the lingua franca of scientific computing due to its readability and the strength of its ecosystem. Key traits include:

  • Dynamic Typing: Variables do not need explicit type declarations, speeding up development.
  • Interpreted Nature: Code is executed line-by-line, which is ideal for exploratory data analysis in environments like Jupyter.
  • Platform Independence: Python code runs identically on Windows, macOS, and Linux, provided the interpreter is present.

Summary of Python Data Structures for Numerical Work

Structure Mutability Best Use Case
List Mutable General purpose collection of heterogeneous items.
Tuple Immutable Fixed sequences, dictionary keys, returning multiple values.
NumPy Array Mutable Homogeneous numerical data, linear algebra operations.
Dictionary Mutable Key-value mapping, fast lookups.

Error Analysis and Floating-Point Arithmetic

Key concepts: Machine Epsilon · Floating-point Representation · Catastrophic Cancellation · Absolute and Relative Error

Exploration of how computers represent real numbers and the resulting inaccuracies in calculations.

Error Analysis and Floating-Point Arithmetic

Overview

Computers represent real numbers using a finite number of bits, leading to rounding and truncation errors. This section quantifies these errors and identifies operations that compromise numerical stability.

Key Concepts

  • Floating-point System: Numbers are stored using a mantissa and an exponent (Scientific Notation).
  • Machine Epsilon ($\epsilon$): The smallest positive number such that $1 + \epsilon > 1$ in floating-point arithmetic.
  • Catastrophic Cancellation: A significant loss of precision that occurs when subtracting two nearly equal numbers.
  • Error Metrics:
    • Absolute Error: $|x - \hat{x}|$
    • Relative Error: $|x - \hat{x}| / |x|$

Practical Application

Homework #1 tasks students with calculating approximation errors and implementing image blurring algorithms, demonstrating how error propagates in visual data processing.

Linear Algebra Foundations for Computing

Key concepts: Vector Span and Independence · Matrix-Vector Multiplication · Column Space and Null Space · Sparse Matrices

A refresher on vector and matrix algebra with a focus on computational implementation.

Linear Algebra Foundations for Computing

Overview

Numerical analysis relies heavily on the structured framework of linear algebra. This section reviews fundamental operations and their implementation in NumPy.

Key Concepts

  • Vectors in $\mathbb{R}^n$: Operations including addition, scalar multiplication, and the dot product.
  • Matrices as Linear Maps: Understanding matrices not just as grids of numbers, but as operators that transform vectors.
  • Computational Efficiency: Introduction to Sparse Matrices (via scipy.sparse) for handling large datasets where most entries are zero.
  • Rank-Nullity Theorem: A core theoretical pillar for understanding the solvability of linear systems.

Implementation

Using NumPy for vector manipulation allows for vectorized operations, which are significantly faster than standard Python loops.

Norms, Condition Numbers, and Stability

Key concepts: Vector Norms (L1, L2, L-infinity) · Induced Matrix Norms · Condition Number · Numerical Stability

Measuring the magnitude of vectors/matrices and the sensitivity of linear systems to perturbations.

Norms, Condition Numbers, and Stability

Overview

To solve $Ax = b$ reliably, we must measure how "large" our errors are and how sensitive the system is to input noise.

Key Concepts

  • Vector Norms: Functions like $L_1$ (Manhattan), $L_2$ (Euclidean), and $L_\infty$ (Max) quantify vector magnitude.
  • Matrix Norms: Measure the maximum "stretch" a matrix applies to a vector.
  • Condition Number ($\kappa(A)$): Defined as $||A|| \cdot ||A^{-1}||$. A high condition number indicates an "ill-conditioned" system where small input changes lead to massive output errors.

Programming Challenge

The Midterm #1 Programming assignment focuses on estimating the $L_1$ condition number using heuristic and randomized approaches, avoiding the expensive explicit calculation of $A^{-1}$.

Solving Linear Systems: Direct Methods and LU

Key concepts: Gaussian Elimination · LU Decomposition · Partial and Full Pivoting · Iterative Refinement

Techniques for solving Ax=b using Gaussian elimination and matrix factorizations.

Solving Linear Systems: Direct Methods and LU

Overview

Direct methods provide an exact solution (ignoring round-off) in a finite number of steps. The most common approach is decomposing a matrix into lower and upper triangular components.

Key Concepts

  • LU Factorization: Decomposing $A = LU$ allows solving $Ly = b$ and $Ux = y$ via forward and back substitution.
  • Pivoting Strategies: Essential for numerical stability. Partial Pivoting swaps rows to ensure the largest available element is used as the divisor, preventing division by zero or very small numbers.
  • Iterative Refinement: A technique to improve the accuracy of a solution $x$ by calculating the residual $r = b - Ax$ and solving for the error correction.

Why This Matters

LU decomposition is the workhorse of numerical linear algebra, used in everything from structural engineering to circuit simulation.

Iterative Methods and Least Squares

Key concepts: Jacobi Method · Gauss-Seidel Method · Diagonal Dominance · Least Squares (Normal Equations)

Solving large-scale systems and overdetermined problems where an exact solution may not exist.

Iterative Methods and Least Squares

Overview

For extremely large or sparse matrices, direct methods become computationally expensive. Iterative methods refine an initial guess until convergence. Additionally, we address "overdetermined" systems where we seek the best possible fit.

Key Concepts

  • Jacobi vs. Gauss-Seidel: Jacobi updates all components simultaneously, while Gauss-Seidel uses the most recently updated values within the same iteration.
  • Convergence: Iterative methods are guaranteed to converge if the matrix $A$ is strictly diagonally dominant.
  • Least Squares: Used when $Ax = b$ has no solution. We solve the Normal Equations ($A^T Ax = A^T b$) to minimize the squared residual $||Ax - b||_2^2$.

Practical Application

Homework #3 explores least squares polynomial fitting and the sensitivity of normal equations to perturbations.

Root-Finding Algorithms

Key concepts: Bisection Method · Newton's Method · Secant Method · Fixed-Point Iteration · Order of Convergence

Numerical techniques for finding the zeros of nonlinear functions.

Root-Finding Algorithms

Overview

Finding the root of $f(x) = 0$ is a fundamental problem in optimization and science. This section compares methods based on their speed and reliability.

Key Concepts

  • Bisection Method: Reliable and robust; uses the Intermediate Value Theorem to narrow down an interval. Slow (linear convergence).
  • Newton's Method: Uses the derivative to find roots. Extremely fast (quadratic convergence) but requires a good initial guess and the derivative $f'(x)$.
  • Secant Method: A variation of Newton's that approximates the derivative using two points. Faster than bisection but slower than Newton.
  • Fixed-Point Iteration: Rewriting $f(x) = 0$ as $x = g(x)$ and iterating. Convergence depends on the magnitude of $g'(x)$.

Comparison

Midterm #2 materials emphasize the trade-offs between these methods, particularly regarding their convergence rates and computational cost per iteration.

Polynomial Interpolation and Approximation

Key concepts: Lagrange Interpolation · Newton Basis and Divided Differences · Chebyshev Points · Piecewise Polynomials (Splines) · Horner's Method

Constructing functions that pass through a specific set of data points.

Polynomial Interpolation and Approximation

Overview

Interpolation allows us to represent discrete data as continuous functions. This is vital for computer graphics, data fitting, and numerical integration.

Key Concepts

  • Bases for Interpolation:
    • Monomial: Simple but numerically unstable for high degrees.
    • Lagrange: Elegant theoretical form but expensive to update.
    • Newton: Uses Divided Differences; efficient to compute and easy to add new points.
  • Chebyshev Nodes: Specifically chosen points that minimize the "Runge Phenomenon" (oscillations at the edges of the interval).
  • Piecewise Interpolation: Using low-degree polynomials (like cubics) over small sub-intervals to ensure smoothness and stability.
  • Horner's Method: An efficient algorithm for evaluating polynomials that minimizes the number of multiplications.

Practical Application

Homework #5 focuses on implementing divided differences and comparing the stability of different polynomial bases.

Source Materials

Study CS323: Numerical Analysis and Computing with AI — Free on Lykke

Sign up for free to generate personalized flashcards, quizzes, and study guides from this course. Chat with an AI tutor that knows the material.

Get Started Free

View this course wiki on Lykke · Browse all public course wikis