A653 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, 1T=1T0+1Bln⁡RR0, 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 q=−uT0B with |q|≤0.131 over the range, and its error after n terms is T0|q|n+11−|q| at worst. How many terms give 0.1 °C?

Name it

The series

Let u=ln⁡(R/R0). Then T=11/T0+u/B=T01+uT0/B, a geometric series in q=−uT0B:

T=T0(1+q+q2+q3+⋯),|q|<1

Truncating after the qn term leaves exactly T0qn+11−q, so the worst error over a range is set by the largest |q| there.

The logarithm

Firmware computes ln⁡x from y=x−1x+1:

ln⁡x=2(y+y33+y55+⋯)

which converges for every x>0 and fast when x is near 1 (here R/R0 runs from about 0.25 to 3.3).

Cost

Evaluating 1+q+q2+⋯+qn by Horner's rule, 1+q(1+q(1+⋯)), takes n multiplications and n additions, with no division.

Worked example

A thermistor (R0 = 10 kΩ, B = 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
  1. Find u. u=ln⁡2.5=0.9163
  2. Use the exact formula. 1T=1298.15+0.91633950=0.0035860 so T=278.86 K, or 5.71 °C.
  3. Find q. q=−0.9163×298.153950=−0.06916
  4. Sum two terms of the series. T≈298.15(1−0.06916+0.00478)=278.96K or 5.81 °C.
  5. Compare with the exact value. The series is 0.09 °C high, which matches the truncation error: 298.15×0.069231+0.0692=0.092°C

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 y series give ln⁡3 to 10−12?

Show the solution
  1. Find y. For x=3: y=24=0.5
  2. Count the terms. The terms are 2⋅0.52k+12k+1 and they fall below 10−12 after 18 terms.
  3. Try x=0.2. There y=−23, and it takes 30.
  4. Reduce the range. Range reduction (dividing x by a power of 2 first and adding multiples of ln⁡2) keeps |y| small and the term count low.

Answer 18 terms give ln⁡3 to 10−12.

Check

A thermistor (R0 = 10 kΩ at 25 °C, B = 3950 K) reads 15 kΩ. What is its temperature?

Check

The series is truncated after the q2 term, with |q| = 0.1. What is the worst error?

Check

Write T_exact(R) (°C, from the Beta formula with R0 = 10 000 Ω, B = 3950 K, T0 = 298.15 K) and T_series(R, n), the geometric series in q=−uT0/B through qn, 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 T0qmax⁡n+11−qmax⁡. Set qmax to the largest |q| 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): 2∑y2k+12k+1 with y=x−1x+1, stopping when a term's size drops below tol. Check it on ln⁡3 and ln⁡0.2.

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 1+q+⋯+qn by Horner's rule, 1+q(1+q(⋯)), 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 u=ln⁡(R/R0) with your ln_series, forms q=−uT0/B, evaluates 1+q+⋯+q4 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 T is geometric, so its error after n terms is known exactly. Why is that rarer, and more valuable, than it sounds?

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.