In this chapter you built Jacobians with pencil and paper: differentiate every output with respect to every input, and lay the results out so that
Row belongs to output ; column belongs to input .
Sometimes pencil and paper aren't an option. The function might be a black box you can only call — a simulator, say — or you've worked a Jacobian out by hand and want an independent check that you didn't slip. Then you measure it instead.
To measure column , leave every input alone except . Nudge up by a small amount and call ; nudge it down by and call again. How far each output moved, divided by the distance between the two calls, estimates that output's partial derivative:
Task: write numerical_jacobian(f, x, h).
f is a Python function. It takes a list of numbers and returns a list of numbers — and doesn't have to equal .x is the point, a list of numbers. Leave it exactly as it was: the caller still needs it.h is the nudge size, a positive number.Use exactly the formula above with the h you are given. Several tests use a fairly large h on purpose, so the estimate can differ slightly from the exact derivative — return the estimate, not the exact value.
Example. The chapter's worked example, at the point :
That agrees with the hand-derived Jacobian at .