lecture 5: Large linear systems
Overview
Saad develops a one-dimensional steady-state heat-transfer problem into a linear system using finite differences, then shows how its boundary conditions and neighboring-point relationships produce a sparse tridiagonal matrix. He builds and solves the system with NumPy, including `linspace`, `ones`, `diag`, and `linalg.solve`, and tests how the temperature profile responds to a Gaussian heat source and changes in thermal conductivity.
Key takeaways
- The steady one-dimensional heat equation `T'' = -S(x)/k` becomes a linear system when its second derivative is replaced by the centered finite difference `(Tᵢ₊₁ - 2Tᵢ + Tᵢ₋₁)/Δx²`.
- For n grid points, the heat stencil produces a tridiagonal matrix with roughly 3n nonzeros among n² entries, so its zero fraction grows as the grid gets larger.
- Boundary conditions belong in the linear system: setting the first and last matrix rows to identity equations preserves the endpoint temperatures in the computed solution.
- NumPy’s `linspace`, `ones`, and `diag` can build the grid and tridiagonal coefficient matrix from arrays whose sizes scale with n, avoiding manual entry of every matrix coefficient.
- For fixed source strength, increasing thermal conductivity reduces the source-driven temperature peak; as conductivity tends to infinity, the solution approaches a straight line between the fixed endpoints.
- Very small conductivity produces an unboundedly large peak in this simplified model, signaling that temperature-dependent source or material behavior may be needed for a more realistic model.
Chapters
- Saad recommends `numpy.linalg.solve` for well-posed systems with hundreds or a few thousand equations.
- Dense, triangular, diagonal, and banded matrices are introduced, with sparse matrices noted as common in heat transfer and fluid mechanics.
- The lecture focuses on constructing a larger system from a reusable recipe rather than writing each equation by hand.
- A forward-difference approximation expresses a first derivative using function values at neighboring grid points.
- The second derivative is approximated by `(Tᵢ₊₁ - 2Tᵢ + Tᵢ₋₁)/h²`.
- Applying these formulas converts differential equations into algebraic equations suitable for a computer.
- The example treats temperature as varying in one spatial dimension across a wall or rod of length L.
- The boundary temperatures are fixed at the left and right ends, while an internal source adds heat.
- The steady governing equation is `d²T/dx² = -S(x)/k`, where `k` is thermal conductivity and `S(x)` is the heat-source strength.
- The continuous rod is represented by grid points, including both boundary points and interior points.
- Each grid point receives a temperature variable; the boundary temperatures are specified while interior temperatures are unknown.
- With n points there are n−1 intervals, and the spacing is determined by the chosen grid.
- At each interior point, the finite-difference equation relates `Tᵢ₋₁`, `Tᵢ`, and `Tᵢ₊₁` to `-Δx² Sᵢ/k`.
- The source value `Sᵢ` is the source function evaluated at coordinate `xᵢ`.
- The boundary points use their prescribed temperatures instead of the interior stencil, which would otherwise refer beyond the computational domain.
- The six-point example includes six equations for `T₀` through `T₅`, including `T₀ = T_L` and `T₅ = T_R`.
- Interior rows repeat the stencil, such as `T₀ - 2T₁ + T₂ = -Δx²S₁/k`.
- The equations are linear because each temperature appears only to the first power and is not multiplied by another unknown.
- Ordering the unknowns from `T₀` to `T₅` gives a 6-by-6 coefficient matrix with nonzeros concentrated near the main diagonal.
- The first and last rows encode the fixed boundary temperatures; interior rows contain the `1, -2, 1` stencil.
- Keeping boundary values in the solution vector makes it straightforward to plot the full temperature profile.
- The matrix has a main diagonal plus first upper and lower diagonals, so it is tridiagonal.
- For n points, the full matrix has n² entries, while its three diagonals contain on the order of 3n entries.
- The stencil pattern repeats for larger grids, so the matrix can be built from a general rule instead of manually entering every coefficient.
- Sparsity is defined as the fraction of matrix entries that are zero; the lecture calls a matrix sparse when at least 50% of entries are zero.
- A tridiagonal matrix has roughly 3n nonzero entries among n² total entries, making its sparsity approach 100% as n grows.
- At 100 points, the lecture estimates about 97% zeros and notes that large sparse systems may require specialized or iterative solvers.
- `np.ones` creates arrays sized to the problem, and multiplying by −2 initializes the main diagonal.
- The main diagonal’s first and last values are changed to 1 for the boundary equations.
- The upper and lower arrays each have n−1 entries, with their boundary-specific entries set to zero.
- `np.diag(D, k=0)` places the main diagonal, while offsets `k=1` and `k=-1` place the upper and lower diagonals.
- Adding the three resulting arrays of matrix entries produces the coefficient matrix `A`.
- This approach creates a dense NumPy array that still stores zeros, a practical choice for the classroom-sized example but not the most memory-efficient representation for very large systems.
- The notebook imports NumPy as `np` and Matplotlib’s plotting interface as `plt`.
- `np.linspace(0, L, n)` creates n evenly spaced coordinates, and `x[1] - x[0]` gives `Δx`.
- Saad recommends testing with a small grid, such as five points, and checking printed intermediate results before scaling up.
- The source is modeled as `α exp(-(x-a)²/σ²)`, with α controlling strength, a setting the center, and σ controlling width.
- Because the function uses NumPy operations, passing a coordinate array evaluates the source at every grid point.
- A five-point grid undersamples the Gaussian, while increasing to 100 points gives a smoother representation.
- Matplotlib plots the source against position and shows how changing σ broadens the Gaussian.
- Changing a moves the peak; centering it at `L/2` gives a symmetric example, while an offset center can help test whether the grid captures the peak.
- The plotted result can be made more readable with a grid and axis labels, including position in meters and source strength in watts per cubic meter.
- The example sets endpoint temperatures to 500 K and 400 K and defines thermal conductivity `k`.
- The right-hand side begins with `-Δx² S(xᵢ)/k`; its first and last entries are replaced by the boundary temperatures.
- `np.linalg.solve(A, b)` returns the temperature at every grid point, which is plotted against position to inspect the solution.
- An excessively strong source initially produces unrealistic temperatures, so its strength is reduced to obtain a more plausible peak between the 500 K and 400 K boundaries.
- As `k` increases, the source contribution scales down; in the large-k limit the equation approaches `T'' = 0`, whose solution is a straight line between the boundaries.
- As `k` becomes very small, the model predicts an increasingly large peak; Saad identifies this as a limitation of a model whose source and conductivity do not respond to temperature.
Transcript excerpt
0:00 Let's begin. Welcome everyone. Uh we will continue today with linear systems of equations, but we're going to take it up a notch. Uh we are going to use numpy.lin algebra solve for most of these wellposed um systems of equations. So well you have enough equations as there are unknowns and none of the equations are linearly dependent on the others. Um so you are guaranteed a solution. uh unless you know the system doesn't solve it, then you have a problem in your coefficient matrix. Um and I I went over some of these types of matrices uh last time. A general dense matrix has a bunch of coefficients in it. Uh most of them are non zero. Um you have upper and
0:48 lower triangular matrices. Um you have a diagonal matrix, identity matrix and banded matrices. These matrices show up a lot in heat transfer problem and fluid mechanics problem. We will be solving a few problems um with this type of matrix. Everything in that's empty here are zeros. So most of the entries in these matrices here um are zeros. We call these sparse matrices. Um but more on that later. Uh so for small linear systems my advice to you for small systems of equations 100 equations or so 500 700 that maybe even a thousand a few thousand equations just use numpy.lin.solve and uh move on with your life call it a day even if the matrix has mostly zeros
1:35 in it. Even if it's very dense just just do this and don't worry about it. Um but I want to give you a more realistic examples. um well not more realistic a a a little a harder example um where you have to build a large system of equations but you don't need to derive
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Saad Explains.