In this paper, a method based on neural networks for intelligently extracting weighting matrices of the optimal controllers’ cost function is presented. Despite the optimal and robust performance of controllers with the cost function, adjusting their gains which are the weighting matrices for the system state variables vector and the system inputs vector, is a challenging and time-consuming task that is usually selected by trial and error method for each specific application; and even little changes in the weighting matrices significantly impact problem-solving and system optimization. Therefore, it is necessary to select these gains automatically to improve controller performance and delete human energy to find the best gains. As a linear controller, linear quadratic regulator, and as a nonlinear controller, nonlinear model predictive control have been employed with trained networks to track the path of a wheeled mobile robot. The simulation and experimental results have been extracted and compared to validate the proposed method. These results have been demonstrated that the intelligent controller’s operation has lower error than the conventional method, which works up to 7% optimal in tracking and up to 19% better in angle state error; furthermore, as the most important aim, the required time and effort to find the weighting matrices in various situations has been omitted.