This paper presents a methodology to maximize the self-sufficiency or cost-effectiveness of grid-connected prosumers by optimizing the sizes of photovoltaic (PV) systems and electrochemical batteries. In the optimal sizing procedure, a limitation on the maximum injection in the grid can affect the energy flows, the economic effectiveness of the investments, and thus the sizing results. After the explanation of the procedure, a case study is presented, and a parametric analysis of the effect of possible injection limits is shown. The procedure is applied to size plants for an Italian domestic prosumer, whose electric load profile was measured for a year. A software program developed using the proposed methodology is also briefly presented. It is used for both research and educational purposes, both in laboratory classes and in remote lessons.