Inverse Kinematics: Analytical and Numerical Methods
What You'll Learn
- 1Derive the closed-form joint angles of a 2R arm from the law of cosines
- 2Test a target for reachability before attempting any solve
- 3Enumerate both elbow solutions and pick one by configuration preference
- 4Run one Jacobian Newton step and judge convergence from the error norm
Forward kinematics is a single evaluation; inverse kinematics is the problem a controller actually faces, and it is harder because the answer may not exist, may not be unique, and may run out of room near a singular pose. Getting it wrong sends the tool past a fixture or into the table. This lesson derives the 2R closed form, states the reachability test as an inequality you can check in one line, and then shows the numerical iteration that general manipulators use when no formula exists.
Theory and Foundations
Start with the geometry rather than the matrices. A 2R arm of link lengths and has its tool at distance from the base, and the triangle formed by the two links fixes the elbow angle by the law of cosines.
The right side is a number the target supplies. If its magnitude exceeds one, no elbow angle exists, which is the reachability test in algebraic clothing. The equivalent geometric statement is a pair of inequalities on the target radius .
When a solution exists, the base angle follows from an atan2 difference that keeps the correct quadrant, and the two-link sum is split by the elbow angle.
Because cosine is even, and both solve the triangle, giving the classic elbow-up and elbow-down pair.
Either branch must reproduce the target when pushed through forward kinematics; that round trip is the cheapest possible unit test of an inverse solver.
For arms with more joints, or for poses that also fix orientation, the closed form becomes unwieldy and iteration takes over. Newton's method on the kinematic map uses the Jacobian to convert a Cartesian error into a joint correction.
Each step should shrink the error norm, and the ratio of successive norms judges whether the iteration is converging or thrashing.
Near a singularity the inverse Jacobian blows up, so production solvers add damping instead of dividing by a small determinant.
For a redundant arm the same expression with is the pseudoinverse, and the null-space term can be added to keep joints away from limits without moving the tool at all.
Key Formulas
Worked Examples
| Method | Cost per cycle | Solutions found | Works when | Typical failure |
|---|---|---|---|---|
| Geometric closed form | a few trig calls | all branches | 2R and simple arms | wrong branch chosen |
| Jacobian Newton | one matrix inverse | one solution | pose is reachable | diverges near singularities |
| Damped least squares | linear solve | one solution | anywhere | lag when damping is large |
| Redundant pseudoinverse | linear solve | one of many | 7-DOF arms | joint limit saturation |
Real-World Examples
- Six-axis welding arms - Controllers pick between wrist-flip and elbow branches every move, choosing the branch with the smallest joint travel to avoid crossing a cable bundle.
- Seven-DOF cobots - A redundant arm keeps the elbow out of the operator's space with a null-space term while the tool pose stays fixed to within 0.1 mm.
- Bin picking - A 0.5 mm pose error from vision can push a grasp outside the reachable set, so the reachability inequality is checked before the arm starts moving.
- Servo rates - Closed-form 2R solves cost a few microseconds, while a full Newton loop on a six-axis chain runs on the order of 10 to 50 microseconds per iteration.
đĄReject before you solve
Running the Closed Form Yourself
Type the formula once and it becomes a function you can unit test, which is the whole argument for closed-form solvers: every branch stays visible and the round trip through forward kinematics either returns the target or fails loudly. Keep the reachability test ahead of the inverse cosine so an unreachable point returns a rejection instead of a math error, and return both elbow branches rather than choosing one inside the solver. Branch preference belongs to the motion planner, where collision geometry and joint limits are known, not to the geometry routine.
import math
def ik_2r(x, y, L1, L2):
r = math.hypot(x, y)
if r > L1 + L2 or r < abs(L1 - L2):
return None # unreachable, reject before acos
c2 = (r * r - L1 * L1 - L2 * L2) / (2 * L1 * L2)
c2 = max(-1.0, min(1.0, c2))
b = math.acos(c2)
base = math.atan2(y, x)
up = (base - math.atan2(L2 * math.sin(b), L1 + L2 * math.cos(b)), b)
down = (base - math.atan2(L2 * math.sin(-b), L1 + L2 * math.cos(-b)), -b)
return up, down
branches = ik_2r(0.50, 0.20, 0.40, 0.30)
for t1, t2 in branches:
xr = 0.40 * math.cos(t1) + 0.30 * math.cos(t1 + t2)
yr = 0.40 * math.sin(t1) + 0.30 * math.sin(t1 + t2)
print(round(math.degrees(t1), 1), round(math.degrees(t2), 1),
round(xr, 3), round(yr, 3))
# -11.5 80.4 0.5 0.2
# 55.1 -80.4 0.5 0.2
Damped Steps in MATLAB
Once the arm carries more joints than the task has dimensions, or the pose sits near a stretched elbow, the plain inverse is the wrong tool and the damped step takes its place. Written with a linear solve instead of an explicit inverse, the expression stays stable as the smallest singular value falls, regularizing the same way a ridge term does in regression: a little accuracy is traded away at healthy poses for a bounded command near the singularity. Raise the damping only as conditioning degrades, because an oversized value makes the arm lag behind the target at poses that never needed it.
function dq = dls_step(J, e, lambda)
% Damped least squares correction for inverse kinematics.
% J: m-by-n Jacobian, e: Cartesian error column, lambda: damping.
m = size(J, 1);
A = J * J' + (lambda ^ 2) * eye(m);
dq = J' * (A \ e); % solve, never form the inverse
end
Common Mistakes
Taking acos without checking its argument
Fix: If the cosine is outside minus one to one the target is unreachable; clamp only for reporting, never silently.
Dropping the elbow-down branch
Fix: Both signs of theta 2 solve the triangle, so keep both and let collision and joint limits choose.
Using J inverse near a stretched arm
Fix: Switch to damped least squares, because the determinant L1 L2 sin theta 2 approaches zero at full extension.
Key Takeaways
- The elbow angle comes from the law of cosines, cos theta 2 = (r squared minus L1 squared minus L2 squared) over 2 L1 L2.
- Reachability is the one-line test that the radius lies between |L1 - L2| and L1 + L2.
- Every interior target has two solutions, elbow up and elbow down, and the round trip through forward kinematics verifies either one.
- Newton iteration needs det J = L1 L2 sin theta 2, which is why damping replaces inversion near full stretch.
Frequently Asked Questions
Why are there two solutions for the same target?
The law of cosines returns a single cosine, and both and produce it. They place the elbow on opposite sides of the line from base to target, and both reproduce the target exactly after forward kinematics.
What do I do when the target is unreachable?
Report it. A common policy is to scale the target onto the workspace boundary along the line to the base, so the arm stops at the closest reachable point instead of oscillating against a limit.
If a closed form exists, why iterate at all?
Closed forms are rare beyond three or four joints, and even where they exist they can be awkward with tool orientation and joint limits. Iteration handles any smooth map, at the price of convergence guarantees you must monitor through the error norm.
Interview Questions
Q1: What is the reachability condition for a 2R arm?
The target radius must satisfy |L1 - L2| less than or equal to r less than or equal to L1 + L2; outside that band the acos argument leaves minus one to one and no solution exists.
Q2: How does a 7-DOF arm choose among infinitely many configurations?
It solves with the pseudoinverse for the task and adds a null-space term for secondary goals such as joint centering or obstacle avoidance, which moves the elbow without moving the tool.