Checking a Gradient Numerically
A steepness worked out by hand is where training bugs hide: the run still trains, just slightly wrong, and nothing crashes. So Kestrel Freight's engineers check the hand-derived figure against one measured directly from the penalty itself. The measured version treats the penalty as a black box. It nudges the setting by a small amount in both directions, sees how much the penalty changed between those two probes, and reports the rate of change that implies over the ground covered — both sides, never just one, because a one-sided probe measures the slope of a nearby point rather than of this one. Here the penalty is the cube of the setting, so the hand-derived steepness is available too.
Task: Print the measured steepness and the hand-derived steepness at the given setting.
Input
One line holding two numbers separated by a single space: the setting to check, and h, the size of the nudge used in each direction.
Output
Two lines, in this order. First the word numeric, a single space, and the measured steepness rounded to 4 decimal places. Then the word analytic, a single space, and the hand-derived steepness rounded to 4 decimal places.
The measured figure is an estimate, so the two lines will not generally agree to the last decimal — a coarse nudge leaves a visible gap between them. Report each figure as it comes out. Nothing is rounded off, reconciled or flagged.
Example:
Input:
1 0.1
Output:
numeric 3.0100
analytic 3.0000
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in