This article focuses on creating an algorithm for the calculation of bending stiffness of an arbitrary polygonal cross section, including the first derivatives of this stiffness with respect to all the input variables. The coordinates of vertices of the cross section are also among these input variables. The algorithm is in principle based on dividing the cross section into trapezoids, calculating zero, first and second moment of area of these trapezoids, including partial derivatives with respect to all the input variables, and then compiling all these partial results into a final output. A DLL library based on this algorithm is then used in an optimization solver based on a reduced‑gradient method. This solver is put into practice to optimize the given cross section characteristics according to prescribed criteria.