A6 · Lesson 53 · Project: a thermistor curve for firmware
A series a microcontroller can afford, with a guaranteed error
A thermostat's microcontroller reads a thermistor's resistance and must report the temperature to 0.1 °C between 0 and 70 °C. The exact formula, , needs a logarithm and a division, which a small processor without a floating-point unit computes slowly. In this project you'll replace the division with a geometric series whose error you can bound exactly, find how many terms the spec needs, compute the logarithm itself from a fast series, and count the multiplications the result costs.
- The curve is the error of the truncated series for temperature, in °C, across 0 to 70 °C, for the number of terms you choose.
- Each extra term cuts the worst error by about a factor of 7.6 here: the series is geometric with ratio up to 0.131.
- Find the fewest terms that keep the whole curve inside ±0.1 °C.
Predict
The series has ratio with over the range, and its error after terms is at worst. How many terms give 0.1 °C?
Four. With = 298.15 K: three terms give °C, just over the spec; four give °C. Because the tail is exactly geometric, the bound is sharp: it's met at the hot end of the range, 70 °C, where is largest.
Name it
The series
Let . Then , a geometric series in :
Truncating after the term leaves exactly , so the worst error over a range is set by the largest there.
The logarithm
Firmware computes from :
which converges for every and fast when is near 1 (here runs from about 0.25 to 3.3).
Cost
Evaluating by Horner's rule, , takes multiplications and additions, with no division.
Worked example
A thermistor ( = 10 kΩ, = 3950 K) reads 25.0 kΩ. What temperature does the exact formula give, and what do two terms of the series give?
Show the solution
- Find .
- Use the exact formula. so K, or 5.71 °C.
- Find .
- Sum two terms of the series. or 5.81 °C.
- Compare with the exact value. The series is 0.09 °C high, which matches the truncation error:
Answer the exact formula gives 278.86 K (5.71 °C), and two terms of the series give 278.96 K (5.81 °C), 0.09 °C high.
Worked example
How many terms of the series give to ?
Show the solution
- Find . For :
- Count the terms. The terms are and they fall below after 18 terms.
- Try . There , and it takes 30.
- Reduce the range. Range reduction (dividing by a power of 2 first and adding multiples of ) keeps small and the term count low.
Answer 18 terms give to .
Check
A thermistor ( = 10 kΩ at 25 °C, = 3950 K) reads 15 kΩ. What is its temperature?
Check
The series is truncated after the term, with = 0.1. What is the worst error?
Check
Write T_exact(R) (°C, from the Beta formula with = 10 000 Ω, = 3950 K, = 298.15 K) and T_series(R, n), the geometric series in through , also in °C. Set err3 and err4 to the largest error of T_series with 3 and 4 terms over 0 to 70 °C, checking every whole degree.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write bound(qmax, n), the geometric tail bound . Set qmax to the largest over 0 to 70 °C, and n_needed to the smallest n with bound(qmax, n) < 0.1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write ln_series(x, tol=1e-12) returning (value, terms): with , stopping when a term's size drops below tol. Check it on and .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write horner_geometric(q, n) evaluating by Horner's rule, , and returning (value, mults), the result and the number of multiplications. Check it against the direct sum.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Put it together: write T_firmware(R) that computes with your ln_series, forms , evaluates by Horner's rule, and returns the temperature in °C. Set worst to its largest error against the exact formula over 0 to 70 °C (every whole degree).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
The series for is geometric, so its error after terms is known exactly. Why is that rarer, and more valuable, than it sounds?
Most Taylor series come with only a remainder bound: some unknown point where a higher derivative is evaluated, so the true error is somewhere below the bound, often far below, and you design with margin you may not need. A geometric series is different: its tail is itself a geometric series, which sums in closed form, so the error after terms is times the leading factor, exactly, not just at most. That lets you pick the smallest number of terms that meets the specification with certainty, and know where in the range the worst case occurs (the largest , here the hottest temperature). For firmware, every term saved is a multiplication saved on every reading, so an exact error is worth having.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.