lecture 6: nonlinear solvers
Overview
Professor Saad closes the linear-solver discussion with sparse matrix storage and solver-selection guidance, then introduces nonlinear root finding through a pipe-flooding calculation. He demonstrates how to define a residual, use SciPy's `root` with an initial guess, inspect the result, and pass changing problem parameters through `args`.
Key takeaways
- For a 3D diffusion grid with n points per direction, the system has n³ unknowns but only about seven nonzeros per equation, so sparse storage can reduce memory from O(n⁶) to O(n³).
- Solver choice should reflect both matrix size and sparsity: use direct solvers for small systems, consider iterative methods for large sparse systems, and exploit specialized direct methods for banded matrices.
- A nonlinear equation should be expressed as a zero-residual problem; for example, transform `f(x) = a` into `r(x) = f(x) - a = 0` before calling SciPy's `root`.
- SciPy's `root` requires an initial guess and returns a result object whose `x`, convergence status, residual, and function-evaluation count help assess the solution.
- The pipe example finds a height of about 1.506 m for 98.44 m³ of water in the coded 15 m-long pipe with 4 m radius, below the example worker's 1.7 m height.
- Passing fixed parameters through `args` keeps residual functions reusable: changing the pipe inputs to 130 m³, 12 m length, and 3 m radius yields a height near 2.446 m.
Chapters
- A 1D discretization with n grid points produces an n-by-n matrix; equivalent square 2D and cubic 3D grids produce n²-by-n² and n³-by-n³ matrices.
- For diffusion, the coefficient matrix has three nonzero bands in 1D, five in 2D, and seven in 3D.
- Storing every zero in a dense 3D matrix becomes impractical: the lecture estimates 512 GB for 64 points in each direction.
- Sparse storage keeps nonzero entries rather than all n⁶ entries in a dense 3D matrix; with seven nonzeros per equation, storage scales roughly with 7n³.
- The lecture compares about 512 GB of dense storage with about 14 MB for sparse storage at 64 grid points per direction.
- In Python, `scipy.sparse.diags` builds a sparse banded matrix, and SciPy's sparse linear algebra solver handles the solve; the matrix can use CSR, or compressed sparse row, storage.
- Timing comparisons show that direct solves grow costly as equation counts increase, even when the matrix comes from a sparse heat-equation discretization.
- A 1D example takes about one second for 10,000 equations, which becomes significant when repeated across millions of time steps or many statistical samples.
- In the 2D comparison, sparse direct solving improves on dense solving but eventually becomes expensive; algebraic multigrid stays nearly flat over the plotted range.
- For small systems, roughly 100-by-100 or 200-by-200, Professor Saad recommends a standard direct solver.
- For large sparse systems, investigate iterative solvers; special tridiagonal and pentadiagonal direct methods can be especially fast.
- Large dense systems are difficult to avoid solving directly, although optimization methods such as least squares can sometimes avoid forming or solving the full dense system.
- A linear equation is a special case within the broader class of equations; nonlinear variables can appear in powers, products, or functions such as cosine.
- The key first step is to identify whether a problem is algebraic or differential, linear or nonlinear, and whether it is root finding or optimization.
- For large sparse linear systems, iterative solvers are presented as effective tools, even though their theory is beyond this lecture's scope.
- The example asks whether a 1.7 m tall worker can walk through a flooded pipe without being submerged.
- The pipe problem uses a water volume of 98.44 m³ and a pipe radius of 4 m; the lecture discusses pipe lengths of 12 m in the setup and 15 m in the later code.
- The unknown water height appears inside an inverse-cosine and square-root expression, so simple algebraic rearrangement is not a practical solution.
- A nonlinear equation such as the pipe-volume relation cannot generally be solved with the linear-system tool `numpy.linalg.solve`.
- Students suggest numerical tools including SciPy's `fsolve`, Mathematica, and Excel; the lecture moves toward SciPy's newer `root` interface.
- A numerical solver searches for an approximate solution, making it important to recognize the problem type before choosing software.
- Whether an equation is nonlinear depends on the variable being solved for, not just on whether the full expression contains nonlinear terms.
- In the lecture's example, a variable under a square root is nonlinear, while another variable that appears only as a factor can still be solved for algebraically.
- A variable that appears both linearly and inside a cosine is nonlinear overall, because the cosine dependence prevents a linear solution.
- Solving `f(x) = 0` means finding x-values where the function intersects the x-axis; these values are called roots or zeros.
- A function can have no roots, one root, or several roots, depending on how many times its graph meets the axis.
- For equations such as `f(x) = 10`, the solution is the intersection with the line y = 10 rather than y = 0.
- To standardize root finding, move the right-hand side to the left and define a residual, for example `r(x) = f(x) - 10`.
- The original equation `f(x) = 10` and the residual equation `r(x) = 0` have the same root.
- For the pipe problem, the residual is defined in terms of the unknown height H, with volume, pipe length, and radius treated as known inputs.
- Professor Saad recommends `scipy.optimize.root` for nonlinear root finding and advises using it instead of the older `fsolve` interface.
- The solver needs a callable residual function and an initial guess; the unknown being solved for must be the function's first argument.
- The initial guess matters because the numerical method iterates from that starting point and may converge to a nearby root when several roots exist.
- The class implementation imports `root` from `scipy.optimize` and writes a Python function that returns the pipe-volume residual for a proposed height H.
- Plotting the residual over heights from 0 to 8 m reveals where the curve crosses zero and helps select a starting guess.
- Changing the water volume from about 98.44 to 120 and then 200 m³ moves the plotted root upward, illustrating how the solution depends on the inputs.
- With the code's values V = 98.44 m³, L = 15 m, and R = 4 m, `root` converges to a water height of about 1.506 m.
- The returned result object reports convergence information, the final residual, and the number of function evaluations; the lecture interprets the small residual as the root-finding error.
- A starting guess closer to the plotted root reduces function evaluations in the example, while a farther guess takes more iterations.
- Instead of hardcoding volume, length, and radius as global variables, the residual can accept them as additional arguments after the unknown height.
- Pass the matching parameter tuple through `root(..., args=(V, L, R))`; the first residual argument remains the variable whose root is being found.
- Using the example's changed inputs, V = 130, L = 12, and R = 3, produces a height of about 2.446 m and makes repeated calculations easier to automate.
Transcript excerpt
0:00 We are almost done with linear systems. I want to give you an overview of matrix storage and memory. We were we are going to come back to this once we get to two dimensional problems. So what I would have liked to do is do the heat transfer problem we did in class but do it in two dimensions. Okay. um that's going to require about a full session full lecture but we're going to come to that anyway later when we explain conservation laws and heat transfer so we will do it then right now I want you to just have enough mileage with these basic tools uh because the 2D case the issue is not the the issue becomes the logistics of putting together the
0:47 coefficient matrix not solving it right now we're focused on just solving the coefficient matrix. Okay, so I don't want to waste time focusing on the logistics and the mechanisms of how we build matrices for 2D or 3D problems. So we will just stick stay focused on um uh lin algebra.solve right now. Um I wanted to talk about matrix storage before we uh we are done with linear systems. So in in two and three dimensions the heat transfer problem we solved this second derivative of temperature this applies also to mass diffusion. So when you're talking about concentrations, you'll you'll cover that again um in the next course transport if you haven't taken mass and heat transfer. They're very
1:35 similar. There's an analogy between had heat and mass transfer because it's a diffusion process. Um in 3D the general operator is a combination of second derivatives of in each direction for some quantity. We'll call it u. This could be temperature. This could be um velocity. This could be um uh
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.