Inverse kinematics calculator for a two-link robot arm
Give a 2R planar arm its link lengths and either the joint angles (forward kinematics) or a target point (inverse kinematics), and get the elbow and tip positions, both elbow-up and elbow-down solutions, the workspace and how close the arm is to a singularity. It opens on the 2R robotic arm challenge of Skill-Lync's MATLAB and Python courses (L1 = 1 m, L2 = 0.5 m), can play the course's 100-frame sweep, and checks the x2, y2 your code printed. Free, no sign-up, runs in your browser.
Closed-form IK checked against a Newton solver and 2,000 round trips · Updated 20 Nov 2026
"From link 1" is the joint angle a motor turns, as in textbooks and DH tables: x2 = x1 + L2·cos(θ1 + θ2). "From the x-axis" is how the course code writes it: x2 = x1 + l2·cosd(θ2).
Inverse kinematics always answers in joint angles (θ2 from link 1) and gives the link-2 angle from the x-axis alongside. Drag the target by editing x and y.
Type the angles and link lengths your code used and the tip position it printed. Pick the convention your report claims. Matching is to the digits you typed.
- Same pose with θ2 measured from the x-axis: θ1 = 30°, θ2 = 75.0°.
- Inverse kinematics of this tip gives this pose (elbow down) and θ1 = 59.28°, θ2 = -45.00° (elbow up).
What is the 2R robotic arm challenge?
A 2R arm is two rigid links joined by two revolute (R) joints, one at the base and one at the elbow, moving in a plane. The challenge asks you to write a program that computes the arm's position for a range of joint angles, plot each pose and save the frames as an animation. It is a weekly challenge in Skill-Lync's MATLAB and Python courses for mechanical engineers, and 1,912 public student reports are about it (949 titled "Week 2- 2R Robotic Arm Challenge").
We read 40 of those reports on 20 Nov 2026. 37 use MATLAB and 3 Python; 26 use L1 = 1 m and L2 = 0.5 m, and 22 sweep both angles with linspace(0, 90, 10), 10 × 10 = 100 frames. This calculator uses those as defaults.
What are the forward kinematics equations of a 2R arm?
Put the base at the origin. Link 1 points at θ1 from the x-axis, so the elbow is at (L1·cos θ1, L1·sin θ1). Link 2 turns a further θ2 at the elbow, so it points at θ1 + θ2:
Worked example: L1 = 1 m, L2 = 0.5 m, θ1 = 30°, θ2 = 45°. Elbow (cos 30°, sin 30°) = (0.8660, 0.5000). Link 2 points at 75°, so the tip is (0.8660 + 0.5·cos 75°, 0.5000 + 0.5·sin 75°) = (0.9954, 0.9830) m, 1.3990 m from the base. The same answer comes out of the Denavit-Hartenberg product of two planar transforms (a = L, α = 0, d = 0), which is how the method grows to arms with more joints.
Is your θ2 measured from link 1 or from the x-axis?
This is the one thing to get right in this challenge, and the place most reports are loose. Almost every report's code computes:
Here θ2′ is link 2's angle from the x-axis, not the elbow joint angle. In our sample, 39 of the 40 reports with code compute it this way and 0 use cos(θ1 + θ2). That is a legitimate way to describe the arm if you say so. Trouble starts when the two get mixed: 4 of the 40 reports define θ2 as "the angle between link 1 and link 2" and then code it from the x-axis.
The difference is not small. Feed θ1 = 30°, θ2 = 45° into the x-axis form and the tip lands at (1.2196, 0.8536), 0.259 m from the joint-angle answer, because the elbow is really bent only 15°. And the sweep changes shape:
- The elbow bends both ways in the course sweep. With θ2 from the x-axis, the joint angle θ2 − θ1 runs from -90° to +90°, so the animation mixes elbow-up and elbow-down poses. A joint swept 0 to 90° bends one way only.
- The plot window fits only one of them. The common axis([-0.1 1.5 0 1.5]) holds all 100 x-axis-form frames, but the joint-angle form reaches x = -0.5 m at θ1 = θ2 = 90°, and 14 of its frames poke outside that window. Use axis equal and a window from about −0.6 to 1.6.
- 10 of the 100 x-axis-form frames are fully stretched (θ1 = θ2, a singular pose, below).
Both conventions reach the same distances, 1.118 to 1.5 m from the base. If your report is about robot kinematics, use the joint angle: it is what an encoder at the elbow reads and what every DH table, robot controller and textbook (Craig; Spong, Hutchinson and Vidyasagar) means by θ2.
How do you solve the inverse kinematics of a 2-link arm?
Inverse kinematics runs the other way: given the tip position (x, y), find θ1 and θ2. The base, elbow and tip form a triangle with sides L1, L2 and r = √(x² + y²), so the law of cosines gives the elbow angle:
Worked example: L1 = 1, L2 = 0.5, target (1, 0.8). cos θ2 = (1 + 0.64 − 1 − 0.25)/(2 × 1 × 0.5) = 0.39, so θ2 = ±67.05°. With +67.05°, θ1 = 17.59° (elbow down, elbow at (0.953, 0.302)). With −67.05°, θ1 = 59.73° (elbow up, at (0.504, 0.864)). Both put the tip exactly on (1, 0.8).
Three cases come out of the cosine. Between −1 and 1: two solutions. Exactly ±1: one, the arm fully stretched or folded. Outside ±1: the point is out of reach, too far (beyond L1 + L2) or too close (inside |L1 − L2|). Use atan2 and not atan: atan(y/x) loses the quadrant, so a target behind the base comes out 180° wrong.
What is the workspace of a two-link arm?
With both joints free to turn all the way round, the tip can reach a ring around the base:
For the course arm that is 0.5 to 1.5 m and 6.283 m². With equal links the hole closes and the tip can reach the base itself. Sweeping both angles only from 0 to 90°, as the challenge does, covers 0.79 m² (12.5% of the ring) with joint angles and about 0.41 m² (6.5%) with θ2 from the x-axis, measured on a 1,200 × 1,200 grid. The x-axis form covers less because its poses fold over each other: different angle pairs reach the same points.
Where are the singularities of a 2R arm?
The Jacobian J maps joint speeds to tip velocity. For the 2R arm:
det J is zero when θ2 = 0° or 180°: the arm is fully stretched or folded, and the tip cannot move along the line of the arm however fast the joints turn. Near those poses inverse kinematics asks for very large joint speeds for small tip moves. At the worked example (θ2 = 45°) det J = 0.3536 m², 70.7% of the best value L1·L2, reached at θ2 = ±90°. The calculator shows det J live; it also equals the Yoshikawa manipulability measure for this arm.
Code for the animation, joint-angle form
The course code is right for θ2 measured from the x-axis. To sweep the real elbow joint, change the two x2, y2 lines. Two more fixes from the reports: 30 of 40 write an "Uncompressed AVI", which stores every pixel of every frame (about 0.7 MB a frame at MATLAB's default 560 × 420 figure, about 70 MB for 100 frames); 'MPEG-4' (Windows and macOS; 'Motion JPEG AVI' on Linux) is far smaller. And use cosd/sind, or convert to radians: cos(30) in MATLAB or Python is the cosine of 30 radians.
l1 = 1; l2 = 0.5; % link lengths, m
theta1 = linspace(0, 90, 10); % joint 1, degrees
theta2 = linspace(0, 90, 10); % joint 2, from link 1
v = VideoWriter('arm.mp4', 'MPEG-4'); % not 'Uncompressed AVI'
open(v)
for i = 1:length(theta1)
for j = 1:length(theta2)
x1 = l1*cosd(theta1(i)); y1 = l1*sind(theta1(i));
x2 = x1 + l2*cosd(theta1(i) + theta2(j));
y2 = y1 + l2*sind(theta1(i) + theta2(j));
plot([0 x1 x2], [0 y1 y2], '-o', 'LineWidth', 3)
axis equal; axis([-0.6 1.6 -0.1 1.6])
writeVideo(v, getframe(gcf))
end
end
close(v)The same in Python with NumPy and Matplotlib (save the frames, then join them). We ran this version end to end: 100 frames, a 10-second video, last tip at (−0.5, 1.0).
import numpy as np
import matplotlib.pyplot as plt
l1, l2 = 1.0, 0.5
th = np.radians(np.linspace(0, 90, 10))
for i, t1 in enumerate(th):
for j, t2 in enumerate(th):
x1, y1 = l1*np.cos(t1), l1*np.sin(t1)
x2, y2 = x1 + l2*np.cos(t1 + t2), y1 + l2*np.sin(t1 + t2)
plt.plot([0, x1, x2], [0, y1, y2], '-o', lw=3)
plt.xlim(-0.6, 1.6); plt.ylim(-0.1, 1.6); plt.gca().set_aspect('equal')
plt.savefig(f'frame_{i*10 + j:03d}.png'); plt.clf()
# then: ffmpeg -framerate 10 -i frame_%03d.png arm.mp4Which mistakes does "Check my numbers" catch?
Type the tip position your program printed for a given θ1, θ2, L1 and L2. The panel recognises:
- The other angle convention: x2 = x1 + l2·cos θ2 when θ2 is meant as the joint angle, or the reverse.
- Degrees fed to cos and sin. At 30° and 45° the x-axis form gives (0.417, -0.563) instead of (1.220, 0.854): a negative y for an arm pointing up is the giveaway. 36 of the 40 reports avoid it with cosd and sind.
- A link length left out: x2 = x1 + cosd(θ2) draws link 2 one unit long. One report in our sample does this, with l2 = 0.5 declared and unused.
- θ1 typed in place of θ2 in the x2 line, link 2's own offsets reported as the tip, and cos and sin swapped.
What this calculator does not cover
- Arms with three or more joints, 3D arms and wrists. 2R planar only. The three-link and DH-table cases are in the robotics book.
- Joint limits and obstacles. Inverse kinematics gives both mathematical solutions; whether the elbow can bend that far is up to your arm.
- Dynamics. No masses, torques or motor sizing. See the book's chapter on manipulator dynamics.
- Trajectories. The sweep is a grid of poses, not a timed path; the animation plays 100 frames at 10 per second.
The 2R robotic arm is one of the weekly challenges in Skill-Lync's MATLAB and Python courses for mechanical engineers, next to ODE solvers, curve fitting, engine cycles and genetic algorithms.
Frequently asked questions
What are the forward kinematics equations of a 2R robotic arm?
With joint angles θ1 (link 1 from the x-axis) and θ2 (link 2 from link 1): x = L1·cos θ1 + L2·cos(θ1 + θ2) and y = L1·sin θ1 + L2·sin(θ1 + θ2). For L1 = 1 m, L2 = 0.5 m, θ1 = 30° and θ2 = 45°, the tip is at (0.9954, 0.9830) m. If your θ2 is measured from the x-axis instead, drop the θ1 inside the brackets.
How do you solve the inverse kinematics of a 2-link arm?
Law of cosines for the elbow, cos θ2 = (x² + y² − L1² − L2²)/(2·L1·L2), then θ1 = atan2(y, x) − atan2(L2·sin θ2, L1 + L2·cos θ2). Both signs of θ2 work, so a reachable point has two solutions: elbow down and elbow up. For L1 = 1, L2 = 0.5 and the target (1, 0.8): θ1 = 17.59°, θ2 = 67.05°, or θ1 = 59.73°, θ2 = -67.05°.
Why does the 2R arm code use x2 = x1 + l2·cos θ2?
Because there θ2 is the angle of link 2 from the x-axis, not the joint angle. It is a valid way to describe the arm, and it is how every report with code in our sample of 40 computes it. It is not what a motor at the elbow turns: that angle is θ2 − θ1. Textbooks, DH tables and robot controllers use the joint angle and write cos(θ1 + θ2).
What is the workspace of a two-link planar arm?
With full rotation at both joints, a ring (annulus) around the base from radius |L1 − L2| to L1 + L2, with area 4π·L1·L2. For L1 = 1 m and L2 = 0.5 m that is 0.5 m to 1.5 m and 6.283 m². Sweeping both angles only from 0 to 90° covers a small part of it: 12.5% with joint angles and 6.5% with θ2 measured from the x-axis.
When is a 2R arm singular?
When det J = L1·L2·sin θ2 = 0, so θ2 = 0° (arm fully stretched) or 180° (folded back). There the tip cannot move along the arm's own line, and inverse kinematics has one solution instead of two. In the course sweep with θ2 measured from the x-axis, 10 of the 100 frames are fully stretched (θ1 = θ2).
What is the difference between forward and inverse kinematics?
Forward kinematics goes from joint angles to the tip position, and always has exactly one answer. Inverse kinematics goes from a wanted tip position to the joint angles: for a 2R arm it has two answers, one, or none, depending on whether the point is inside the workspace, on its edge, or outside.
Related on Skill-Lync
- Robotics book: inverse kinematics of two- and three-link arms (interactive)
- Robotics book: the Jacobian, singularities and static forces
- Curve fitting calculator (the week 5 challenge of the same courses)
- Otto and Diesel cycle calculator (the air-standard cycle challenge)
- Python project ideas for mechanical engineers
- Student project: week 2, 2R robotic arm challenge
- All free engineering tools
Sources
- J. J. Craig, Introduction to Robotics: Mechanics and Control, 4th ed., Pearson, 2018: planar two-link kinematics, the law-of-cosines inverse solution, Jacobian and singularities.
- M. W. Spong, S. Hutchinson, M. Vidyasagar, Robot Modeling and Control, 2nd ed., Wiley, 2020: DH convention and the two-link planar arm (elbow-up and elbow-down solutions).
- T. Yoshikawa, "Manipulability of Robotic Mechanisms", International Journal of Robotics Research 4(2), 1985: the manipulability measure |det J|.
- MathWorks, cosd, getframe and VideoWriter documentation (profiles 'Uncompressed AVI' and 'MPEG-4').
- Skill-Lync, sample of 40 public student reports of the 2R robotic arm challenge, read 20 Nov 2026: language, link lengths, angle ranges, the x2, y2 formula and the video format used.
- Skill-Lync, scripts/verify/arm-2r.py: forward kinematics both ways and by the DH product, inverse kinematics in closed form and by a Newton solver from 24 starts, 2,000 random round trips, the course sweep and the workspace areas. This page recomputes its numbers in the browser code and checks them against that script at build time.