A2 · Lesson 13 · Lab: functions and equations
Letting the computer find the answer algebra can't
Every equation in this course so far had an answer you could write down. Real circuits soon produce ones that don't, such as a diode's exponential in series with a resistor. Then engineers let a computer close in on the answer step by step. In this lab you'll write four small Python tools that do that kind of work: a piecewise function, an interval-halving solver, a line through two points, and a loop that adds up a series. If you haven't started A4 yet, its lessons on functions, if, loops and lists (lessons 1 to 9) cover all the Python used here.
- The curve is with = 10 ms: a capacitor charging toward 5 V, minus the 3.3 V it has to reach. The answer is where the curve crosses zero.
- The two markers are the ends of the bracket where the solver is still looking. Each step cuts the bracket in half and keeps the half whose ends have opposite signs.
- Step it up and watch the bracket close in. How many steps until it's narrower than a microsecond?
Predict
The bracket starts 30 ms wide and halves at every step. About how many steps does it take to get it under 1 µs?
It takes 15. The bracket has to shrink by a factor of , and = 32 768 is the first power of two past that. Each step pins down one more binary digit of the answer, or about 3.3 steps per decimal digit. That's slow next to the methods you'll meet in calculus, but it can't fail once the bracket holds a sign change.
Name it
Functions in Python
A Python function takes some values and returns a result. Inside, if, elif and else pick between cases, which is all a piecewise function needs:
def clip(x, lo, hi):
if x < lo:
return lo
elif x > hi:
return hi
else:
return x
A function can also take another function as a value. That's how one solver can work on any equation: you hand it the function whose zero you want.
Bisection
If is continuous and and have opposite signs, the graph must cross zero somewhere between them. Bisection keeps halving that bracket:
- Find the midpoint .
- If and have opposite signs (or ), the crossing is in , so set . Otherwise it's in , so set .
- Repeat until is smaller than the tolerance you want.
After steps the bracket is wide, so reaching a width takes steps.
Lines from points
The line through and has slope and intercept . Two lines and meet where :
Sums with a loop
A loop adds terms one at a time:
total = 0
for k in range(n):
total = total + a * r**k
For a geometric series you can check the loop against . For most series there's no formula, and the loop is the only way.
Worked example
Run three steps of bisection on , starting from .
Show the solution
- Check the bracket. is negative and is positive, so there's a crossing between.
- First midpoint, 15 ms. It's positive, so keep ms.
- Second midpoint, 7.5 ms. It's negative, so keep ms.
- Third midpoint, 11.25 ms. It's positive, so keep ms.
- Compare with the exact answer. It's inside the final bracket, which is now 3.75 ms wide:
Answer after three steps the crossing is in ms, which holds the exact answer, 10.788 ms.
Worked example
A 5 V source drives a 1 kΩ resistor in series with a diode whose current is A. What voltage is across the diode?
Show the solution
- Match the currents. The resistor's current and the diode's current must match:
- Choose a method. No algebra will free from inside the exponential and the line at once.
- Bracket the root. is positive and is hugely negative, so bisection on works.
- Bisect. Twenty steps narrow the bracket below 1 µV, at V.
- Current.
Answer the diode has 0.6925 V across it, with 4.31 mA flowing.
Worked example
A battery reads 12.6 V with no load and 12.1 V at 10 A. Where does its line cross the line of a 1.2 Ω load?
Show the solution
- The battery's line. Through (0 A, 12.6 V) and (10 A, 12.1 V), the slope is V/A, so
- The load's line.
- Set them equal.
- Voltage.
- In the lab. That crossing is the operating point, and
intersectfinds it from the two slopes and intercepts.
Answer the lines cross at 10.08 A and 12.10 V, which is the operating point.
Check
Bisection starts with a bracket 30 ms wide. How many steps until the bracket is no wider than 1 µs?
Check
Bisection on finds that at the midpoint has the same sign as . Where is the next midpoint?
Check
Write clip(x, lo, hi): it returns lo when x is below lo, hi when x is above hi, and x otherwise. This is an amplifier that can't swing past its supply rails.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write bisect(f, a, b, tol). It assumes f(a) and f(b) have opposite signs, halves the bracket until it's narrower than tol, and returns the midpoint of the final bracket. The tests use it on the capacitor and the diode from the examples.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write line_through(x1, y1, x2, y2), returning the slope and intercept (m, c), and intersect(m1, c1, m2, c2), returning the point (x, y) where two lines meet. The test finds a battery's line from two readings and crosses it with a load's line.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write geometric_sum(a, r, n), which adds the first n terms with a loop, and steps_within(r, frac), which returns the smallest whole number with frac. A capacitor charged in steps that each close a share of the remaining gap follows exactly these.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Bisection never fails on a bracketed root, yet it's slow. Explain both: why it always works, and why each step gains so little.
It always works because of continuity. If is continuous and has opposite signs at the two ends of an interval, its graph has to cross zero somewhere inside. Each step checks the midpoint and keeps whichever half still has opposite signs at its ends, so the crossing is never lost, whatever the function looks like between the points. It's slow because each step uses only the sign of at the midpoint and throws away its size. That one bit of information halves the bracket, so each step adds one binary digit to the answer, about 3.3 steps for every decimal digit. Methods that also use how steep the curve is, like Newton's method in calculus, can double the number of correct digits at each step, but they can fail when started badly.
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.