The first aim is handled by using energy balance. The energy balance between the load plus all losses and the output from PV system must be carried out to compute the optimum number of PV modules, Npv at each time step of the simulation. The hourly PV energy generated and hourly energy demand plus hourly losses of energy are compared with each other as the following equations to get the optimum number of PV modules.