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

A5 · Lesson 31 · Lab: optimization in code

Finding the best value when calculus gives no tidy answer

Setting a derivative to zero finds the best value when you can solve the resulting equation. Often you can't: a solar panel's power curve has an exponential in it, and a choice from a list of standard resistors has no derivative at all. Then the computer searches. In this lab you'll write a golden-section search that closes in on a maximum without derivatives, check it against problems you can solve by hand, track a solar panel's maximum power point, and pick the best standard resistors by brute force.

  • The curve is a solar panel's power, P=V(Isc−I0(eV/nVT−1)), against its voltage. The current is nearly constant until the diode term takes over near open circuit.
  • The marker is the maximum power point. A golden-section search started on [0,25] V closes in on it by discarding, at each step, the part of the bracket that can't hold the peak.
  • Step k up. The readout is the bracket's width after k steps: it shrinks by the same factor, 0.618, every step, wherever the peak is.

Predict

A golden-section search starts with a bracket 30 V wide. About how many steps until it's narrower than 1 µV?

Name it

Golden-section search

To maximize a function with a single peak on [a,b], compare it at two inner points, c=b−g(b−a) and d=a+g(b−a), with g=5−12=0.618.

  • If f(c)>f(d), the peak can't be in (d,b]: set b=d.
  • Otherwise it can't be in [a,c): set a=c.

The golden ratio is chosen so that one of the old inner points becomes an inner point of the new bracket, so each step costs just one new evaluation. The bracket shrinks by 0.618 each time, whatever the function looks like.

Checking a search

A numerical optimizer should be tested on a problem with a known answer first: P(V)=8V−0.004V3 peaks at V=8/0.012=25.82, and P(R)=E2R(R+r)2 peaks at R=r.

Discrete choices

Standard resistor values come from a list, so "the best pair" is found by trying them all: for a divider with 12 values per decade over a decade, that's only 144 pairs, a trivial loop for a computer. When there are many parts, smarter searches are needed, but brute force is the honest baseline.

Worked example

Check the search on P(V)=8V−0.004V3 over [10,40].

Show the solution
  1. Exact peak. P′=8−0.012V2 is zero at V=25.820, where P=137.71 W.
  2. Search result. A golden-section search from [10,40] to a tolerance of 10−9 returns 25.819 889, agreeing to the last digit shown.
  3. What it shows. Now the code can be trusted on a curve without a formula.

Answer the search returns 25.819 889, matching the exact peak at V=25.820, where P=137.71 W.

Worked example

A panel has Isc = 5 A, I0=10−9 A and nVT = 1.203 V (36 cells). Where is its maximum power point?

Show the solution
  1. Why a search. Setting dPdV=0 gives an equation with V both inside and outside an exponential, which can't be solved in closed form.
  2. Search. A golden-section search on [0,25] V finds the peak at 23.24 V, delivering 110.5 W.
  3. Open-circuit voltage. Voc=nVTln⁡(Isc/I0+1)=26.87V
  4. Compare. The best voltage is about 87% of it, a common rule of thumb for solar trackers.

Answer the maximum power point is at 23.24 V, delivering 110.5 W.

Check

A golden-section search starts with a bracket 30 wide. How many steps until it's narrower than 0.001?

Check

Where does P(V)=8V−0.002V3 have its maximum?

Check

Write golden_max(f, a, b, tol=1e-9) returning the location of the maximum of a single-peaked f on [a, b]. Set V = golden_max(lambda V: 8*V - 0.004*V**3, 10, 40).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A panel has short-circuit current Isc = 5 A, I0=10−9 A and nVT = 1.203 V. With your golden_max (in the starter), set Vmp to the voltage of maximum power on [0,30], Pmp to that power, and ratio to Vmp divided by the open-circuit voltage nVTln⁡(Isc/I0+1).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Using the E12 list in the starter (1 kΩ to 8.2 kΩ), try every pair of a top resistor R1 and bottom R2 and set best to the (R1, R2) whose divider ratio R2/(R1+R2) is closest to k = 0.55, and err to how far off its ratio is.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A golden-section search assumes one peak. f(x)=e−(x−1)2+1.3e−((x−2.8)/0.08)2 has two on [0,π]: a broad low one and a narrow tall one. Write global_max(f, a, b, n): evaluate f at n + 1 evenly spaced points, take the best one, then run your golden_max (in the starter) on the interval one grid step either side of it. Set x1 = golden_max(f, 0, math.pi) and x2 = global_max(f, 0, math.pi, 50).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does a golden-section search need the function to have a single peak in the bracket, and what happens if it doesn't?

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.