A437 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 37 · Solving equations numerically

When the algebra runs out, let the computer search

Where does cos⁡x=x? There's no formula for it: no amount of algebra will isolate x. 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?

Name it

Three tools for three kinds of equation

Bisection, for any one equation in one unknown. Write the equation as f(x)=0. If f(a) and f(b) have opposite signs and f 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 x3−3x−1: np.roots([1, 0, -3, -1]).

Linear systems. Several unknowns linked by linear equations, like

3x+2y=7x−y=−1

are written as a matrix times a vector: [321−1][xy]=[7−1], 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
  1. 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]
  1. Check by hand. It's a series string of 7 kΩ, so 0.714 mA flows, and node 2 is v2=0.714×4V=2.857V

Answer node 1 is at 4.286 V and node 2 at 2.857 V.

Worked example

Find the roots of s2+3s+2 and s2+2s+5.

Show the solution
  1. 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]
  1. The first polynomial. Its roots are −2 and −1: the first circuit settles with two real decays.
  2. The second polynomial. Its roots are −1±2j: the second rings, with the complex pair from A3.

Answer s2+3s+2 has roots −2 and −1, and s2+2s+5 has roots −1±2j.

Check

Write bisect(f, a, b) that returns a root of f between a and b to within 10−9, assuming f(a) and f(b) have opposite signs. Then use it to set x to the solution of cos⁡x=x.

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 x3−3x−1, 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 x,y,z:

4x+y+z=10x+2y+z=12x+y+7z=14

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 f(x)=x2+1 between −1 and 1. What happens?

Check

A capacitor charges as v(t)=Vs(1−e−t/τ) with Vs = 9 V and tau = 10 ms. Write newton(f, df, x0), which repeats x←x−f(x)/f′(x) until a step is smaller than 10−12 and returns x. Use it from x0 = tau to set t to the time in ms when v 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 f(a) and f(b) have opposite signs?

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.