A greenhouse arm must bring its sensor to a leaf. It is a planar two-link arm: the shoulder joint at the origin, a first link of length l1, an elbow, and a second link of length l2 ending in the sensor (the hand). The joint angles are in radians and relative: t1 is the first link's angle from the +x axis, and t2 is the second link's angle from the direction of the first link. With x to the right and y up, the hand is at
J is the Jacobian at the current angles: the 2 \times 2 matrix with rows (\partial x / \partial t_1, \partial x / \partial t_2) and (\partial y / \partial t_1, \partial y / \partial t_2). Let e = (t_x - x,\; t_y - y) be the vector from the hand to the target, as a column.
Two update rules are on trial. Each step computes the change \Delta q of the two angles from the current angles and then adds it to both at once, q \leftarrow q + \Delta q:
- Jacobian transpose: \Delta q = \alpha \, J^T e.
- Damped least squares: \Delta q = J^T (J J^T + \lambda^2 I)^{-1} e, where I is the 2 \times 2 identity matrix.
Write compare_steps(l1, l2, target, start, alpha, lam, steps) that runs each rule for exactly steps steps from start = (t1, t2) (each rule from the same start) and returns a tuple (d_transpose, d_dls, closer): the final distance from the hand to the target for each rule, and "transpose" or "dls" for the rule that ended closer, or "tie" if the two distances differ by at most 1e-9.
Examples
Input: l1 = 1, l2 = 1, target = (1, 1), start = (0, 0.5), alpha = 0.2, lam = 0.1, steps = 5
Output: (0.40630961, 0.00067156, "dls")
Input: l1 = 1, l2 = 1, target = (1, 1), start = (0, 0.5), alpha = 0.2, lam = 0.1, steps = 1
Output: (0.54651309, 1.22298628, "transpose")
Explanation: the first damped step aims straight at the target as if the arm were
linear, overshoots, and lands farther away than it started (1.0204).
Input: l1 = 1, l2 = 1, target = (1, 1), start = (0, 0.5), alpha = 0.2, lam = 0.1, steps = 0
Output: (1.02036705, 1.02036705, "tie")
Constraints
- answers are compared with a tolerance of
1e-6; do not round - the tests avoid distances that differ by almost exactly
1e-9
Goals
- Iterate the Jacobian-transpose update for a fixed number of steps
- Iterate the damped-least-squares update, inverting a $2 \times 2$ matrix each step
- Compare how close each method gets in the same number of steps