In this essay, a method is presented for approximating the optimal control problem (OCP) with nonlinear delay differential equations using global collocation at Legendre–Gauss–Radau points. This OCP models the spread of the computer virus. The operational matrices and collocation method together with hybrid Legendre polynomials and block-pulse functions are applied to discretize the model and convert it into a large-scale finite-dimensional nonlinear programming that can be solved by the existing well-developed methods in Matlab software. The numerical simulations show that the proposed collocation method leads to high precision solutions. Finally, the effects of the parameters that appeared in the model are analyzed. The experimental results demonstrate that the strategy for increasing the failure and the retrieval rates and decreasing the epidemic rate and the number of new computers gives a considerable influence on controlling and restraining the propagation of computer viruses.