A4 · Lesson 37 · Solving equations numerically
When the algebra runs out, let the computer search
Where does ? There's no formula for it: no amount of algebra will isolate . Equations like that are everywhere in electronics, from a diode's current to the frequency where a filter's gain crosses some level. A computer finds the answer to twelve digits in microseconds, by searching in a way that's guaranteed to close in on it.
Predict
Bisection keeps halving an interval that contains a root. Starting from an interval 1 wide, roughly how many halvings until it's narrower than a millionth?
About 20. Each halving divides the width by 2, and , so twenty halvings take it below a millionth. That's the power of halving: each step adds about a third of a decimal digit, so twelve digits takes about forty steps.
Name it
Three tools for three kinds of equation
Bisection, for any one equation in one unknown. Write the equation as . If and have opposite signs and is continuous, a root lies between them. Check the midpoint, keep the half where the sign still changes, and repeat:
def bisect(f, a, b, tol=1e-12):
fa = f(a)
while b - a > tol:
m = (a + b) / 2
fm = f(m)
if (fm > 0) == (fa > 0): # same sign as f(a): the root is in [m, b]
a, fa = m, fm
else: # the root is in [a, m]
b = m
return (a + b) / 2
It's slow compared with cleverer methods, but it can't fail once the root is bracketed. Libraries like SciPy offer faster ones (scipy.optimize.brentq) built on the same idea.
Roots of a polynomial. np.roots(coefficients) returns every root, complex ones included, from the coefficients in order of falling power. For : np.roots([1, 0, -3, -1]).
Linear systems. Several unknowns linked by linear equations, like
are written as a matrix times a vector: , and solved with
A = np.array([[3, 2], [1, -1]])
b = np.array([7, -1])
x, y = np.linalg.solve(A, b) # x = 1, y = 2
This is how circuit simulators work: Kirchhoff's laws give one linear equation per node, and the solver handles hundreds of them at once. You'll write those equations yourself in C1; A7 explains what the solver does inside.
Worked example
Solve a two-node circuit: 5 V through 1 kΩ into node 1, 2 kΩ from node 1 to node 2, 4 kΩ from node 2 to ground, nothing else.
Show the solution
- Write KCL at each node, with conductances, and solve. The 5 V source through 1 kΩ drives 5 mA into node 1.
import numpy as np
G = np.array([[1/1e3 + 1/2e3, -1/2e3],
[-1/2e3, 1/2e3 + 1/4e3]])
i = np.array([5/1e3, 0])
print(np.linalg.solve(G, i)) # [4.2857 2.8571]
- Check by hand. It's a series string of 7 kΩ, so 0.714 mA flows, and node 2 is
Answer node 1 is at 4.286 V and node 2 at 2.857 V.
Worked example
Find the roots of and .
Show the solution
- Pass the coefficients to
np.roots.
print(np.roots([1, 3, 2])) # [-2. -1.]
print(np.roots([1, 2, 5])) # [-1.+2.j -1.-2.j]
- The first polynomial. Its roots are −2 and −1: the first circuit settles with two real decays.
- The second polynomial. Its roots are : the second rings, with the complex pair from A3.
Answer has roots −2 and −1, and has roots .
Check
Write bisect(f, a, b) that returns a root of f between a and b to within , assuming f(a) and f(b) have opposite signs. Then use it to set x to the solution of .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use np.roots to find all three roots of , and set r to them sorted from smallest to largest real part.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Solve this system with np.linalg.solve and set sol to the array of :
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
You call bisection on between −1 and 1. What happens?
Check
A capacitor charges as with Vs = 9 V and tau = 10 ms. Write newton(f, df, x0), which repeats until a step is smaller than and returns . Use it from x0 = tau to set t to the time in ms when reaches Vt = 2 V, and iters to how many steps it took.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A = np.array([[1, 2], [2, 4]]) and b = np.array([3, 6]). What does np.linalg.solve(A, b) do?
Explain why
Why is bisection guaranteed to find a root once and have opposite signs?
A continuous function can't get from a negative value to a positive one without passing through zero, so a root lies somewhere between a and b. Each step checks the midpoint and keeps whichever half still has opposite signs at its ends, so the root is always inside the interval. The interval halves every step, so after n steps the root is pinned down to within .
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.