This simulation accompanies Chapter 3 of Vibe Coding for Engineers by Anil Bahuman
01 — State the Problem
A Robot Arm Lives in Two Worlds
Controllers speak joint angles. Tasks live in Cartesian space. Forward kinematics is a recipe. Inverse kinematics is a mystery — drag the sliders to feel why.
end-effector: (·, ·)
Forward kinematics — easy ✓
x = L₁cos(θ₁) + L₂cos(θ₁+θ₂)
y = L₁sin(θ₁) + L₂sin(θ₁+θ₂)
Inverse kinematics — hard ✗
Given (xd, yd), find θ₁, θ₂
such that FK(θ) = target
The problem we need to solve
F(θ) = FK(θ) − target = 0 where θ = [θ₁, θ₂]ᵀ
This is a nonlinear system. Sine and cosine make it transcendental — algebra alone cannot solve it.
02 — Amplify the Problem
Why Algebra Breaks Down
The functions that define robot position — sine, cosine — have no algebraic inverse in combination. For chains with 3+ joints, no closed form exists at all.
The system to solve
f₁ = L₁cos(θ₁) + L₂cos(θ₁+θ₂) − xd = 0
f₂ = L₁sin(θ₁) + L₂sin(θ₁+θ₂) − yd = 0
Why direct inversion fails
arcsin only returns one branch Multiple solutions for same target
θ₁ and θ₂ are entangled nonlinearly
6-DOF arms: 16+ solution branches
The key reframing
Don't solve it exactly. Linearize near a guess, step toward the root, and repeat until close enough.
‖F(θ)‖ over θ-space
blue = near zero (solution) · bright = large error
⚡
The singularity problem: At certain configurations, two joints become kinematically aligned — the arm loses a degree of freedom momentarily. The Jacobian becomes singular (det = 0). This must be handled with damped least-squares or joint limits. We will see this in the solution step.
03 — The Solution
Newton-Raphson: Iterate to the Root
Replace the hard nonlinear solve with a sequence of trivial linear ones. The Jacobian tells us which direction reduces error the most — click the workspace to watch it converge.
The update rule
θk+1 = θk − J(θk)⁻¹F(θk)
The Jacobian (2-DOF planar)
J = [−L₁s₁−L₂s₁₂ −L₂s₁₂]
[ L₁c₁+L₂c₁₂ L₂c₁₂]
Convergence rate
Near root: quadratic convergence
eₖ₊₁ ≈ C · eₖ²
1mm → 1μm → 1nm in 3 steps
← click the workspace to set a target
click anywhere in workspace to solve
convergence plot — error per iteration
04 — Summary
What the Engineer Takes Away
Newton-Raphson doesn't solve the problem — it dissolves it, by replacing the hard question with a sequence of easy ones.
⟳
Iterative, not algebraic
No closed-form needed. Start anywhere inside the workspace, step toward the root using local gradient information.
∂
The Jacobian is the engine
J maps infinitesimal joint-space motion to end-effector motion. Inverting it gives the correction direction.
²
Quadratic convergence
Once inside the basin of convergence, error squares each iteration. Practically: 5–8 steps to machine precision.
△
Singularities require care
det(J) → 0 at kinematic singularities. Damped least-squares: J† = Jᵀ(JJᵀ + λI)⁻¹ is the standard fix.
The one formula to remember
θk+1 = θk − J⁻¹(θk) · F(θk)
F(θ) = FK(θ) − target | J = ∂FK/∂θ | converges quadratically near the solution
📖
Further reading for your book: Spong & Vidyasagar §4.4 on iterative IK; Buss (2004) "Introduction to inverse kinematics with Jacobian transpose, pseudoinverse and damped least-squares methods" — the canonical reference for practitioners.