  The structure of the ferroelectric phase of undoped KTiOPO$_4$ and its solid
solutions with zirconium and niobium is studied from first principles within
the density functional theory. The second-order nonlinear susceptibility tensor
and the spontaneous polarization of these materials are obtained. It is shown
that an improvement in nonlinear optical properties of KTiOPO$_4$ upon its
doping with Zr and Nb cannot be explained by a systematic change in the
composition of crystals and is apparently associated with the occurrence of
defects. Possible structures of such defects are discussed.
