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, , 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 V closes in on it by discarding, at each step, the part of the bracket that can't hold the peak.
- Step up. The readout is the bracket's width after 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?
About 36. Each step multiplies the width by 0.618, so it takes steps. That's a little slower than bisection's halving, but it needs no derivative and only one new function value per step.
Name it
Golden-section search
To maximize a function with a single peak on , compare it at two inner points, and , with .
- If , the peak can't be in : set .
- Otherwise it can't be in : set .
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: peaks at , and peaks at .
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 over .
Show the solution
- Exact peak. is zero at , where W.
- Search result. A golden-section search from to a tolerance of returns 25.819 889, agreeing to the last digit shown.
- 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 , where W.
Worked example
A panel has = 5 A, A and = 1.203 V (36 cells). Where is its maximum power point?
Show the solution
- Why a search. Setting gives an equation with both inside and outside an exponential, which can't be solved in closed form.
- Search. A golden-section search on V finds the peak at 23.24 V, delivering 110.5 W.
- Open-circuit voltage.
- 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 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, A and = 1.203 V. With your golden_max (in the starter), set Vmp to the voltage of maximum power on , Pmp to that power, and ratio to Vmp divided by the open-circuit voltage .
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 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. has two on : a broad low one and a narrow tall one. Write global_max(f, a, b, n): evaluate 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?
Each step compares the function at two inner points and throws away the outer part beyond the lower one, on the reasoning that if the function is lower there, the peak must be on the other side. That reasoning is only valid if the function rises to one peak and then falls: then a lower value at than at proves the peak isn't to the right of . With two peaks in the bracket, the comparison can throw away the part holding the higher one, and the search converges confidently to a lesser, local maximum. It never reports a problem, because it never looks at the discarded part again. That's why a search is started from a bracket that a plot or physical reasoning says holds a single peak, or run from several starting brackets and the best answer kept.
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.