This paper addresses a problem of optimizing the operation of a gasoline direct injection (GDI) engine equipped with a Three Way Catalyst (TWC) and a Lean NOx Trap (LNT). This engine can be operated significantly lean of stoichiometry. Lean operation results in reduced pumping losses and improved fuel economy. During lean operation the LNT is used to trap the oxides of nitrogen (NOx) that constitute a regulated pollutant. As the LNT loading with NOx increases, its trapping efficiency decreases. The trap can be purged of the stored NOx by operating the engine at slightly rich of stoichiometry conditions. A trade-off has to be found between fuel economy, which deteriorates with longer purge time, and NOx reduction. It is shown how this problem can be cast in the setting of optimal control and solved numerically.