In this paper, we consider the problem of determining an optimal trajectory for the execution of class of robot tasks using a learning-adaptive robot control systems. A quadratic cost functional which involves the reference trajectory and the actual control efforts is optimized on-line while the robot is learning how to execute the tasks. The control-optimization scheme presented in this paper has a hierarchical structure which consists of i) a trajectory tracking controller; ii) a 'learning' algorithm which estimates the robot dynamics; and iii) a gradient flow algorithm which attempts to minimize the cost functional using the current estimate of the robot dynamics, and generates the reference trajectory for the tracking controller. The stability of the overall control-optimization system is analyzed and the system is proved to be asymptotically stable. The reference trajectory generated by the gradient flow algorithm converges to a local minimum as long as the training tasks are sufficiently rich.