> For the complete documentation index, see [llms.txt](https://panav.gitbook.io/robotics-handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://panav.gitbook.io/robotics-handbook/foundations/calculus.md).

# Calculus

Calculus is the mathematical language of *change* - and robotics is the engineering of motion, sensing, and control. Every velocity comes from a derivative, every odometry estimate from an integral, every controller from minimizing a cost. This page covers the calculus concepts you actually use on a robot.

### Differentiation in Kinematics

Robotic motion is defined by a position function $$q(t)$$ of joint angles or Cartesian coordinates over time. Differentiation yields:

* **Velocity**

$$
\dot{q}(t) = \frac{d}{dt}q(t)
$$

* **Acceleration**

$$
\ddot{q}(t) = \frac{d^2}{dt^2} q(t)
$$

**Example - cubic joint trajectory.** For a single-joint rotary robot moving smoothly from $$\theta\_0$$ to $$\theta\_f$$ via a cubic in $t$:

$$
\theta(t) = a\_0 + a\_1 t + a\_2 t^2 + a\_3 t^3
$$

$$
\dot{\theta}(t) = a\_1 + 2 a\_2 t + 3 a\_3 t^2
$$

$$
\ddot{\theta}(t) = 2 a\_2 + 6 a\_3 t
$$

Cubics give continuous velocity but discontinuous jerk - fine for low-speed manipulators, painful for high-acceleration arms or AMRs. For those, use quintic polynomials or s-curves.

### Integration in Motion and Sensing

Integration accumulates rates into displacements or sensor estimates:

* **Position from velocity:** $$q(t) = q(t\_0) + \int\_{t\_0}^{t} \dot{q}(\tau), d\tau$$
* **Odometry:** integrating wheel velocities to track robot pose (and accumulating drift in the process - this is why you fuse with IMU/LiDAR).

**Example - constant acceleration:**

$$
v(t) = v\_0 + a,t
$$

$$
s(t) = s\_0 + v\_0,t + \tfrac{1}{2},a,t^2
$$

### Trajectory Planning

Smooth paths between waypoints require piecewise-polynomial fits with continuous position, velocity, and (often) acceleration. A common segment between times $$t\_i$$ and $$t\_{i+1}$$ is the cubic:

$$
q\_i(\tau) = C\_0 + C\_1 \tau + C\_2 \tau^2 + C\_3 \tau^3,\quad \tau \in \[0,,T]
$$

With boundary conditions tying segments together:

$$
q\_i(0) = q(t\_i),\quad \dot{q}\_i(0) = \dot{q}(t\_i)
$$

$$
q\_i(T) = q(t\_{i+1}),\quad \dot{q}*i(T) = \dot{q}(t*{i+1})
$$

This gives four equations for the four unknowns $$C\_0, C\_1, C\_2, C\_3$$. Higher-order splines (quintic, septic) add acceleration/jerk continuity at extra computational cost.

### Dynamics via Lagrange's Equations

Robot dynamics relate joint torques $$\boldsymbol{\tau}$$, positions $$\mathbf{q}$$, velocities $$\dot{\mathbf{q}}$$, and accelerations $$\ddot{\mathbf{q}}$$. The Euler-Lagrange formulation yields the canonical manipulator equation:

$$
M(\mathbf{q}),\ddot{\mathbf{q}} + C(\mathbf{q},\dot{\mathbf{q}}),\dot{\mathbf{q}} + \mathbf{g}(\mathbf{q}) = \boldsymbol{\tau}
$$

Where:

* $$M(\mathbf{q})$$ is the mass (inertia) matrix
* $$C(\mathbf{q},\dot{\mathbf{q}})$$ contains Coriolis and centrifugal terms
* $$\mathbf{g}(\mathbf{q})$$ is the gravity vector

Inverse dynamics (compute $$\boldsymbol{\tau}$$ from $$\ddot{\mathbf{q}}$$) is the basis for computed-torque control. Forward dynamics (compute $$\ddot{\mathbf{q}}$$ from $$\boldsymbol{\tau}$$) drives simulation.

### Control System Design

Calculus underlies feedback controllers and optimal control:

**PID control** combines proportional, integral, and derivative terms on tracking error $e(t)$:

$$
u(t) = K\_P,e(t) + K\_I \int\_0^{t} e(\tau),d\tau + K\_D,\frac{d}{dt},e(t)
$$

The integral term eliminates steady-state error; the derivative term provides damping. Tuning is mostly empirical - start with P, add D for damping, add I last and sparingly (it causes wind-up).

<figure><img src="https://923066007-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJR2yBCOFnSaxfostbWYx%2Fuploads%2Fgit-blob-0890f5122a64769781a0818b9645873c94ab8fa6%2Fpid-control.gif?alt=media" alt="PID step response changing shape as P, I, and D gains vary, showing overshoot, settling time, and steady-state error"><figcaption></figcaption></figure>

**Linear Quadratic Regulator (LQR)** solves the continuous-time algebraic Riccati equation to minimize a quadratic cost $$J = \int (\mathbf{x}^\top Q \mathbf{x} + \mathbf{u}^\top R \mathbf{u}),dt$$. The result is a state-feedback gain $$\mathbf{u} = -K\mathbf{x}$$ that's optimal for the linearized system.

**Model Predictive Control (MPC)** rolls the optimization forward in time over a finite horizon, re-solving every timestep. Heavier compute, but handles constraints (joint limits, obstacle avoidance) natively. See `../programming-for-robotics/optimization-libraries.md`.

### Perception and Computer Vision

In vision, image intensity $I(u, v, t)$ varies with camera/object motion. The **optical flow constraint equation** assumes brightness constancy:

$$
\frac{\partial I}{\partial t} + \nabla I \cdot \mathbf{v} = 0
$$

Where $$\partial I / \partial t$$ is the temporal derivative, $$\nabla I = (I\_u, I\_v)$$ is the spatial gradient, and $$\mathbf{v}$$ is the pixel velocity field. This single equation per pixel is under-constrained (two unknowns, one equation - the aperture problem), so methods like Lucas-Kanade add a smoothness assumption over a local window.

### Probability and Sensor Fusion (a teaser)

For state estimation (Kalman filters, particle filters), calculus shows up as:

* **Gaussian densities** - multiplying, marginalizing, conditioning all involve integrals.
* **Linearization** - the EKF approximates a nonlinear function $$f(\mathbf{x})$$ via its first-order Taylor expansion $$f(\mathbf{x}) \approx f(\mathbf{x}\_0) + J\_f(\mathbf{x}\_0)(\mathbf{x} - \mathbf{x}\_0)$$. The Jacobian $$J\_f$$ is pure calculus.
* **Optimization** - graph SLAM (g2o, GTSAM, Ceres) minimizes nonlinear least squares via Gauss-Newton or Levenberg-Marquardt, both gradient-based.

See `../slam-and-state-estimation/sensor-fusion.md` and `../programming-for-robotics/optimization-libraries.md` for how these become real code.

***

Calculus weaves through every layer of robotics - from physical motion and sensor fusion to high-level planning and optimal control. Master it once, and the same intuition transfers from PID tuning to bundle adjustment to training neural policies.
