for i = 2:length(timesteps)
excess_energy_kWh = E(i) - home_energy_use_kWh(i);
battery_energy_kWh(i) = min(battery_energy_kWh(i-1) + excess_energy_kWh, battery_capacity_kWh);
battery_energy_kWh(i) = max(battery_energy_kWh(i-1) + excess_energy_kWh, 0);
purchase_from_grid_kWh(i) = -excess_energy_kWh;
total_purchase_from_grid_kWh = sum(purchase_from_grid_kWh);
total_savings_with_solar_USD = total_purchase_from_grid_kWh * price_per_kWh_USD;
energy_from_company_kWh = sum(home_energy_use_kWh);
savings_without_solar_USD = energy_from_company_kWh * price_per_kWh_USD;
As shown above, I'm trying to create a model solar panel energy and revenue predictor. The only problem I'm having is that the revenue I get is different from the actual revenue, with the error being in this battery section. The battery capacity is 14 kWh, and this section is needed as there are days when the solar panel doesn't produce the required energy amount (home_energy_use_kWh). I've tried multiple ways to solve this, and none of them have worked. If needed, I can post the whole code and the datasheet. Thank you for your help.