A9 · Lesson 7 · Lab: multivariable functions
Gradients, tolerances and optimization by computer
Lessons 4 to 6 took derivatives of functions of several variables by hand. In practice those functions are often a simulation or a measurement, with no formula to differentiate. In this lab you'll compute gradients numerically, compare three ways of estimating how component tolerances spread to a circuit's output, climb a power surface to the maximum-power load, fit a plane to sensor readings, and solve two nonlinear equations at once with Newton's method in two variables.
- The surface is the power delivered to a load by a 10 V source with impedance Ω, shown as a slice through .
- Move : the peak over is highest when Ω cancels the source's reactance.
- At that slice the peak sits at Ω, the conjugate match, delivering 0.25 W.
Predict
Two 10 kΩ resistors in a divider each vary independently with a standard deviation of 50 Ω. Compared with adding the two effects' sizes, the actual spread of the output is:
Smaller by about . Independent errors add in squares: the output's standard deviation is , and with equal contributions that's times one of them, not 2. Adding the sizes gives the worst case, which is real but rare. A Monte Carlo simulation of many random dividers confirms the root-sum-square figure.
Name it
Numerical gradients
Central differences estimate each partial derivative:
with error proportional to ; times the variable's size balances that against rounding.
Three tolerance estimates
For an output with independent input errors :
- worst case: , the largest possible deviation;
- root-sum-square: , the standard deviation for small, independent errors;
- Monte Carlo: draw thousands of random input sets, compute for each, and measure its spread directly. It handles large errors and nonlinear functions where the linear estimates fail.
Optimization and Newton's method
Gradient ascent steps uphill: , with a step size small enough not to overshoot. Newton's method for a system uses the Jacobian matrix of partial derivatives: , which converges quadratically near a root.
Worked example
Estimate the spread of a 10 V divider's 5 V output when both 10 kΩ resistors have a standard deviation of 50 Ω.
Show the solution
- Sensitivities. With :
- Root-sum-square.
- Check by simulation. A Monte Carlo run with 100 000 random dividers gives 17.6 mV.
- Compare with adding. Adding the sizes would give 25 mV, the worst case for errors in opposite directions.
Answer the output spreads with a standard deviation of about 17.7 mV.
Worked example
Where do the circle and the parabola cross?
Show the solution
- Substitute the parabola. With the circle becomes
- Solve for , then .
- Check with Newton's method. Newton's method on the pair, with reaches the same point from in five or six steps, each roughly doubling the number of correct digits.
Answer they cross at .
Check
A 5 V divider uses two equal 1 kΩ resistors, each with a standard deviation of 1% of its value. What is the root-sum-square spread of the output?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
A source has impedance Ω. What load reactance delivers the most power?
Write units the way you normally would: 1.5 Ω, 1500 mΩ and 0.0015 kΩ are the same answer.
Check
Write grad(f, x, h=1e-5) returning the gradient of f at the NumPy point x by central differences. Set g = grad(f, np.array([1.0, 2.0])) for .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Simulate 100 000 dividers ( = 10 V, two 10 kΩ resistors drawn with rng.normal(1e4, 50, N), rng = np.random.default_rng(1)). Set sd to the output's standard deviation and rss to the root-sum-square estimate.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Maximize the power W by gradient ascent from with numerical gradients and step size 2000, for 20 000 steps. Set best to the final and Pmax to the power there.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A plate's temperature is read at the points in the starter. Fit the plane by least squares and set coef = (a, b, c).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Solve , by Newton's method from , 20 steps, using the Jacobian. Set p to the solution.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
When would you trust a Monte Carlo tolerance analysis over the root-sum-square formula?
The root-sum-square formula linearizes the output around its nominal value and assumes independent errors, so it's exact only for small errors and nearly linear functions. If tolerances are large, the function curves enough that the linear estimate misses, and it can't capture effects that only appear at extremes, like a transistor saturating or a filter's peak moving. Correlated errors, such as resistors from the same reel drifting together with temperature, also break the formula unless the correlations are included. Monte Carlo simply evaluates the real function on many random samples, so it handles all of these, at the cost of computation and a statistical error that shrinks as .
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.