ROB-210 · Lesson 9 of 12 · 29 min
Inverse kinematics: numerical methods
Closed-form inverse kinematics is fast and complete, but it exists only for arms with special geometry, and every new design needs a new derivation. Redundant arms with seven joints, humanoids, mobile manipulators, legged robots and arms with unusual wrists all need a more general approach. Numerical IK treats the problem as finding the root of an equation or the minimum of an error, and improves an initial guess step by step until the tool reaches the target. These methods work for almost any robot, handle redundancy and joint limits naturally, and power the default solvers in tools such as MoveIt. This lesson develops Newton–Raphson IK, the pseudoinverse, damped least squares, gradient methods, and the practical tricks that make them reliable.
IK as root finding
Write the forward kinematics as a function \( \mathbf{x} = f(\mathbf{q}) \) from joint variables to a task-space description of the tool. For the planar 3R arm, \( \mathbf{x} = (x, y, \phi) \). Inverse kinematics asks for \( \mathbf{q} \) such that \( f(\mathbf{q}) = \mathbf{x}_d \), the desired pose, or equivalently a root of the error \( \mathbf{e}(\mathbf{q}) = \mathbf{x}_d - f(\mathbf{q}) \). In one dimension, Newton's method finds roots by following the tangent line: \( q_{k+1} = q_k + e(q_k)/f'(q_k) \). In many dimensions the derivative becomes a matrix of partial derivatives, the Jacobian:
Each column of J says how the tool moves when one joint moves a little. Lesson 10 studies the Jacobian in depth; here we need only that, near the current configuration, a small joint change produces an approximately linear tool change, \( \Delta\mathbf{x} \approx J\,\Delta\mathbf{q} \). For the planar 3R arm the Jacobian follows by differentiating the forward kinematics of lesson 5:
The Newton–Raphson iteration
Setting the linear approximation equal to the error gives the Newton step: solve \( J\,\Delta\mathbf{q} = \mathbf{e} \) and update \( \mathbf{q} \leftarrow \mathbf{q} + \Delta\mathbf{q} \). Repeat until the error is below a tolerance, typically a fraction of a millimeter and a fraction of a degree. When J is square and invertible, \( \Delta\mathbf{q} = J^{-1}\mathbf{e} \). Near a solution, Newton's method converges quadratically: the number of correct digits roughly doubles with each iteration, so a handful of iterations usually suffices.
The algorithm in outline is:
- Start from an initial guess \( \mathbf{q}_0 \), usually the robot's current configuration.
- Compute the forward kinematics \( f(\mathbf{q}_k) \) and the error \( \mathbf{e}_k = \mathbf{x}_d - f(\mathbf{q}_k) \).
- If \( \lVert \mathbf{e}_k \rVert \) is below tolerance, stop and return \( \mathbf{q}_k \).
- Compute the Jacobian \( J(\mathbf{q}_k) \) and solve for the step \( \Delta\mathbf{q} \).
- Update \( \mathbf{q}_{k+1} = \mathbf{q}_k + \Delta\mathbf{q} \), optionally limiting the step size, and repeat up to a maximum number of iterations.
For orientation in three dimensions the error cannot simply be a difference of Euler angles, which would suffer from their singularities and wrap-around. Instead the orientation error is taken as the rotation vector of \( R_d R^\mathsf{T} \), the matrix logarithm from lesson 7, giving a six-dimensional pose error \( (\mathbf{e}_\omega, \mathbf{e}_p) \) that pairs naturally with the six-row geometric Jacobian of lesson 10.
Worked problem
One Newton step for a 2R arm
A 2R arm with \( L_1 = L_2 = 0.5 \) m is at \( \boldsymbol{\theta} = (0^\circ, 90^\circ) \), so its tool is at \( (0.5, 0.5) \). Take one Newton step toward \( (0.45, 0.55) \).
Show solution
- Error: \( \mathbf{e} = (-0.05, 0.05) \).
- Jacobian at \( (0, 90^\circ) \): \( J = \begin{bmatrix} -L_1s_1 - L_2s_{12} & -L_2s_{12} \\ L_1c_1 + L_2c_{12} & L_2c_{12} \end{bmatrix} = \begin{bmatrix} -0.5 & -0.5 \\ 0.5 & 0 \end{bmatrix} \).
- Solve \( J\Delta\boldsymbol{\theta} = \mathbf{e} \): the second row gives \( 0.5\Delta\theta_1 = 0.05 \), so \( \Delta\theta_1 = 0.1 \) rad; the first gives \( -0.05 - 0.5\Delta\theta_2 = -0.05 \), so \( \Delta\theta_2 = 0 \).
- New angles: \( (5.73^\circ, 90^\circ) \). Forward kinematics gives \( (0.448, 0.547) \).
Answer: After one step the tool is within about 4 mm of the target; a second step reduces the error to well under a millimeter. Here a pure shoulder rotation was the right move, and the Jacobian found it.
Non-square Jacobians: the pseudoinverse
J is square only when the number of joints equals the number of task variables. A redundant arm, such as a seven-joint arm controlling a six-dimensional pose, has more columns than rows and infinitely many steps that achieve the same tool motion. A task that specifies more than the robot can control, such as asking a five-joint arm for a full six-dimensional pose, has more rows than columns and usually no exact step. The Moore–Penrose pseudoinverse handles both cases:
For a redundant arm, \( \Delta\mathbf{q} = J^{+}\mathbf{e} \) is the smallest joint step, in the least-squares sense, that achieves the desired tool motion. For an over-specified task, it is the step that reduces the error as much as possible. In software, the pseudoinverse is computed from the singular value decomposition, which is numerically robust; never form \( (JJ^\mathsf{T})^{-1} \) explicitly near singular configurations.
Redundancy is an opportunity as well as a complication. Any joint motion in the null space of J, the set of joint velocities that produce no tool motion, can be added without disturbing the tool. The projector \( (I - J^{+}J) \) maps any desired joint motion into that null space, so the full update
reaches the target while also moving toward a secondary goal \( \Delta\mathbf{q}_0 \), such as staying away from joint limits, avoiding an obstacle with the elbow, or keeping a comfortable posture. This is how seven-axis arms keep their elbows out of the way while the tool follows a path.
Damped least squares
Near a singularity, J becomes nearly rank-deficient: some tool motions require enormous joint motions, and the inverse or pseudoinverse produces huge, erratic steps. Damped least squares, also called the Levenberg–Marquardt method, trades a little accuracy for much better behavior by adding a small damping term \( \lambda \):
Far from singularities, with \( \lambda \) small relative to J's singular values, this is almost the pseudoinverse step. Near singularities, the damping caps the step size: in terms of the singular values \( \sigma_i \), the pseudoinverse divides by \( \sigma_i \), while damped least squares multiplies by \( \sigma_i/(\sigma_i^2 + \lambda^2) \), which never exceeds \( 1/(2\lambda) \). The cost is that the tool approaches the target a little more slowly and, at an exact singularity, may stop short of it. Many solvers adapt \( \lambda \) automatically, increasing it when steps make the error worse and reducing it when they succeed.
| Method | Step | Strengths | Weaknesses |
|---|---|---|---|
| Newton (inverse Jacobian) | \( J^{-1}\mathbf{e} \) | Quadratic convergence | Square J only; explodes near singularities |
| Pseudoinverse | \( J^{+}\mathbf{e} \) | Handles redundancy; minimum-norm steps | Unstable near singularities |
| Damped least squares | \( J^\mathsf{T}(JJ^\mathsf{T} + \lambda^2I)^{-1}\mathbf{e} \) | Robust near singularities | Slower; damping must be tuned |
| Jacobian transpose (gradient) | \( \alpha J^\mathsf{T}\mathbf{e} \) | No inversion; very simple | Slow linear convergence |
| General optimization (for example SQP) | Minimize a cost subject to constraints | Joint limits, collisions and preferences together | More computation; tuning |
Gradient methods and optimization
Another view of IK is minimization: find \( \mathbf{q} \) that minimizes \( \tfrac{1}{2}\lVert \mathbf{e}(\mathbf{q}) \rVert^2 \). The gradient of this cost is \( -J^\mathsf{T}\mathbf{e} \), so gradient descent gives the Jacobian transpose method, \( \Delta\mathbf{q} = \alpha J^\mathsf{T}\mathbf{e} \). It needs no matrix inversion and is stable, but it converges slowly, especially in narrow valleys of the cost. Its appeal is simplicity, and it has a physical interpretation: \( J^\mathsf{T} \) also maps a force at the tool to joint torques, so the method behaves like pulling the tool toward the target with a spring.
Framing IK as optimization makes it easy to add other objectives and constraints: joint limits as bounds, collision avoidance as inequality constraints, preferences for posture or manipulability as extra cost terms. TRAC-IK, a popular ROS solver, runs a Newton-style solver with random restarts in parallel with a sequential quadratic programming (SQP) solver and returns whichever finds a valid solution first. MoveIt's default KDL solver uses a damped Newton method; other plugins include BioIK, a genetic and gradient hybrid, and the analytical IKFast solvers of lesson 8. Whole-body controllers for humanoids solve a quadratic program every control cycle, combining many weighted or prioritized tasks.
Practical issues
Numerical IK finds one solution, the one its iteration happens to converge to from the initial guess. Starting from the current configuration usually gives the nearest solution, which is what a controller wants for smooth motion. To find a different solution, or to recover when the solver gets stuck in a local minimum, restart from other guesses, often random ones within joint limits. Solvers also need a maximum iteration count and a time limit, typically a few milliseconds, so they fail gracefully when a target is unreachable rather than looping forever.
Joint limits need explicit handling: clamping the updated angles to their limits after each step is simple and often adequate, while optimization-based solvers treat limits as constraints. Units matter too: a pose error mixes meters and radians, so it is common to weight the position and orientation errors, deciding for example that 1 mm of position error is as important as 0.1° of orientation error. And because numerical IK can return angles that differ by multiples of 360° from a nearby solution, wrap continuous joints into a consistent range before comparing or commanding them.
import numpy as np
L = np.array([0.4, 0.3, 0.1])
def fk(q):
a = np.cumsum(q)
return np.array([L @ np.cos(a), L @ np.sin(a), a[-1]])
def jac(q):
a = np.cumsum(q)
J = np.zeros((3, 3))
for j in range(3):
J[0, j] = -L[j:] @ np.sin(a[j:])
J[1, j] = L[j:] @ np.cos(a[j:])
J[2, j] = 1.0
return J
def ik_dls(xd, q0, lam=0.01, tol=1e-6, iters=50):
q = np.array(q0, float)
for k in range(iters):
e = xd - fk(q)
e[2] = (e[2] + np.pi) % (2*np.pi) - np.pi # wrap heading error
if np.linalg.norm(e) < tol: return q, k
J = jac(q)
q += J.T @ np.linalg.solve(J @ J.T + lam**2*np.eye(3), e)
return q, iters
xd = np.array([0.5206, 0.5157, np.radians(15)])
q, k = ik_dls(xd, np.radians([0, 20, 0]))
print(f"converged in {k} iterations: q = {np.round(np.degrees(q), 2)} deg")
q, k = ik_dls(xd, np.radians([90, -30, -40]))
print(f"from another start, {k} iterations: q = {np.round(np.degrees(q), 2)} deg")converged in 6 iterations: q = [ 30.01 45. -60.01] deg from another start, 5 iterations: q = [ 68.23 -45. -8.23] deg
The two runs illustrate the central property of numerical IK: different starting points converge to different solutions. The first finds the elbow-down configuration of lessons 5 and 8; the second, starting with the elbow bent the other way, finds the elbow-up mirror solution. Both put the tool on the target.
Worked problem
Effect of damping near a singularity
A 2R arm with \( L_1 = L_2 = 0.5 \) m is almost straight, \( \theta_2 = 1^\circ \). The smaller singular value of its Jacobian is about 0.0039. Compare the gain on that direction for the pseudoinverse and for damped least squares with \( \lambda = 0.02 \).
Show solution
- Pseudoinverse gain: \( 1/\sigma = 1/0.0039 \approx 256 \): a 1 mm error in the weak direction demands about 0.26 rad (15°) of joint motion.
- DLS gain: \( \sigma/(\sigma^2 + \lambda^2) = 0.0039/(0.0000152 + 0.0004) \approx 9.4 \).
- Maximum possible DLS gain: \( 1/(2\lambda) = 25 \).
Answer: Damping cuts the gain in the near-singular direction from about 256 to about 9, preventing a violent joint jump, at the cost of correcting that small error more slowly.
Common mistakes
MistakeUsing a plain inverse or pseudoinverse near singularities.
InsteadUse damped least squares or another regularized method.
MistakeComputing orientation error as a difference of Euler angles.
InsteadUse the rotation vector of \( R_dR^\mathsf{T} \), and wrap planar heading errors into \( (-\pi, \pi] \).
MistakeRunning the iteration without limits.
InsteadSet a maximum number of iterations and a time budget, and report failure.
MistakeStarting every solve from zero.
InsteadStart from the current configuration for continuity, and use random restarts to escape local minima.
MistakeIgnoring joint limits in the iteration.
InsteadClamp after each step, or use a solver that treats limits as constraints.
Summary and key terms
- Numerical IK iteratively drives the pose error \( \mathbf{x}_d - f(\mathbf{q}) \) to zero and works for almost any robot.
- The Jacobian linearizes forward kinematics: \( \Delta\mathbf{x} \approx J\Delta\mathbf{q} \).
- Newton–Raphson solves \( J\Delta\mathbf{q} = \mathbf{e} \) and converges quadratically near a solution.
- The pseudoinverse handles redundant and over-specified tasks; null-space motion pursues secondary goals.
- Damped least squares stays stable near singularities by capping the gain on weak directions.
- Gradient (Jacobian transpose) and optimization methods add flexibility for limits, collisions and preferences.
- Numerical solvers find the solution nearest their initial guess and need iteration limits and restarts.
- Numerical IK
- Solving inverse kinematics by iterative improvement of a guess.
- Jacobian
- The matrix of partial derivatives of the task variables with respect to the joint variables.
- Newton–Raphson method
- Root finding by repeatedly solving the linearized equations.
- Quadratic convergence
- Convergence in which the error is roughly squared at each step.
- Pseudoinverse
- The Moore–Penrose generalized inverse used for non-square or rank-deficient matrices.
- Null space
- Joint motions that produce no tool motion.
- Damped least squares
- A regularized step \( J^\mathsf{T}(JJ^\mathsf{T} + \lambda^2I)^{-1}\mathbf{e} \) that is stable near singularities.
- Jacobian transpose method
- Gradient descent on the squared pose error using \( J^\mathsf{T}\mathbf{e} \).
- Random restart
- Repeating an iterative solve from a new random initial guess.
- TRAC-IK
- A ROS IK solver combining Newton-style and SQP methods with restarts.
Practice problems
Work each problem on paper first, then reveal the answer to check.
For a 6-joint arm and a 6-D pose task, what shape is the Jacobian?
Show answer
6 × 6.
For a 7-joint arm and a 6-D pose task?
Show answer
6 × 7.
Write the Newton step for a square, invertible Jacobian.
Show answer
\( \Delta\mathbf{q} = J^{-1}\mathbf{e} \).
What does the projector \( I - J^{+}J \) do?
Show answer
Maps joint motions into the null space, so they do not move the tool.
What is the largest gain damped least squares can apply with \( \lambda = 0.05 \)?
Show answer
\( 1/(2 \times 0.05) = 10 \).
A heading error computes as 350°. What should it be wrapped to?
Show answer
−10°.
Why use the current joint configuration as the initial guess?
Show answer
It tends to converge to the nearest solution, giving smooth motion.
Which method uses \( \alpha J^\mathsf{T}\mathbf{e} \)?
Show answer
The Jacobian transpose (gradient descent) method.
Name a secondary goal a redundant arm can pursue in its null space.
Show answer
Avoiding joint limits, avoiding obstacles with the elbow or maximizing manipulability.
A solver hits its iteration limit with error still large. Name two possible causes.
Show answer
The target is unreachable, or the solver is stuck in a local minimum or near a singularity.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.