Unmanned Aerial Vehicles(UAVs) are finding use in military applications, surveillance tasks, for environment monitoring, geological survey, etc. It is beneficial to use a team of coordinated UAVs rather than a single UAV, because multiple UAVs can complete the tasks with ease and more robustness than a single UAV. In this paper, a controller is proposed in optimal control framework for achieving any desired formation. By properly choosing formation vectors any formation can be achieved. A time varying formation vector is proposed which helps in achieving circular formation. In situations like loss of UAVs from formation group, the UAVs have to reconfigure for completing the task optimally maintaining the communication links. Reconfiguration has also been achieved in this framework. The controller is also capable of achieving encirclement of a target/immobilization of the target. From extensive simulations, it is seen that the UAVs are capable of achieving any formation; the formation can follow any desired trajectory, can achieve reconfiguration and encirclement of a stationary and moving target as well.