This article presents a new two-axis solar tracker based on an online optimization algorithm so as to track the position of the sun without using its movement model. In this research, four well-known optimization algorithms are employed to find the two unknown parameters named azimuth and zenith angles, which determine the position of the sun. The magnitude of the sunray is considered as the cost function of all algorithms. Then, several experiments are carried out to find the best optimization algorithm with optimal population size, number of iterations, and also the best initialization method. Uniform initialization leads to faster convergence compared to random initialization. The results clearly show that the particle swarm optimization algorithm with a population size of 15 and 7 iterations using uniform initialization method has better performance than the other algorithms, with a convergence time of less than 40 s. The average fitness value or voltage received by the tracker is 2.4 Volts in this method, which is higher than other methods. TLBO also performs well with a population size of 15 and 7 iterations. Afterward, the artificial neural network with one hidden layer and 20 neurons is employed to predict these two parameters in each day and moment in a year in Shiraz city according to the experimental data extracted from PSO. Number of the day from January and the time are inputs and zenith and azimuth angles are considered the output of neural network modeling. The performance of the proposed ANN model is evaluated using regression plots, demonstrating a strong correlation between predicted and target outputs. Finally, the outcomes reveal the feasibility of using online optimization algorithms and neural network modeling in an effort to bypass the complex mathematical model of mechatronic systems and predict the movement of the sun automatically.