Reconfigurable intelligent surfaces (RISs) are an emerging technology for enhancing the performance of wireless networks at a low and affordable cost, complexity, and power consumption. We introduce an algorithm for optimizing a single-input single-output RIS-assisted system in which the RIS is modeled by using an electromagnetic-compliant framework based on mutual impedances. More precisely, we provide the following new contributions: (i) in the absence of mutual coupling among the scattering elements of the RIS, we derive a closed-form expression for the optimal tunable impedances, which inherently accounts for the interplay between the amplitude and phase of the lumped loads of the RIS; and (ii) in the presence of mutual coupling, we introduce an iterative algorithm for optimizing the tunable impedances of the RIS. The algorithm is proved to be convergent by showing that the objective function is non-decreasing and upper bounded. Numerical results reveal that the mutual coupling among the scattering elements of the RIS significantly affects the end-to-end signal-to-noise ratio (SNR) if the inter-distance is less than half of the wavelength. If the RIS is optimized by explicitly taking into account the impact of mutual coupling, a better end-to-end SNR is obtained.