Advertisement
print623

Task1: Matlab code for Sizing of PV and BESS

Jun 7th, 2024 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.97 KB | None | 0 0
  1. % Clear the workspace to avoid conflicts
  2. clear;
  3. clear hours;
  4.  
  5. % Load data
  6. load('data.mat');
  7.  
  8. % Extract data from the tables
  9. pv_yield = table_PV(:); % PV power for a 1kWp panel in watts (W)
  10. grainmill_demand = table_grainmill(:); % (W)
  11. household_demand = table_household(:); % (W)
  12. pump_demand = table_pump(:); % (W)
  13.  
  14. % Convert watts to kilowatts (kW)
  15. pv_yield = pv_yield / 1000; % Convert W to kW for hourly period
  16. grainmill_demand = grainmill_demand / 1000; % Convert W to kW
  17. household_demand = household_demand / 1000; % Convert W to kW
  18. pump_demand = pump_demand / 1000; % Convert W to kW
  19.  
  20. % Total demand is the sum of all individual demands
  21. total_demand = grainmill_demand + household_demand + pump_demand;
  22.  
  23. % Create a datetime array for the time variable
  24. start_time = datetime(2017,1,1,0,0,0);
  25. num_hours = length(total_demand);
  26. time = start_time + hours(0:num_hours-1);
  27.  
  28. % Create a timetable for easier handling
  29. data = timetable(time', total_demand, pv_yield);
  30.  
  31. % Plot the overall data for a quick overview
  32. figure;
  33. subplot(2, 1, 1);
  34. plot(data.Time, data.total_demand);
  35. title('Electricity Demand');
  36. xlabel('Time');
  37. ylabel('Demand (kW)');
  38.  
  39. subplot(2, 1, 2);
  40. plot(data.Time, data.pv_yield);
  41. title('PV Power Yield');
  42. xlabel('Time');
  43. ylabel('PV Yield (kW)');
  44.  
  45. % Normalize the data for percentage plotting and finding the critical period
  46. total_demand_percentage = (data.total_demand / max(data.total_demand)) * 100; % Percentage of maximum demand
  47. pv_yield_percentage = (data.pv_yield / max(data.pv_yield)) * 100; % Percentage of maximum PV yield
  48.  
  49. % Define the period length (in days) for analysis
  50. period_days = 10;
  51. period_hours = period_days * 24;
  52.  
  53. % Calculate the difference between demand and PV generation in percentage
  54. net_demand_percentage = total_demand_percentage - pv_yield_percentage;
  55.  
  56. % Initialize variables to store the critical period
  57. max_net_demand_percentage = -inf;
  58. critical_start_index = 1;
  59.  
  60. % Loop through the data to find the period with the highest net demand in percentage
  61. for i = 1:(height(data) - period_hours + 1)
  62.     current_net_demand_percentage = sum(net_demand_percentage(i:i + period_hours - 1));
  63.     if current_net_demand_percentage > max_net_demand_percentage
  64.         max_net_demand_percentage = current_net_demand_percentage;
  65.         critical_start_index = i;
  66.     end
  67. end
  68.  
  69. % Extract the critical period data
  70. critical_period_data = data(critical_start_index:(critical_start_index + period_hours - 1), :);
  71. critical_period_demand_percentage = total_demand_percentage(critical_start_index:(critical_start_index + period_hours - 1));
  72. critical_period_pv_yield_percentage = pv_yield_percentage(critical_start_index:(critical_start_index + period_hours - 1));
  73.  
  74.  
  75.  
  76. % Plot the critical period for visualization in percentage
  77. figure;
  78. plot(critical_period_data.Time, critical_period_demand_percentage, 'b', 'DisplayName', 'Demand (percentage)');
  79. hold on;
  80. plot(critical_period_data.Time, critical_period_pv_yield_percentage, 'r', 'DisplayName', 'PV Yield (percentage)');
  81. title(['Electricity Demand and PV Power Yield (percentage) during the Critical Period (', num2str(period_days), ' days)']);
  82. xlabel('Time');
  83. ylabel('Percentage Value');
  84. legend;
  85. hold off;
  86.  
  87. % Use the start and end times from critical_period_data for sizing the PV and BESS
  88. critical_start_time = critical_period_data.Time(1);
  89. critical_end_time = critical_period_data.Time(end);
  90.  
  91. % Extract the data for the critical period
  92. critical_period_data = data(data.Time >= critical_start_time & data.Time <= critical_end_time, :);
  93.  
  94. % Calculate system size for the critical period
  95. pv_size_kWp = calculate_pv_size(critical_period_data);
  96.  
  97. % Calculate daily energy balance using scaled PV yield
  98. daily_data = retime(critical_period_data, 'daily', 'sum');
  99. daily_net_energy = daily_data.pv_yield * pv_size_kWp - daily_data.total_demand;
  100.  
  101. % Determine the maximum daily energy deficit
  102. max_daily_deficit = -min(daily_net_energy); % Negative sign to get deficit
  103.  
  104. % Calculate BESS size based on max_daily_deficit
  105. [bess_size, cost_pv, cost_bess] = calculate_bess_size(pv_size_kWp, max_daily_deficit);
  106. fprintf('PV System Size: %.2f kWp\n', pv_size_kWp);
  107. fprintf('BESS Size: %.2f kWh\n', bess_size);
  108. fprintf('Cost of PV System: £%.2f\n', cost_pv);
  109. fprintf('Cost of BESS: £%.2f\n', cost_bess);
  110. total = cost_pv + cost_bess;
  111. fprintf('Total cost: £%.2f\n', total);
  112. % Function to calculate the PV size
  113.  
  114. function pv_size_kWp = calculate_pv_size(selected_data)
  115.     % Calculate total demand and PV yield over the selected period
  116.     total_demand = sum(selected_data.total_demand);
  117.     total_pv_yield = sum(selected_data.pv_yield);
  118.    
  119.     % PV system size (kWp)
  120.     pv_size_kWp = total_demand / total_pv_yield;
  121. end
  122.  
  123. % Function to calculate the BESS size
  124. function [bess_size_kWh, cost_pv, cost_bess] = calculate_bess_size(pv_size_kWp, max_daily_deficit)
  125.     % BESS size (kWh)
  126.     bess_size_kWh = max_daily_deficit;
  127.    
  128.     % Costs
  129.     cost_pv = pv_size_kWp * 1000 * 2.73;
  130.     cost_bess = bess_size_kWh * 86;
  131. end
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement