The growing use of new technologies as electric vehicles and renewable energy sources (loads and generators with high nonlinear content), cause a deterioration in the voltage and current signals in the grid. The sensitive loads are significantly affected by these problems. In this way, an Unified Power Quality Conditioner (UPQC) with a Linear Quadratic Regulator optimal control is proposed with the incorporation of an internal model of sinusoidal signals. The objective is to compensate harmonic distortion and to guarantee correct RMS amplitude values for the load voltage and the grid current, at the same time, providing Power Factor correction. The proposed system attenuates the harmonic distortion of both signals with a THD lower than 5% on nonlinear loads with high power consumption. Additionally, the system reaches a effective compensation of sags and swells of long and short duration.