The article proposes a nonlinear optimal control method for the model of the wheeled inverted pendulum (WIP). This is a difficult control and robotics problem due to the system's strong nonlinearities and due to its underactuation. First, the dynamic model of the WIP undergoes approximate linearization around a temporary operating point which is recomputed at each time step of the control method. The linearization procedure makes use of Taylor series expansion and of the computation of the associated Jacobian matrices. For the linearized model of the wheeled pendulum, an optimal (H-infinity) feedback controller is developed. The controller's gain is computed through the repetitive solution of an algebraic Riccati equation at each iteration of the control algorithm. The global asymptotic stability properties of the control method are proven through Lyapunov analysis. Finally, by using the H-infinity Kalman Filter as a robust state estimator, the implementation of a state estimation-based control scheme becomes also possible.