Optimal control of photovoltaic arrays