Azzera filtri
Azzera filtri

MATLAB code for fuzzy logic based battery scheduling for 24 hours in microgrid to minimize cost and grid dependency

16 visualizzazioni (ultimi 30 giorni)
% Define input variables
time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
solar_power = [0 0 0 0 0 0 0 0.008 0.15 0.301 0.418 0.478 0.956 0.842 0.315 0.169 0.022 0 0 0 0 0 0 0];
wind_power = [0.119 0.119 0.119 0.119 0.119 0.061 0.119 0.087 0.119 0.206 0.585 0.694 0.261 0.158 0.119 0.087 0.119 0.119 0.087 0.119 0.087 0.087 0.061 0.041];
microturbine_power = [6.0024 6 6.0065 6.0118 6.0024 6 6 6 6 6 6 6 6 6 6 6.006 6 6 6 6 6.0034 6.0036 6 6];
battery_soc = zeros(1, 24); % Initialize with zeros for 24 hours
power_demand = [52 50 50 51 56 53 70 75 76 80 78 74 72 72 76 80 85 88 90 87 78 71 65 56];
price_electricity = [0.23 0.19 0.14 0.12 0.12 0.20 0.23 0.38 1.50 4 4 4 1.5 4 2 1.95 0.6 0.41 0.35 0.43 1.17 0.54 0.30 0.26];
% Define input membership functions
fis = addInput(fis, 'input', 'BatterySoC', [0 100]);
fis = addMf(fis, 'input', 1, 'Low', 'gaussmf', [20 0]);
fis = addMf(fis, 'input', 2, 'Medium', 'gaussmf', [15 50]);
fis = addMf(fis, 'input', 3, 'High', 'gaussmf', [15 100]);
fis = addInput(fis, 'input', 'PowerDemand', [0 100]);
fis = addMf(fis, 'input', 1, 'Low', 'gaussmf', [15 0]);
fis = addMf(fis, 'input', 2, 'Medium', 'gaussmf', [15 50]);
fis = addMf(fis, 'input', 3, 'High', 'gaussmf', [15 100]);
fis = addInput(fis, 'input', 'PriceElectricity', [0 4]);
fis = addMf(fis, 'input', 1, 'Low', 'gaussmf', [0.2 0]);
fis = addMf(fis, 'input', 2, 'Medium', 'gaussmf', [0.2 2]);
fis = addMf(fis, 'input', 3, 'High', 'gaussmf', [0.2 4]);
fis = addInput(fis, 'input', 'SolarPower', [0 1]);
fis = addMf(fis, 'input', 1, 'Low', 'gaussmf', [0.2 0]);
fis = addMf(fis, 'input', 2, 'Medium', 'gaussmf', [0.2 0.5]);
fis = addMf(fis, 'input', 3, 'High', 'gaussmf', [0.2 1]);
fis = addInput(fis, 'input', 'WindPower', [0 1]);
fis = addMf(fis, 'input', 1, 'Low', 'gaussmf', [0.2 0]);
fis = addMf(fis, 'input', 2, 'Medium', 'gaussmf', [0.2 0.5]);
fis = addMf(fis, 'input', 3, 'High', 'gaussmf', [0.2 1]);
fis = addInput(fis, 'input', 'MicroturbinePower', [0 6.5]);
fis = addMf(fis, 'input', 1, 'Low', 'gaussmf', [0 2]);
fis = addMf(fis, 'input', 2, 'Medium', 'gaussmf', [1 3.5]);
fis = addMf(fis, 'input', 3, 'High', 'gaussmf', [3 6.5]);
% Define output membership functions
fis = addInput(fis, 'output', 'BatteryPower', [-5 5]);
fis = addMf(fis, 'output', 1, 'Discharge', 'gaussmf', [0.5 -5]);
fis = addMf(fis, 'output', 2, 'NoChange', 'gaussmf', [0.5 0]);
fis = addMf(fis, 'output', 3, 'Charge', 'gaussmf', [0.5 5]);
% Define rules
ruleList = [
FLrules
0 0 0 0 0
0 0 0 0 1
0 0 0 0 2
0 0 0 1 0
0 0 0 1 1
0 0 0 1 2
0 0 0 2 0
0 0 0 2 1
0 0 0 2 2
0 0 1 0 0
0 0 1 0 1
0 0 1 0 2
0 0 1 1 0
0 0 1 1 1
0 0 1 1 2
0 0 1 2 0
0 0 1 2 1
0 0 1 2 2
0 0 2 0 0
0 0 2 0 1
0 0 2 0 2
0 0 2 1 0
0 0 2 1 1
0 0 2 1 2
0 0 2 2 0
0 0 2 2 1
0 0 2 2 2
0 1 0 0 0
0 1 0 0 1
0 1 0 0 2
0 1 0 1 0
0 1 0 1 1
0 1 0 1 2
0 1 0 2 0
0 1 0 2 1
0 1 0 2 2
0 1 1 0 0
0 1 1 0 1
0 1 1 0 2
0 1 1 1 0
0 1 1 1 1
0 1 1 1 2
0 1 1 2 0
0 1 1 2 1
0 1 1 2 2
0 1 2 0 0
0 1 2 0 1
0 1 2 0 2
0 1 2 1 0
0 1 2 1 1
0 1 2 1 2
0 1 2 2 0
0 1 2 2 1
0 1 2 2 2
0 2 0 0 0
0 2 0 0 1
0 2 0 0 2
0 2 0 1 0
0 2 0 1 1
0 2 0 1 2
0 2 0 2 0
0 2 0 2 1
0 2 0 2 2
0 2 1 0 0
0 2 1 0 1
0 2 1 0 2
0 2 1 1 0
0 2 1 1 1
0 2 1 1 2
0 2 1 2 0
0 2 1 2 1
0 2 1 2 2
0 2 2 0 0
0 2 2 0 1
0 2 2 0 2
0 2 2 1 0
0 2 2 1 1
0 2 2 1 2
0 2 2 2 0
0 2 2 2 1
0 2 2 2 2
1 0 0 0 0
1 0 0 0 1
1 0 0 0 2
1 0 0 1 0
1 0 0 1 1
1 0 0 1 2
1 0 0 2 0
1 0 0 2 1
1 0 0 2 2
1 0 1 0 0
1 0 1 0 1
1 0 1 0 2
1 0 1 1 0
1 0 1 1 1
1 0 1 1 2
1 0 1 2 0
1 0 1 2 1
1 0 1 2 2
1 0 2 0 0
1 0 2 0 1
1 0 2 0 2
1 0 2 1 0
1 0 2 1 1
1 0 2 1 2
1 0 2 2 0
1 0 2 2 1
1 0 2 2 2
1 1 0 0 0
1 1 0 0 1
1 1 0 0 2
1 1 0 1 0
1 1 0 1 1
1 1 0 1 2
1 1 0 2 0
1 1 0 2 1
1 1 0 2 2
1 1 1 0 0
1 1 1 0 1
1 1 1 0 2
1 1 1 1 0
1 1 1 1 1
1 1 1 1 2
1 1 1 2 0
1 1 1 2 1
1 1 1 2 2
1 1 2 0 0
1 1 2 0 1
1 1 2 0 2
1 1 2 1 0
1 1 2 1 1
1 1 2 1 2
1 1 2 2 0
1 1 2 2 1
1 1 2 2 2
1 2 0 0 0
1 2 0 0 1
1 2 0 0 2
1 2 0 1 0
1 2 0 1 1
1 2 0 1 2
1 2 0 2 0
1 2 0 2 1
1 2 0 2 2
1 2 1 0 0
1 2 1 0 1
1 2 1 0 2
1 2 1 1 0
1 2 1 1 1
1 2 1 1 2
1 2 1 2 0
1 2 1 2 1
1 2 1 2 2
1 2 2 0 0
1 2 2 0 1
1 2 2 0 2
1 2 2 1 0
1 2 2 1 1
1 2 2 1 2
1 2 2 2 0
1 2 2 2 1
1 2 2 2 2
2 0 0 0 0
2 0 0 0 1
2 0 0 0 2
2 0 0 1 0
2 0 0 1 1
2 0 0 1 2
2 0 0 2 0
2 0 0 2 1
2 0 0 2 2
2 0 1 0 0
2 0 1 0 1
2 0 1 0 2
2 0 1 1 0
2 0 1 1 1
2 0 1 1 2
2 0 1 2 0
2 0 1 2 1
2 0 1 2 2
2 0 2 0 0
2 0 2 0 1
2 0 2 0 2
2 0 2 1 0
2 0 2 1 1
2 0 2 1 2
2 0 2 2 0
2 0 2 2 1
2 0 2 2 2
2 1 0 0 0
2 1 0 0 1
2 1 0 0 2
2 1 0 1 0
2 1 0 1 1
2 1 0 1 2
2 1 0 2 0
2 1 0 2 1
2 1 0 2 2
2 1 1 0 0
2 1 1 0 1
2 1 1 0 2
2 1 1 1 0
2 1 1 1 1
2 1 1 1 2
2 1 1 2 0
2 1 1 2 1
2 1 1 2 2
2 1 2 0 0
2 1 2 0 1
2 1 2 0 2
2 1 2 1 0
2 1 2 1 1
2 1 2 1 2
2 1 2 2 0
2 1 2 2 1
2 1 2 2 2
2 2 0 0 0
2 2 0 0 1
2 2 0 0 2
2 2 0 1 0
2 2 0 1 1
2 2 0 1 2
2 2 0 2 0
2 2 0 2 1
2 2 0 2 2
2 2 1 0 0
2 2 1 0 1
2 2 1 0 2
2 2 1 1 0
2 2 1 1 1
2 2 1 1 2
2 2 1 2 0
2 2 1 2 1
2 2 1 2 2
2 2 2 0 0
2 2 2 0 1
2 2 2 0 2
2 2 2 1 0
2 2 2 1 1
2 2 2 1 2
2 2 2 2 0
2 2 2 2 1
2 2 2 2 2
];
fis = addrule(fis, ruleList);
% Initialize variables
battery_capacity = 30; % kWh
battery_soc(1) = 100; % Initial state of charge in percentage
% Fuzzy inference for each hour
for hour = 1:24
input = [
battery_soc(hour), ...
power_demand(hour), ...
price_electricity(hour), ...
solar_power(hour), ...
wind_power(hour), ...
microturbine_power(hour)
];
% Perform fuzzy inference
output = evalfis(input, fis);
% Update battery state of charge
if output < 0
% Discharging
battery_soc(hour + 1) = battery_soc(hour) + output / battery_capacity * 100;
elseif output > 0
% Charging
battery_soc(hour + 1) = battery_soc(hour) + output / battery_capacity * 100;
else
% No change
battery_soc(hour + 1) = battery_soc(hour);
end
% Ensure battery SoC stays within [0, 100] range
battery_soc(hour + 1) = max(0, min(100, battery_soc(hour + 1)));
% Display results
fprintf('Hour %d: Battery SoC = %.2f%%, Battery Power = %.2f kW\n', hour, battery_soc(hour), output);
if output > 0
fprintf('Battery is charging with %.2f kW\n', output);
elseif output < 0
fprintf('Battery is discharging with %.2f kW\n', -output);
else
fprintf('Battery is neither charging nor discharging\n');
end
end

Risposta accettata

Sam Chak
Sam Chak il 19 Set 2023
Modificato: Sam Chak il 20 Set 2023
There are many syntax errors because the name and behavior of some functions in Fuzzy Logic Toolbox have changed since R2018b version. I corrected some. Perhaps you can follow up, and post the revised code here again, so that I can check.
% Define input variables
time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
solar_power = [0 0 0 0 0 0 0 0.008 0.15 0.301 0.418 0.478 0.956 0.842 0.315 0.169 0.022 0 0 0 0 0 0 0];
wind_power = [0.119 0.119 0.119 0.119 0.119 0.061 0.119 0.087 0.119 0.206 0.585 0.694 0.261 0.158 0.119 0.087 0.119 0.119 0.087 0.119 0.087 0.087 0.061 0.041];
microturbine_power = [6.0024 6 6.0065 6.0118 6.0024 6 6 6 6 6 6 6 6 6 6 6.006 6 6 6 6 6.0034 6.0036 6 6];
battery_soc = zeros(1, 24); % Initialize with zeros for 24 hours
power_demand = [52 50 50 51 56 53 70 75 76 80 78 74 72 72 76 80 85 88 90 87 78 71 65 56];
price_electricity = [0.23 0.19 0.14 0.12 0.12 0.20 0.23 0.38 1.50 4 4 4 1.5 4 2 1.95 0.6 0.41 0.35 0.43 1.17 0.54 0.30 0.26];
% Create a Mamdani fuzzy inference system
fis = mamfis('Name', "Fuzzy_BatterySoC_Ctrl"); % <-- this line was missing previously
% Define input membership functions
fis = addInput(fis, [0 100], 'Name', 'BatterySoC');
fis = addMF(fis, 'BatterySoC', 'gaussmf', [20 0], 'Name', 'Low');
fis = addMF(fis, 'BatterySoC', 'gaussmf', [15 50], 'Name', 'Medium');
fis = addMF(fis, 'BatterySoC', 'gaussmf', [15 100], 'Name', 'High');
fis = addInput(fis, [0 100], 'Name', 'PowerDemand');
fis = addMF(fis, 'PowerDemand', 'gaussmf', [15 0], 'Name', 'Low');
fis = addMF(fis, 'PowerDemand', 'gaussmf', [15 50], 'Name', 'Medium');
fis = addMF(fis, 'PowerDemand', 'gaussmf', [15 100], 'Name', 'High');
fis = addInput(fis, [0 4], 'Name', 'PriceElectricity');
fis = addMF(fis, 'PriceElectricity', 'gaussmf', [0.2 0], 'Name', 'Low');
fis = addMF(fis, 'PriceElectricity', 'gaussmf', [0.2 2], 'Name', 'Medium');
fis = addMF(fis, 'PriceElectricity', 'gaussmf', [0.2 4], 'Name', 'High');
fis = addInput(fis, [0 1], 'Name', 'SolarPower');
fis = addMF(fis, 'SolarPower', 'gaussmf', [0.2 0.0], 'Name', 'Low');
fis = addMF(fis, 'SolarPower', 'gaussmf', [0.2 0.5], 'Name', 'Medium');
fis = addMF(fis, 'SolarPower', 'gaussmf', [0.2 1.0], 'Name', 'High');
fis = addInput(fis, [0 1], 'Name', 'WindPower');
fis = addMF(fis, 'WindPower', 'gaussmf', [0.2 0.0], 'Name', 'Low');
fis = addMF(fis, 'WindPower', 'gaussmf', [0.2 0.5], 'Name', 'Medium');
fis = addMF(fis, 'WindPower', 'gaussmf', [0.2 1.0], 'Name', 'High');
fis = addInput(fis, [0 6.5], 'Name', 'MicroturbinePower');
fis = addMF(fis, 'MicroturbinePower', 'gaussmf', [1 2.0], 'Name', 'Low'); % check sigma
fis = addMF(fis, 'MicroturbinePower', 'gaussmf', [1 3.5], 'Name', 'Medium');
fis = addMF(fis, 'MicroturbinePower', 'gaussmf', [3 6.5], 'Name', 'High');
figure(1)
TL = tiledlayout(3, 2);
nexttile()
plotmf(fis, 'input', 1)
nexttile()
plotmf(fis, 'input', 2)
nexttile()
plotmf(fis, 'input', 3)
nexttile()
plotmf(fis, 'input', 4)
nexttile()
plotmf(fis, 'input', 5)
nexttile()
plotmf(fis, 'input', 6)
title(TL, 'Input MFs of Fuzzy Battery Variables')
% Define output membership functions
fis = addOutput(fis, [-5 5], 'Name', 'BatteryPower');
fis = addMF(fis, 'BatteryPower', 'gaussmf', [0.5 -5], 'Name', 'Discharge');
fis = addMF(fis, 'BatteryPower', 'gaussmf', [0.5 0], 'Name', 'NoChange');
fis = addMF(fis, 'BatteryPower', 'gaussmf', [0.5 5], 'Name', 'Charge');
figure(2)
plotmf(fis, 'output', 1), grid on
title('Output MFs of BatteryPower')
  13 Commenti
REENA
REENA il 24 Set 2023
% Define input variables
%time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
SP = [0 0 0 0 0 0 0 0.008 0.15 0.301 0.418 0.478 0.956 0.842 0.315 0.169 0.022 0 0 0 0 0 0 0];
WP = [0.119 0.119 0.119 0.119 0.119 0.061 0.119 0.087 0.119 0.206 0.585 0.694 0.261 0.158 0.119 0.087 0.119 0.119 0.087 0.119 0.087 0.087 0.061 0.041];
MTP = [36.0024 36 36.0065 36.0118 36.0024 36 36 36 36 36 36 36 36 36 36 36.006 36 36 36 36 36.0034 36.0036 36 36];
BSoC = zeros(1, 24); % Initialize with zeros for 24 hours
PD = [52 50 50 51 56 53 70 75 76 80 78 74 72 72 76 80 85 88 90 87 78 71 65 56];
Pgen= [36.12 36.12 36.13 36.13 36.12 36.06 36.12 36.09 36.27 36.51 37.00 37.17 37.22 37.00 36.43 36.26 36.14 36.12 36.09 36.12 36.09 36.09 36.06 36.04];
%EP = [0.23 0.19 0.14 0.12 0.12 0.20 0.23 0.38 1.50 4 4 4 1.5 4 2 1.95 0.6 0.41 0.35 0.43 1.17 0.54 0.30 0.26];
%Pgen= SP+WP+MTP;
%Output= Pgen-power_demand;
% Create a Mamdani fuzzy inference system
fis = mamfis('Name', "Fuzzy_BatteryAction");
% Define input membership functions
fis = addInput(fis, [0 100], 'Name', 'BSoC');
fis = addMF(fis, 'BSoC', 'trimf', [0 20 30], 'Name', 'Low');
fis = addMF(fis, 'BSoC', 'trimf', [20 50 80], 'Name', 'Medium');
fis = addMF(fis, 'BSoC', 'trimf', [70 85 100], 'Name', 'High');
fis = addInput(fis, [50 100], 'Name', 'PD');
fis = addMF(fis, 'PD', 'trimf', [50 60 70], 'Name', 'Low');
fis = addMF(fis, 'PD', 'trimf', [65 75 85], 'Name', 'Medium');
fis = addMF(fis, 'PD', 'trimf', [80 90 100], 'Name', 'High');
fis = addInput(fis, [35 40], 'Name', 'Pgen');
fis = addMF(fis, 'Pgen', 'trimf', [35 35.5 36], 'Name', 'Low');
fis = addMF(fis, 'Pgen', 'trimf', [ 36 36.5 37], 'Name', 'Medium');
fis = addMF(fis, 'Pgen', 'trimf', [37 38 39], 'Name', 'High');
% Define output membership functions
fis = addOutput(fis, [0 1], 'Name', 'BatteryAction');
fis = addMF(fis, 'BatteryAction', 'trimf', [0 0.3 0.6],'Name','charge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.4 0.7 0.9],'Name','Discharge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.8 0.9 1],'Name','Float');
% Define rules
ruleList = [
1 1 1 3 1 1
2 1 1 2 1 1
3 1 1 2 1 1
1 2 1 3 1 1
2 2 1 2 1 1
3 2 1 2 1 1
1 3 1 3 1 1
2 3 1 2 1 1
3 3 1 2 1 1
1 1 2 1 1 1
2 1 2 1 1 1
3 1 2 3 1 1
1 2 2 3 1 1
2 2 2 2 1 1
3 2 2 2 1 1
1 3 2 3 1 1
2 3 2 2 1 1
3 3 2 2 1 1
1 1 3 1 1 1
2 1 3 2 1 1
3 1 3 2 1 1
1 2 3 1 1 1
2 2 3 2 1 1
3 2 3 2 1 1
1 3 3 1 1 1
2 3 3 2 1 1
3 3 3 2 1 1
];
fis = addRule(fis, ruleList);
% Initialize variables
battery_capacity = 30; % kW
BSoC(1) = 100; % Initial state of charge of the battery in percentage
% Fuzzy inference for each hour
for hour = 1:24
input = [
BSoC(hour), ...
PD(hour), ...
Pgen(hour),...
];
% Perform fuzzy inference
output = evalfis(fis, input );
% Update battery state of charge
if output < 0
% Discharging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
elseif output > 0
% Charging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
else
% No change
BSoC(hour + 1) = BSoC(hour);
end
% Ensure battery SoC stays within [20, 80] range
BSoC(hour + 1) = max(20, min(80, BSoC(hour + 1)));
% Display results
fprintf('Hour %d: Battery SoC = %.2f, Battery Power = %.2f \n', hour, BSoC(hour), output);
if output > 0
fprintf('Battery is charging with %.2f kW\n', output);
elseif output < 0
fprintf('Battery is discharging with %.2f kW\n', -output);
else
fprintf('Battery is neither charging nor discharging\n');
end
end
Sam Chak
Sam Chak il 24 Set 2023
Hi @REENA,
The designs of some membership functions (MFs) over the universe of discourse of each fuzzy variable sometimes result in no rules being fired for the Output (Battery Action) in certain situations. This leads to the defuzzified output value being set to its mean range value of 0.5. I have run some tests in the code below. You can see the eight situations that result in a "No rules fired for Output 1" warning. The MFs should be designed to cover the entire universe of discourse of each fuzzy variable.
If you can explain why the MFs are designed this way, perhaps I can guide you, although I am not a Battery Expert.
% Define input variables
% time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
SP = [0 0 0 0 0 0 0 0.008 0.15 0.301 0.418 0.478 0.956 0.842 0.315 0.169 0.022 0 0 0 0 0 0 0];
WP = [0.119 0.119 0.119 0.119 0.119 0.061 0.119 0.087 0.119 0.206 0.585 0.694 0.261 0.158 0.119 0.087 0.119 0.119 0.087 0.119 0.087 0.087 0.061 0.041];
MTP = [36.0024 36 36.0065 36.0118 36.0024 36 36 36 36 36 36 36 36 36 36 36.006 36 36 36 36 36.0034 36.0036 36 36];
BSoC = zeros(1, 24); % Initialize with zeros for 24 hours
PD = [52 50 50 51 56 53 70 75 76 80 78 74 72 72 76 80 85 88 90 87 78 71 65 56];
Pgen = [36.12 36.12 36.13 36.13 36.12 36.06 36.12 36.09 36.27 36.51 37.00 37.17 37.22 37.00 36.43 36.26 36.14 36.12 36.09 36.12 36.09 36.09 36.06 36.04];
% EP = [0.23 0.19 0.14 0.12 0.12 0.20 0.23 0.38 1.50 4 4 4 1.5 4 2 1.95 0.6 0.41 0.35 0.43 1.17 0.54 0.30 0.26];
% Pgen = SP + WP + MTP;
% Output = Pgen - power_demand;
% Create a Mamdani fuzzy inference system
fis = mamfis('Name', "Fuzzy_BatteryAction");
% Define membership functions for 3 Fuzzy input variables
fis = addInput(fis, [0 100], 'Name', 'BSoC');
fis = addMF(fis, 'BSoC', 'trimf', [ 0 20 30], 'Name', 'Low');
fis = addMF(fis, 'BSoC', 'trimf', [20 50 80], 'Name', 'Medium');
fis = addMF(fis, 'BSoC', 'trimf', [70 85 100], 'Name', 'High');
fis = addInput(fis, [50 100], 'Name', 'PD');
fis = addMF(fis, 'PD', 'trimf', [50 60 70], 'Name', 'Low');
fis = addMF(fis, 'PD', 'trimf', [65 75 85], 'Name', 'Medium');
fis = addMF(fis, 'PD', 'trimf', [80 90 100], 'Name', 'High');
fis = addInput(fis, [35 40], 'Name', 'Pgen');
fis = addMF(fis, 'Pgen', 'trimf', [35 35.5 36], 'Name', 'Low');
fis = addMF(fis, 'Pgen', 'trimf', [36 36.5 37], 'Name', 'Medium');
fis = addMF(fis, 'Pgen', 'trimf', [37 38 39], 'Name', 'High');
figure(1)
TL = tiledlayout(3, 1);
nexttile()
plotmf(fis, 'input', 1), grid on, ylabel('\mu')
nexttile()
plotmf(fis, 'input', 2), grid on, ylabel('\mu')
nexttile()
plotmf(fis, 'input', 3), grid on, ylabel('\mu')
title(TL, 'Input MFs of Battery Variables')
% Define membership functions for 1 Fuzzy output variable
fis = addOutput(fis, [0 1], 'Name', 'BatteryAction');
fis = addMF(fis, 'BatteryAction', 'trimf', [0 0.3 0.6], 'Name', 'Charge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.4 0.7 0.9], 'Name', 'Discharge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.8 0.9 1.0], 'Name', 'Float');
figure(2)
plotmf(fis, 'output', 1), grid on
title('Output MFs of Battery Action')
% Define 27 rules
ruleList = [
1 1 1 3 1 1
2 1 1 2 1 1
3 1 1 2 1 1
1 2 1 3 1 1
2 2 1 2 1 1
3 2 1 2 1 1
1 3 1 3 1 1
2 3 1 2 1 1
3 3 1 2 1 1
1 1 2 1 1 1
2 1 2 1 1 1
3 1 2 3 1 1
1 2 2 3 1 1
2 2 2 2 1 1
3 2 2 2 1 1
1 3 2 3 1 1
2 3 2 2 1 1
3 3 2 2 1 1
1 1 3 1 1 1
2 1 3 2 1 1
3 1 3 2 1 1
1 2 3 1 1 1
2 2 3 2 1 1
3 2 3 2 1 1
1 3 3 1 1 1
2 3 3 2 1 1
3 3 3 2 1 1];
fis = addRule(fis, ruleList);
% Check if each rule is fired over the universe of discourse of each fuzzy variable
ruleview(fis)
Warning: ruleview will be removed in a future release. Use the Fuzzy Logic Designer app instead.
%% Running some tests
% Test 0: Mean of universe of discourse
out0 = evalfis(fis, [50 75 37.5])
out0 = 0.6611
% Test 1: Left extreme of universe of discourse of BSoC
out1 = evalfis(fis, [0 75 37.5])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out1 = 0.5000
% Test 2: Right extreme of universe of discourse of BSoC
out2 = evalfis(fis, [100 75 37.5])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out2 = 0.5000
% Test 3: Left extreme of universe of discourse of PD
out3 = evalfis(fis, [50 50 37.5])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out3 = 0.5000
% Test 4: Right extreme of universe of discourse of PD
out4 = evalfis(fis, [50 100 37.5])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out4 = 0.5000
% Test 5: Left extreme of universe of discourse of Pgen
out5 = evalfis(fis, [50 75 35])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out5 = 0.5000
% Test 6: When Pgen = 36
out6 = evalfis(fis, [50 75 36])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out6 = 0.5000
% Test 7: When Pgen = 37
out7 = evalfis(fis, [50 75 37])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out7 = 0.5000
% Test 8: Right extreme of universe of discourse of Pgen (39 < Pgen < 40)
out8 = evalfis(fis, [50 75 40])
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
out8 = 0.5000
%% ----- User-supplied Data -----
% Initialize variables
battery_capacity = 30; % kW
BSoC(1) = 100; % Initial state of charge of the battery in percentage
% Fuzzy inference for each hour
for hour = 1:24
input = [
BSoC(hour), ...
PD(hour), ...
Pgen(hour), ...
]
% Perform fuzzy inference
output = evalfis(fis, input)
% Update battery state of charge
if output < 0
% Discharging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
elseif output > 0
% Charging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
else
% No change
BSoC(hour + 1) = BSoC(hour);
end
% Ensure battery SoC stays within [20, 80] range
BSoC(hour + 1) = max(20, min(80, BSoC(hour + 1)));
% Display results
fprintf('Hour %d: Battery SoC = %.2f, Battery Power = %.2f \n', hour, BSoC(hour), output);
if output > 0
fprintf('Battery is charging with %.2f kW\n', output);
elseif output < 0
fprintf('Battery is discharging with %.2f kW\n', -output);
else
fprintf('Battery is neither charging nor discharging\n');
end
end
input = 1×3
100.0000 52.0000 36.1200
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
output = 0.5000
Hour 1: Battery SoC = 100.00, Battery Power = 0.50
Battery is charging with 0.50 kW
input = 1×3
80.0000 50.0000 36.1200
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
output = 0.5000
Hour 2: Battery SoC = 80.00, Battery Power = 0.50
Battery is charging with 0.50 kW
input = 1×3
80.0000 50.0000 36.1300
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
output = 0.5000
Hour 3: Battery SoC = 80.00, Battery Power = 0.50
Battery is charging with 0.50 kW
input = 1×3
80.0000 51.0000 36.1300
output = 0.9000
Hour 4: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 56.0000 36.1200
output = 0.9000
Hour 5: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 53.0000 36.0600
output = 0.9000
Hour 6: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 70.0000 36.1200
output = 0.6557
Hour 7: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 75.0000 36.0900
output = 0.6543
Hour 8: Battery SoC = 80.00, Battery Power = 0.65
Battery is charging with 0.65 kW
input = 1×3
80.0000 76.0000 36.2700
output = 0.6618
Hour 9: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 80.0000 36.5100
output = 0.6611
Hour 10: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80 78 37
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
output = 0.5000
Hour 11: Battery SoC = 80.00, Battery Power = 0.50
Battery is charging with 0.50 kW
input = 1×3
80.0000 74.0000 37.1700
output = 0.6540
Hour 12: Battery SoC = 80.00, Battery Power = 0.65
Battery is charging with 0.65 kW
input = 1×3
80.0000 72.0000 37.2200
output = 0.6552
Hour 13: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80 72 37
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.5.
output = 0.5000
Hour 14: Battery SoC = 80.00, Battery Power = 0.50
Battery is charging with 0.50 kW
input = 1×3
80.0000 76.0000 36.4300
output = 0.6639
Hour 15: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 80.0000 36.2600
output = 0.6611
Hour 16: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 85.0000 36.1400
output = 0.6566
Hour 17: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 88.0000 36.1200
output = 0.6557
Hour 18: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 90.0000 36.0900
output = 0.6543
Hour 19: Battery SoC = 80.00, Battery Power = 0.65
Battery is charging with 0.65 kW
input = 1×3
80.0000 87.0000 36.1200
output = 0.6557
Hour 20: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 78.0000 36.0900
output = 0.6543
Hour 21: Battery SoC = 80.00, Battery Power = 0.65
Battery is charging with 0.65 kW
input = 1×3
80.0000 71.0000 36.0900
output = 0.6543
Hour 22: Battery SoC = 80.00, Battery Power = 0.65
Battery is charging with 0.65 kW
input = 1×3
80.0000 65.0000 36.0600
output = 0.9000
Hour 23: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 56.0000 36.0400
output = 0.9000
Hour 24: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW

Accedi per commentare.

Più risposte (1)

Sam Chak
Sam Chak il 24 Set 2023
I can fix the warning message in the fuzzy system. Could you please interpret the meaning of the output values for me? Do they make sense to you? If they don't, then you may need to reshape the membership functions (MFs) of the output.
% Define input variables
% time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
SP = [0 0 0 0 0 0 0 0.008 0.15 0.301 0.418 0.478 0.956 0.842 0.315 0.169 0.022 0 0 0 0 0 0 0];
WP = [0.119 0.119 0.119 0.119 0.119 0.061 0.119 0.087 0.119 0.206 0.585 0.694 0.261 0.158 0.119 0.087 0.119 0.119 0.087 0.119 0.087 0.087 0.061 0.041];
MTP = [36.0024 36 36.0065 36.0118 36.0024 36 36 36 36 36 36 36 36 36 36 36.006 36 36 36 36 36.0034 36.0036 36 36];
BSoC = zeros(1, 24); % Initialize with zeros for 24 hours
PD = [52 50 50 51 56 53 70 75 76 80 78 74 72 72 76 80 85 88 90 87 78 71 65 56];
Pgen = [36.12 36.12 36.13 36.13 36.12 36.06 36.12 36.09 36.27 36.51 37.00 37.17 37.22 37.00 36.43 36.26 36.14 36.12 36.09 36.12 36.09 36.09 36.06 36.04];
% EP = [0.23 0.19 0.14 0.12 0.12 0.20 0.23 0.38 1.50 4 4 4 1.5 4 2 1.95 0.6 0.41 0.35 0.43 1.17 0.54 0.30 0.26];
% Pgen = SP + WP + MTP;
% Output = Pgen - power_demand;
% Create a Mamdani fuzzy inference system
fis = mamfis('Name', "Fuzzy_BatteryAction");
% Define membership functions for 3 Fuzzy input variables
fis = addInput(fis, [0 100], 'Name', 'BSoC');
fis = addMF(fis, 'BSoC', 'linzmf', [20 30], 'Name', 'Low');
fis = addMF(fis, 'BSoC', 'trimf', [20 50 80], 'Name', 'Medium');
fis = addMF(fis, 'BSoC', 'linsmf', [70 85], 'Name', 'High');
fis = addInput(fis, [50 100], 'Name', 'PD');
fis = addMF(fis, 'PD', 'linzmf', [60 70], 'Name', 'Low');
fis = addMF(fis, 'PD', 'trimf', [65 75 85], 'Name', 'Medium');
fis = addMF(fis, 'PD', 'linsmf', [80 90], 'Name', 'High');
fis = addInput(fis, [35 40], 'Name', 'Pgen');
fis = addMF(fis, 'Pgen', 'linzmf', [35.5 36], 'Name', 'Low');
fis = addMF(fis, 'Pgen', 'trimf', [35.5 36.5 37.5], 'Name', 'Medium');
fis = addMF(fis, 'Pgen', 'linsmf', [37 38], 'Name', 'High');
figure(1)
TL = tiledlayout(3, 1);
nexttile()
plotmf(fis, 'input', 1), grid on, ylabel('\mu')
nexttile()
plotmf(fis, 'input', 2), grid on, ylabel('\mu')
nexttile()
plotmf(fis, 'input', 3), grid on, ylabel('\mu')
title(TL, 'Input MFs of Battery Variables')
% Define membership functions for 1 Fuzzy output variable
fis = addOutput(fis, [0 1], 'Name', 'BatteryAction');
fis = addMF(fis, 'BatteryAction', 'trimf', [0 0.3 0.6], 'Name', 'Charge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.4 0.7 0.9], 'Name', 'Discharge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.8 0.9 1.0], 'Name', 'Float');
figure(2)
plotmf(fis, 'output', 1), grid on
title('Output MFs of Battery Action')
% Define 27 rules
ruleList = [
1 1 1 3 1 1
2 1 1 2 1 1
3 1 1 2 1 1
1 2 1 3 1 1
2 2 1 2 1 1
3 2 1 2 1 1
1 3 1 3 1 1
2 3 1 2 1 1
3 3 1 2 1 1
1 1 2 1 1 1
2 1 2 1 1 1
3 1 2 3 1 1
1 2 2 3 1 1
2 2 2 2 1 1
3 2 2 2 1 1
1 3 2 3 1 1
2 3 2 2 1 1
3 3 2 2 1 1
1 1 3 1 1 1
2 1 3 2 1 1
3 1 3 2 1 1
1 2 3 1 1 1
2 2 3 2 1 1
3 2 3 2 1 1
1 3 3 1 1 1
2 3 3 2 1 1
3 3 3 2 1 1];
fis = addRule(fis, ruleList);
% Check if each rule is fired over the universe of discourse of each fuzzy variable
ruleview(fis)
Warning: ruleview will be removed in a future release. Use the Fuzzy Logic Designer app instead.
%% ----- User-supplied Data -----
% Initialize variables
battery_capacity = 30; % kW
BSoC(1) = 100; % Initial state of charge of the battery in percentage
% Fuzzy inference for each hour
for hour = 1:24
input = [
BSoC(hour), ...
PD(hour), ...
Pgen(hour), ...
]
% Perform fuzzy inference
output = evalfis(fis, input)
% Update battery state of charge
if output < 0
% Discharging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
elseif output > 0
% Charging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
else
% No change
BSoC(hour + 1) = BSoC(hour);
end
% Ensure battery SoC stays within [20, 80] range
BSoC(hour + 1) = max(20, min(80, BSoC(hour + 1)));
% Display results
fprintf('Hour %d: Battery SoC = %.2f, Battery Power = %.2f \n', hour, BSoC(hour), output);
if output > 0
fprintf('Battery is charging with %.2f kW\n', output);
elseif output < 0
fprintf('Battery is discharging with %.2f kW\n', -output);
else
fprintf('Battery is neither charging nor discharging\n');
end
end
input = 1×3
100.0000 52.0000 36.1200
output = 0.9000
Hour 1: Battery SoC = 100.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 50.0000 36.1200
output = 0.9000
Hour 2: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 50.0000 36.1300
output = 0.9000
Hour 3: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 51.0000 36.1300
output = 0.9000
Hour 4: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 56.0000 36.1200
output = 0.9000
Hour 5: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 53.0000 36.0600
output = 0.9000
Hour 6: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 70.0000 36.1200
output = 0.6611
Hour 7: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 75.0000 36.0900
output = 0.6627
Hour 8: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 76.0000 36.2700
output = 0.6639
Hour 9: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 80.0000 36.5100
output = 0.6611
Hour 10: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80 78 37
output = 0.6611
Hour 11: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 74.0000 37.1700
output = 0.6576
Hour 12: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 72.0000 37.2200
output = 0.6566
Hour 13: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80 72 37
output = 0.6611
Hour 14: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 76.0000 36.4300
output = 0.6639
Hour 15: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 80.0000 36.2600
output = 0.6611
Hour 16: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 85.0000 36.1400
output = 0.6611
Hour 17: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 88.0000 36.1200
output = 0.6632
Hour 18: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 90.0000 36.0900
output = 0.6627
Hour 19: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 87.0000 36.1200
output = 0.6632
Hour 20: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 78.0000 36.0900
output = 0.6627
Hour 21: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 71.0000 36.0900
output = 0.6627
Hour 22: Battery SoC = 80.00, Battery Power = 0.66
Battery is charging with 0.66 kW
input = 1×3
80.0000 65.0000 36.0600
output = 0.9000
Hour 23: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
input = 1×3
80.0000 56.0000 36.0400
output = 0.9000
Hour 24: Battery SoC = 80.00, Battery Power = 0.90
Battery is charging with 0.90 kW
  3 Commenti
REENA
REENA il 24 Set 2023
Modificato: Sam Chak il 25 Set 2023
here i have done some modification and getting all the three battery status 'charging' %'discharging' and 'neither charging nor discharging' but still the output doesn't make %any sense(its 0,-0.24 and 0.58kW only at various hrs) and if we keep initial %soc(BSoC)=100% the for each hr(1 to 24) BSoC(hour+1) is 80 due to the line:
% BSoC(hour + 1) = max(20, min(80, BSoC(hour + 1))); and still ' battery is charging' is displayed.
i have kept this condition to prevent overcharging and discharging of the battery. such that BSoC should %remain within max and min limits i.e. 20% to 80%
% Define input variables
%time = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
SP = [0 0 0 0 0 0 0 0.008 0.15 0.301 0.418 0.478 0.956 0.842 0.315 0.169 0.022 0 0 0 0 0 0 0];
WP = [0.119 0.119 0.119 0.119 0.119 0.061 0.119 0.087 0.119 0.206 0.585 0.694 0.261 0.158 0.119 0.087 0.119 0.119 0.087 0.119 0.087 0.087 0.061 0.041];
MTP = [36.0024 36 36.0065 36.0118 36.0024 36 36 36 36 36 36 36 36 36 36 36.006 36 36 36 36 36.0034 36.0036 36 36];
BSoC = zeros(1, 24); % Initialize with zeros for 24 hours
PD = [52 50 50 51 56 53 70 75 76 80 78 74 72 72 76 80 85 88 90 87 78 71 65 56];
Pgen= [36.12 36.12 36.13 36.13 36.12 36.06 36.12 36.09 36.27 36.51 37.00 37.17 37.22 37.00 36.43 36.26 36.14 36.12 36.09 36.12 36.09 36.09 36.06 36.04];
%EP = [0.23 0.19 0.14 0.12 0.12 0.20 0.23 0.38 1.50 4 4 4 1.5 4 2 1.95 0.6 0.41 0.35 0.43 1.17 0.54 0.30 0.26];
%Pgen= SP+WP+MTP;
%Output= Pgen-power_demand;
% Create a Mamdani fuzzy inference system
fis = mamfis('Name', "Fuzzy_BatteryAction");
% Define input membership functions
fis = addInput(fis, [0 100], 'Name', 'BSoC');
fis = addMF(fis, 'BSoC', 'trimf', [0 20 30], 'Name', 'Low');
fis = addMF(fis, 'BSoC', 'trimf', [20 50 80], 'Name', 'Medium');
fis = addMF(fis, 'BSoC', 'trimf', [70 85 100], 'Name', 'High');
fis = addInput(fis, [50 100], 'Name', 'PD');
fis = addMF(fis, 'PD', 'trimf', [50 60 70], 'Name', 'Low');
fis = addMF(fis, 'PD', 'trimf', [65 75 85], 'Name', 'Medium');
fis = addMF(fis, 'PD', 'trimf', [80 90 100], 'Name', 'High');
fis = addInput(fis, [35 40], 'Name', 'Pgen');
fis = addMF(fis, 'Pgen', 'trimf', [35 35.5 36], 'Name', 'Low');
fis = addMF(fis, 'Pgen', 'trimf', [ 36 36.5 37], 'Name', 'Medium');
fis = addMF(fis, 'Pgen', 'trimf', [37 38 39], 'Name', 'High');
% Define output membership functions
% fis = addOutput(fis, [0 1], 'Name', 'BatteryAction');
% fis = addMF(fis, 'BatteryAction', 'trimf', [0 0.3 0.6],'Name','charge');
% fis = addMF(fis, 'BatteryAction', 'trimf', [0.4 0.7 0.9],'Name','Discharge');
% fis = addMF(fis, 'BatteryAction', 'trimf', [0.8 0.9 1],'Name','Float');
fis = addOutput(fis, [-1 1], 'Name', 'BatteryAction');
fis = addMF(fis, 'BatteryAction', 'trimf', [-1 0 0.5],'Name','charge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.25 0.6 0.9],'Name','Discharge');
fis = addMF(fis, 'BatteryAction', 'trimf', [0.8 0.9 1],'Name','Float');
% Define rules
ruleList = [
1 1 1 3 1 1
2 1 1 2 1 1
3 1 1 2 1 1
1 2 1 3 1 1
2 2 1 2 1 1
3 2 1 2 1 1
1 3 1 3 1 1
2 3 1 2 1 1
3 3 1 2 1 1
1 1 2 1 1 1
2 1 2 1 1 1
3 1 2 3 1 1
1 2 2 3 1 1
2 2 2 2 1 1
3 2 2 2 1 1
1 3 2 3 1 1
2 3 2 2 1 1
3 3 2 2 1 1
1 1 3 1 1 1
2 1 3 2 1 1
3 1 3 2 1 1
1 2 3 1 1 1
2 2 3 2 1 1
3 2 3 2 1 1
1 3 3 1 1 1
2 3 3 2 1 1
3 3 3 2 1 1
];
fis = addRule(fis, ruleList);
% Initialize variables
battery_capacity = 30; % kW
BSoC(1) = 40; % Initial state of charge of the battery in percentage
% Fuzzy inference for each hour
for hour = 1:24
input = [
BSoC(hour), ...
PD(hour), ...
Pgen(hour),...
];
% Perform fuzzy inference
output = evalfis(fis, input );
% Update battery state of charge
if output < 0
% Discharging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
elseif output > 0
% Charging
BSoC(hour + 1) = BSoC(hour) + output / battery_capacity * 100;
else
% No change
BSoC(hour + 1) = BSoC(hour);
end
% Ensure battery SoC stays within [20, 80] range
BSoC(hour + 1) = min(80, max(20, BSoC(hour + 1)));
%with %.2f kW
% Display results
fprintf('Hour %d: BSoC(hour+1)= %.2f, Battery Power = %.2f \n', hour, BSoC(hour), output);
if output > 0
fprintf('Battery is charging\n', output);
elseif output < 0
fprintf('Battery is discharging\n', -output);
else
fprintf('Battery is neither charging nor discharging\n');
end
end
Hour 1: BSoC(hour+1)= 40.00, Battery Power = -0.23
Battery is discharging
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.
Hour 2: BSoC(hour+1)= 39.25, Battery Power = 0.00
Battery is neither charging nor discharging
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.
Hour 3: BSoC(hour+1)= 39.25, Battery Power = 0.00
Battery is neither charging nor discharging
Hour 4: BSoC(hour+1)= 39.25, Battery Power = -0.24
Battery is discharging
Hour 5: BSoC(hour+1)= 38.45, Battery Power = -0.22
Battery is discharging
Hour 6: BSoC(hour+1)= 37.72, Battery Power = -0.24
Battery is discharging
Hour 7: BSoC(hour+1)= 36.93, Battery Power = 0.58
Battery is charging
Hour 8: BSoC(hour+1)= 38.86, Battery Power = 0.58
Battery is charging
Hour 9: BSoC(hour+1)= 40.78, Battery Power = 0.58
Battery is charging
Hour 10: BSoC(hour+1)= 42.71, Battery Power = 0.58
Battery is charging
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.
Hour 11: BSoC(hour+1)= 44.65, Battery Power = 0.00
Battery is neither charging nor discharging
Hour 12: BSoC(hour+1)= 44.65, Battery Power = 0.58
Battery is charging
Hour 13: BSoC(hour+1)= 46.57, Battery Power = 0.58
Battery is charging
Warning: No rules fired for Output 1. Defuzzified output value set to its mean range value 0.
Hour 14: BSoC(hour+1)= 48.49, Battery Power = 0.00
Battery is neither charging nor discharging
Hour 15: BSoC(hour+1)= 48.49, Battery Power = 0.58
Battery is charging
Hour 16: BSoC(hour+1)= 50.44, Battery Power = 0.58
Battery is charging
Hour 17: BSoC(hour+1)= 52.37, Battery Power = 0.58
Battery is charging
Hour 18: BSoC(hour+1)= 54.30, Battery Power = 0.58
Battery is charging
Hour 19: BSoC(hour+1)= 56.22, Battery Power = 0.58
Battery is charging
Hour 20: BSoC(hour+1)= 58.15, Battery Power = 0.58
Battery is charging
Hour 21: BSoC(hour+1)= 60.07, Battery Power = 0.58
Battery is charging
Hour 22: BSoC(hour+1)= 61.99, Battery Power = 0.58
Battery is charging
Hour 23: BSoC(hour+1)= 63.91, Battery Power = -0.24
Battery is discharging
Hour 24: BSoC(hour+1)= 63.13, Battery Power = -0.24
Battery is discharging
Sam Chak
Sam Chak il 25 Set 2023
Advice: When adding code to your comment, please click the Code icon and insert the MATLAB code in the grey field. Afterward, hit the Run icon . Also, display the plots (using plotmf() and ruleview()) as I did in the corrected code. This will enable me to easily track the changes you have made.
Request: As a battery researcher, I assume you have a good understanding of Battery State of Charge, Power Demand, Power Generation, and Battery Action. However, I would appreciate it if you could explain what they are and how the three inputs allow you to manually determine the Battery Action (output). At this point, I'm not concerned with Fuzzy Logic but rather seeking to understand how the system should work from a Battery Expert's perspective, not a Fuzzy Expert's.

Accedi per commentare.

Categorie

Scopri di più su Fuzzy Inference System Modeling in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by