In this paper an iterative algorithm for solving the generalized Sylvester matrix equations is firstly constructed by using the conjugate gradients and the matrix properties and the convergence of the algorithm is proved.Furthermore,when the equation is compatible the algorithm is proved to converge to the minimal norm solution to the problem.Then the algorithm is modified slightly to obtain the corresponding optimal approximation.Finally,numerical...