در کد باتری ساعت فعلی به ساعت بعدی اضافه نمیشود لطفا اصلاحش کن % Calculate excess…
- User
در کد باتری ساعت فعلی به ساعت بعدی اضافه نمیشود لطفا اصلاحش کن
% Calculate excess production for each homefor hour = 1:24 solarProduction = calculateSolarProduction(hour); windProduction = calculateWindProduction(hour); totalProduction = solarProduction + windProduction; usageLevel = initialUsageLevels(hour);
% Calculate excess production excessProduction(hour) = totalProduction - usageLevel; if excessProduction(hour) < 0 excessProduction(hour) = 0; % اگر تولید مازاد منفی باشد end
% Perform energy transactions between homes for j = 1:numHomes if i ~= j for hour = 1:24 batteryLevelHomeJ = batteryLevels(j, hour); usageLevelHomeJ = initialUsageLevels(hour);
% Check if home j needs energy if usageLevelHomeJ > batteryLevelHomeJ && buyingStatus(j, hour) == 0 && buyingStatus(i, hour) == 0
% Calculate how much home i can sell saleAmount = min([excessProduction(hour), batteryLevels(i, hour), usageLevelHomeJ - batteryLevelHomeJ]);
% Limit selling to 50% of battery capacity maxSaleAmount = 0.5 * batteryLevels(i, hour);
% Check if there is a sale amount if saleAmount > 0 saleAmount = min(saleAmount, maxSaleAmount);
salePrice = electricityPrices{i}(hour);
buyingStatus(i, hour) = 2; % home i is selling buyingStatus(j, hour) = 1; % home j is buying totalSales(i) = totalSales(i) + saleAmount; totalSalesMatrix(i, hour) = totalSalesMatrix(i, hour) + saleAmount; totalSaleCosts(i) = totalSaleCosts(i) + (salePrice * saleAmount);
% Update battery levels batteryLevels(i, hour) = batteryLevels(i, hour) - saleAmount; batteryLevels(j, hour) = min(BatteryCapacity, batteryLevels(j, hour) + saleAmount);
% Ensure that buyer's battery level is updated correctly if hour < 24 batteryLevels(j, hour + 1) = min(BatteryCapacity, batteryLevels(j, hour + 1) + excessProduction(hour + 1) - usageLevelHomeJ); % Carry forward to next hour end
totalPurchasesMatrix(j, hour) = totalPurchasesMatrix(j, hour) + saleAmount;
% Update excess production after sale excessProduction(hour) = excessProduction(hour) - saleAmount; end end
% Calculate battery charge and discharge if excessProduction(hour) > 0 && batteryLevels(i, hour) < BatteryCapacity chargeAmount = min(excessProduction(hour), BatteryCapacity - batteryLevels(i, hour)); batteryLevels(i, hour) = batteryLevels(i, hour) + chargeAmount; excessProduction(hour) = excessProduction(hour) - chargeAmount; % پس از شارژ، تولید مازاد به روز شود elseif batteryLevels(i, hour) < usageLevel && batteryLevels(i, hour) > 0 dischargeAmount = min(usageLevel - batteryLevels(i, hour), batteryLevels(i, hour)); batteryLevels(i, hour) = batteryLevels(i, hour) - dischargeAmount; در کد باتری ساعت فعلی به ساعت بعدی اضافه نمیشود لطفا اصلاحش کن
- Assistant
- User
- Assistant