An approach to the problem of collision avoidance of two unmanned vessels in certain area based on cost function minimization is presented in the paper. A script written in the MATLAB computational environment that calculates the optimal maneuver to prevent a collision is described. The cost function in this study is defined as the square of the difference between the safe distance and the Closest Point of Approach, and in order to find the optimal maneuver, it needs to be minimized, for which the fmincon (a MATLAB optimization function) is used in this code. The calculation of collision avoidance maneuvers is made “ from the perspective” of the VTS: it’s optimized for two vessels and allows the vessels to pass each other at a specified distance. The script, taking as input a matrix with data on pairs of approaching vessels (their x and y coordinates, speeds, and courses), by minimizing the cost function, calculates the optimal change in speeds and/or courses for two vessels, allowing them to pass each other at a safe distance. To verify the functionality of the script, a successful simulation is carried out in MATLAB. Several examples of its operation are given. These are situations where the closest point of approach (CPA) is grea-ter than safe one; situations where the CPA is less than safe distance, but the time to CPA is greater than safe time; and situations of dangerous convergence. The calculation results are illustrated with MATLAB graphs. The code of the script described in this paper can be further refined to work in conjunction with other algorithms, and it can also be used to create training datasets for training neural networks to predict safe maneuvers to prevent collisions of unmanned vessels at sea. In this study, the influence of wind and current is not considered, and the COLREGs‑72 is not also taken in consideration. Vessels can maneuver both to port and starboard, as well as reduce speed regardless of the type of approach situation.