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

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 RL+jXL by a 10 V source with impedance 50+30j Ω, shown as a slice through XL.
  • Move XL: the peak over RL is highest when XL=−30 Ω cancels the source's reactance.
  • At that slice the peak sits at RL=50 Ω, 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:

Name it

Numerical gradients

Central differences estimate each partial derivative:

∂f∂x≈f(x+h,y)−f(x−h,y)2h

with error proportional to h2; h≈10−5 times the variable's size balances that against rounding.

Three tolerance estimates

For an output y=f(x1,…,xn) with independent input errors σi:

  • worst case: ∑|∂if|Δxi, the largest possible deviation;
  • root-sum-square: ∑(∂ifσi)2, the standard deviation for small, independent errors;
  • Monte Carlo: draw thousands of random input sets, compute y 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: 𝐱←𝐱+η∇f, with a step size η small enough not to overshoot. Newton's method for a system 𝐅(𝐱)=𝟎 uses the Jacobian matrix J of partial derivatives: 𝐱←𝐱−J−1𝐅, 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
  1. Sensitivities. With V=10R2R1+R2: ∂V∂R1=−10R2(R1+R2)2=−2.5×10−4V/Ω∂V∂R2=+2.5×10−4V/Ω
  2. Root-sum-square. σV=2×2.5×10−4×50=17.7mV
  3. Check by simulation. A Monte Carlo run with 100 000 random dividers gives 17.6 mV.
  4. 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 x2+y2=4 and the parabola y=x2 cross?

Show the solution
  1. Substitute the parabola. With y=x2 the circle becomes y+y2=4
  2. Solve for y, then x. y=−1+172=1.562x=±1.250
  3. Check with Newton's method. Newton's method on the pair, with J=[2x2y−2x1] reaches the same point from (1,1) in five or six steps, each roughly doubling the number of correct digits.

Answer they cross at (±1.250,1.562).

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 50+j20 Ω. 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 f=x2y+sin⁡y.

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 (Vin = 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 P(R,X)=50R(50+R)2+(30+X)2 W by gradient ascent from (R,X)=(10,0) with numerical gradients and step size 2000, for 20 000 steps. Set best to the final (R,X) 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 T=a+bx+cy 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 x2+y2=4, y=x2 by Newton's method from (1,1), 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?

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.