Lecture 2: Errors and Basic Coding Proficiency
Overview
The lecture introduces error analysis and practical Python skills for engineering calculations. It explains how the Ariane 5 failure resulted from a 16-bit integer overflow, distinguishes modeling, programming, measurement, roundoff, and truncation errors, and develops true and approximate relative error measures. It also demonstrates Jupyter Notebook, NumPy, finite-difference derivatives, and why reusable functions, loops, and array indexing are preferable to repetitive copy-and-paste calculations.
Key takeaways
- The Ariane 5 Flight 501 failure shows why numeric conversions need range checks and error handling: a velocity too large for a 16-bit signed integer caused guidance systems to fail, and the flight computer treated an error message as navigation data.
- Roundoff can invalidate algebra that is exact on paper: in finite-precision floating-point arithmetic, x - (x - 1) can evaluate to 0 at x = 10^16 instead of the exact value 1.
- Relative error makes an error interpretable across different scales: a 1-meter discrepancy is 1% for a 100-meter bridge but 10% for a 10-meter structure.
- True error is useful for validating code on known-answer cases, but relative approximate error is more practical when the true answer is unavailable.
- The forward-difference derivative estimate improves as h is halved in the demonstrated range, with its error approximately halving as expected for a first-order method.
- Jupyter Notebook combines executable Python, Markdown, equations, and visualizations; using NumPy, functions, loops, and array indexing avoids fragile copy-and-paste calculations.
Chapters
- The opening clip from Hidden Figures follows Dorothy Vaughan as she learns to program NASA's IBM 7090.
- Before electronic machines, human computers performed calculations collaboratively, including grid-based weather simulations.
- The example frames coding as a way to automate calculations that once required teams of people.
- Professor Saad says the Chemical Engineering department moved to Python in 2017, ahead of several other engineering departments.
- Python is free, relatively easy to start using, and widely used in scientific computing.
- For solving nonlinear equations and ordinary differential equations, quick development can matter more than the speed of compiled C or C++.
- Jupyter Notebook combines executable code, explanatory text, equations, and visualizations in one document.
- Jupyter supports more than 40 languages through separate backends, although Python is the common default.
- Anaconda bundles Python with Jupyter, while JupyterHub lets students use University of Utah CHPC resources through a browser.
- Professor Saad recommends installing Python locally, while presenting CHPC as an alternative for students who do not want to configure a laptop.
- The Ariane 5 Flight 501 failure followed a conversion of a 64-bit floating-point velocity value to a 16-bit signed integer.
- A 16-bit signed integer can represent values only from -32,768 to 32,767, so the rocket's velocity exceeded the available range.
- The guidance systems shut down after the overflow, and the flight computer misread an error message as navigation data.
- The same code ran in both backup systems, and alignment software continued running after liftoff even though it was not needed in flight.
- The class evaluates x - (x - 1), which is exactly 1 for a real number x.
- In Jupyter, code cells execute calculations, while Markdown cells display formatted text and headings.
- The lecture demonstrates Shift+Enter to execute a cell and the shortcuts B, A, and X to add or delete cells.
- At x = 10^16, floating-point rounding makes x - 1 indistinguishable from x, so the computed result becomes 0 instead of 1.
- Modeling error comes from choosing an unsuitable physical description, such as linear drag when nonlinear drag is needed.
- Programming error is a mistake in code, while measurement error arises when an instrument cannot measure a quantity accurately.
- A Utah Symphony ventilation study received an HVAC spreadsheet reporting zero velocities because the instruments could not resolve the hall's slow airflow, around 0.1 m/s.
- Numerical errors include roundoff from finite computer precision and truncation from retaining only part of an infinite series.
- Absolute true error is defined as the difference between a true value and an approximate value.
- The forward-difference formula estimates a derivative using [f(x + h) - f(x)] / h.
- For f(x) = 2e^(0.5x), the class estimates f'(3) using h = 0.5 and h = 0.05.
- Reducing h makes the approximation closer to the derivative in this example, and the absolute error drops by about a factor of ten.
- Absolute error alone does not show how significant a discrepancy is without a reference scale.
- Relative true error compares absolute true error with the true value and is commonly reported as a percentage.
- A 1-foot error on a 1,000-foot bridge is about 0.1%, while the same error on a 10-foot table is 10%.
- The lecture also compares a 1-meter error on 100-meter and 10-meter structures, producing relative errors of 1% and 10%.
- In real applications, the true answer is often unknown, which limits the direct use of true error.
- Known-answer test cases let engineers validate and verify code before using it on more complicated problems.
- A linear-system solver can be tested against systems with exact integer solutions to build confidence in its results.
- Even calculator values such as e^0.3 are approximations, but they can serve as trusted reference values for testing.
- The exponential can be approximated by the Taylor series 1 + x + x²/2 + x³/6 + continuing terms.
- For x = 0.3, adding terms moves the estimate from 1 to 1.3, then 1.345, and toward the calculator's result.
- A calculator stops when additional terms no longer change the displayed result enough to matter.
- The approach relies on convergence: successive terms must improve the approximation rather than move it away from the answer.
- Approximate error compares a more accurate estimate with a less accurate estimate, such as Taylor approximations with 12 and 11 terms.
- Relative approximate error scales that difference by the more accurate approximation.
- A numerical routine can stop when relative approximate error falls below a chosen tolerance, such as 1%.
- The lecture distinguishes relative error from absolute error, which is useful when a physical tolerance is specified in units such as inches.
- Students estimate f'(3) for f(x) = 2e^(0.5x) using h values 0.5, 0.25, 0.125, and 0.0625.
- The first approximation has no relative approximate error because there is no earlier estimate to compare against.
- Each smaller h produces a more accurate estimate in this example, so it is compared with the estimate from the preceding, larger h.
- Halving h approximately halves the relative approximate error, illustrating the forward-difference method's first-order behavior.
- The coding exercise imports NumPy with the conventional alias `import numpy as np`.
- NumPy supplies `np.exp` for evaluating the exponential function used in the derivative calculation.
- The exercise uses x = 3 and successively halves h from 8 to 4, 2, 1, and 0.5.
- The derivative estimate uses the forward-difference formula, and the relative approximate error is computed from successive estimates.
- The brute-force notebook approach manually creates separate derivative estimates and error calculations for each h value.
- Professor Saad points out that repeating this setup for hundreds or thousands of parameter values is inefficient.
- A misplaced parenthesis in the copied exponential expression demonstrates how repetition can introduce coding mistakes.
- The exercise motivates replacing duplicated calculations with a Python function, a loop, or array-based operations.
- A function can package the derivative calculation once and accept x and h as inputs.
- A for loop can apply the same calculation to each h value stored in a container.
- The lecture introduces Python arrays as containers for the sequence of step sizes.
- These approaches are presented as a route toward computing the full error study in only a few lines of code.
- Python uses zero-based forward indexing, so the first, second, and fifth elements have indices 0, 1, and 4.
- Negative indexing accesses elements from the end: index -1 is the last element, and -2 is the second-to-last.
- The example contrasts accessing the last item by calculating the array length with directly using `A[-1]`.
- Professor Saad ends by promising to use indexing to complete the derivative and error calculation in roughly three lines.
Transcript excerpt
0:01 Let's begin. Uh good morning everyone. Second class. Thank you for coming again. I hope you have uh downloaded Python. You got that sorted out. Yeah. Anaconda. >> Okay. So, you'll follow with me today and then Yeah. Yeah. It's it's not it's not too bad. Um the the slides from last week, there were a few slides left over that I'm not going to cover, but they're self-escriptive. It's how you access Python. There's two option. One with the Center for High Performance Computing. If you don't want to worry about downloading stuff on your laptop, you can just sign up for an account and access um Python through a web browser which is great and easy lightweight but
0:49 in the long term you need Python locally so that you can do amazing stuff on your computer. So I do recommend doing downloading Anaconda and having a Python distribution locally and specifically with the Python distribution we need to be accessing what's called Jupiter. I will explain today a little more about Jupiter. Uh but first I wanted to share with you uh this little clip that I share in all my classes. Some of you have seen it before. Uh and it's a you know when I used when I used to teach uh numerical methods um a second year class you know first students first time maybe just the first time being exposed to programming. It's less so now because programming coding coding I'm going to call it coding not programming it's become more and more people are learning how to code earlier u but this was the
1:39 first code I wanted to encourage them and show them that actually coding in this day and age is so much easier than it was 10 years ago 20 years ago 30 40
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.