如何修复限制变量

问题描述 投票:0回答:1

我正在尝试创建一个太阳能电池板能源和收入预测模型。我遇到的唯一问题是我获得的收入与实际收入不同,错误出现在电池部分。电池容量为 14 kWh,需要此部分,因为有时太阳能电池板无法产生所需的能量 (home_energy_use_kWh)。我尝试了多种方法来解决这个问题,但没有一个有效。如果需要,我可以发布整个代码和数据表。谢谢您的帮助。

%% Calculate the amount of energy in the system's battery in each time step
for i = 2:length(timesteps)
    excess_energy_kWh = E(i) - home_energy_use_kWh(i); % Calculate excess energy
    if excess_energy_kWh > 0 % If excess energy is produced
        battery_energy_kWh(i) = min(battery_energy_kWh(i-1) + excess_energy_kWh, battery_capacity_kWh); % Charge battery
    else % If energy deficit
        battery_energy_kWh(i) = max(battery_energy_kWh(i-1) + excess_energy_kWh, 0); % Discharge battery
        purchase_from_grid_kWh(i) = -excess_energy_kWh; % Record grid purchase
    end
end
%% Calculate how much energy you will need to get from the energy company
total_purchase_from_grid_kWh = sum(purchase_from_grid_kWh);

% Calculate how much money you would save over the hypothetical period
total_savings_with_solar_USD = total_purchase_from_grid_kWh * price_per_kWh_USD;

% Calculate how much money you would save if you didn't use solar panels
energy_from_company_kWh = sum(home_energy_use_kWh);
% Use price_per_kWh_USD
savings_without_solar_USD = energy_from_company_kWh * price_per_kWh_USD;
matlab
1个回答
0
投票

我想问题出在能源短缺的情况下。想象一下,在某个时候

i
你有以下情况:

  • 电池能量为10kWh:
    battery_energy_kWh(i-1) = 10
  • 产生的能量为 5 kWh:
    E(i) = 5
  • 家庭能源使用量为7千瓦时:
    home_energy_use_kWh(i) = 7

因此,

excess_energy_kWh = 5 - 7 = - 2
,所以你会在else语句中。 在那里,你减少了电池能量,
battery_energy_kWh(i) = 10 + -2 = 8
,这是正确的,但你也从电网购买,
purchase_from_grid_kWh(i) = - -2 = 2
,我想这不应该发生。

如果您使用电池能源,则无需从电网购买能源,不是吗? 只有当电池电量低于 0 kWh 时,您才应该从电网购买,不是吗?

© www.soinside.com 2019 - 2024. All rights reserved.