A nonlinear single rotary inverted pendulum is considered in this paper. A novel online algorithm, that is used to stabilize the rotary pendulum targeting the desired profiles based on analytical solutions, is presented. The new algorithm employs a single analytical function to create the pendulum angle and the velocity profiles. The reference pendulum angle is described by a series of time dependent exponential functions. Boundary conditions at the rest and the steady-state upright conditions are automatically satisfied. Moreover, target pendulum angle at the steady-state upright condition is ensured by evaluating one algorithm parameter (the quarter-way pendulum angle) for the reference pendulum angle and velocity functions. The quarter-way pendulum angle is numerically computed by integrating a simple link angular displacement differential equation. This new algorithm is capable of generating the well-suited torque that guarantees the stability of the system without utilizing any linear or nonlinear control methods. Furthermore, unlike the existing pendulum algorithms, the analytical scheme proposed in this work is able to successfully handle very large dispersions in initial pendulum angle while satisfying the desired states. Numerical simulations with random off-nominal conditions (with change in initial pendulum angle) are presented. The results demonstrated that the proposed algorithm is effective in performing the system stabilization with great performance characteristics.