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

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 f(t)=5(1−e−t/τ)−3.3 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?

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 f is continuous and f(a) and f(b) have opposite signs, the graph must cross zero somewhere between them. Bisection keeps halving that bracket:

  1. Find the midpoint m=(a+b)/2.
  2. If f(a) and f(m) have opposite signs (or f(m)=0), the crossing is in [a,m], so set b=m. Otherwise it's in [m,b], so set a=m.
  3. Repeat until b−a is smaller than the tolerance you want.

After n steps the bracket is (b−a)/2n wide, so reaching a width w takes ⌈log2⁡((b−a)/w)⌉ steps.

Lines from points

The line through (x1,y1) and (x2,y2) has slope m=y2−y1x2−x1 and intercept c=y1−mx1. Two lines y=m1x+c1 and y=m2x+c2 meet where m1x+c1=m2x+c2:

x=c2−c1m1−m2

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 a1−rn1−r. For most series there's no formula, and the loop is the only way.

Worked example

Run three steps of bisection on f(t)=5(1−e−t/0.01)−3.3, starting from [0,30ms].

Show the solution
  1. Check the bracket. f(0)=−3.3 is negative and f(30ms)=1.45 is positive, so there's a crossing between.
  2. First midpoint, 15 ms. f=5(1−e−1.5)−3.3=0.584 It's positive, so keep [0,15] ms.
  3. Second midpoint, 7.5 ms. f=5(1−e−0.75)−3.3=−0.662 It's negative, so keep [7.5,15] ms.
  4. Third midpoint, 11.25 ms. f=5(1−e−1.125)−3.3=0.077 It's positive, so keep [7.5,11.25] ms.
  5. Compare with the exact answer. It's inside the final bracket, which is now 3.75 ms wide: 10ms×ln⁡(5/1.7)=10.788ms

Answer after three steps the crossing is in [7.5,11.25] 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 I=10−14(eV/0.02585−1) A. What voltage is across the diode?

Show the solution
  1. Match the currents. The resistor's current and the diode's current must match: f(V)=5−V1000−10−14(eV/0.02585−1)=0
  2. Choose a method. No algebra will free V from inside the exponential and the line at once.
  3. Bracket the root. f(0)=0.005 is positive and f(1) is hugely negative, so bisection on [0,1] works.
  4. Bisect. Twenty steps narrow the bracket below 1 µV, at V=0.6925 V.
  5. Current. I=5−0.69251000A=4.31mA

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
  1. The battery's line. Through (0 A, 12.6 V) and (10 A, 12.1 V), the slope is −0.5/10=−0.05 V/A, so V=12.6−0.05I
  2. The load's line. V=1.2I
  3. Set them equal. 12.6−0.05I=1.2II=12.61.25=10.08A
  4. Voltage. V=1.2×10.08=12.10V
  5. In the lab. That crossing is the operating point, and intersect finds 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 [1,4] finds that f at the midpoint has the same sign as f(1). 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 a,ar,ar2,… with a loop, and steps_within(r, frac), which returns the smallest whole number n with rn≤ 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.

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.