Modeling, nonlinear controller design, Lyapunov stability analysis, and MATLAB simulation for a two-wheeled inverted pendulum (TWIP / Segway) — an underactuated, inherently unstable nonlinear system. Two controllers, Sliding Mode Control (SMC) and Feedback Linearization (FL), are derived, proven stable, and compared on settling time, overshoot, and disturbance rejection.
MEEN 655 — Design of Nonlinear Control Systems · Texas A&M University
A Segway is a wheeled inverted pendulum (WIP): a body balanced on a wheeled base that must actively stabilize its upright tilt. It is:
- Nonlinear — tilt/translation coupling, gravity, trigonometric terms;
- Underactuated — fewer independent actuators than degrees of freedom;
-
Open-loop unstable — the upright equilibrium (
$\theta = 0$ ) diverges under the slightest perturbation.

Open-loop response — with no control, tilt and position blow up.
Using a Lagrangian formulation with state
Jacobian linearization reveals two equilibria: the upright position (
Sliding surface and reaching law:
A saturation function with boundary layer
Output
| Controller | Lyapunov function | Derivative | Result |
|---|---|---|---|
| SMC | Asymptotically stable; |
||
| FL | Stable for |
![]() SMC — tilt driven to upright |
![]() Feedback Linearization — smooth convergence |
![]() SMC rejects a 20 N·m disturbance spike at t = 3 s |
![]() SMC vs FL — head-to-head |
| Feature | Sliding Mode Control | Feedback Linearization |
|---|---|---|
| Settling time | Often faster | Slightly longer |
| Overshoot | Lower (sliding behavior) | Slightly higher |
| Disturbance rejection | Very strong (inherent robustness) | Weaker |
Conclusion: both stabilize the upright equilibrium, but SMC outperforms Feedback Linearization, primarily due to its inherent robustness to disturbances. Note the underactuation: tilt is stabilized while the wheel position
$x$ drifts (see06-position-drift-underactuation.png).
All simulations are pure MATLAB (ode45), no toolboxes required.
| File | Purpose | Generates |
|---|---|---|
TWIP_OpenLoop.m |
Uncontrolled dynamics (motivation) | Open-loop instability |
segway_smc_main.m + segway_dynamics.m |
Sliding Mode Controller w/ boundary-layer saturation | SMC stabilization |
TWIP_FL.m |
Feedback Linearization controller | FL stabilization |
ProjectCompare.m |
SMC vs FL + disturbance-rejection test | Comparison plots |
cd matlab
TWIP_OpenLoop % see why control is needed
segway_smc_main % sliding mode stabilization
TWIP_FL % feedback linearization
ProjectCompare % compare both + disturbance rejectionThe full IEEE-format report — derivations, Lyapunov proofs, and discussion — is in
docs/Segway_Nonlinear_Control_Report.pdf.
Code released under the MIT License. The report is © 2026 Ishan Sharma.
Authored by Ishan Sharma · M.S. Electrical Engineering, Texas A&M University · Control Systems & Robotics.



