Advertisement
print623

Matlab code for Sustainable Electrical Systems Coursework 2

Mar 9th, 2024 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.40 KB | Science | 0 0
  1.                                                                                                                                                                                                                                                                                                                                                                                                  Sustainable Electrical Systems Coursework 2
  2. Authors: Thiraphat Likitkumjorn
  3. Date: 18 March 2024
  4. Task 1
  5. Let's now acquire the wind speed data first.
  6. % Acquire the path of the current folder
  7. currentFolder = pwd;
  8. csvFileName = 'E4.50 Coursework_No2_2024_Data_F 2.xls';
  9. csvFilePath = fullfile(currentFolder, csvFileName);
  10.  
  11. % read the csv file and acquire wind speed data
  12. data = readtable(csvFilePath);
  13. % Convert date to datetime
  14. data.date = datetime(data.date, 'InputFormat', 'dd-MMM-yy');
  15.  
  16. % Plot Wind Output Profile
  17. figure;
  18. plot(data.date + hours(data.period/2), data.('RealWindOutput'), 'LineWidth', 1.5);
  19. title('Wind Output Profile');
  20. xlabel('Half hourly period');
  21. ylabel('Real Wind Output (kWh)');
  22. grid on;
  23. % Update y-axis tick labels to show real values
  24. ax = gca;
  25. ax.YAxis.Exponent = 0;
  26.  
  27. % Plot SBP and SSP Profiles
  28. figure;
  29. plot(data.date + hours(data.period/2), data.SBP, 'LineWidth', 1.5, 'DisplayName', 'SBP (£/MWh)', 'Color', 'green');
  30. hold on;
  31. plot(data.date + hours(data.period/2), data.SSP, 'LineWidth', 1.5, 'DisplayName', 'SSP (£/MWh)', 'Color', 'red');
  32. title('SBP and SSP Profiles');
  33. xlabel('Half hourly period');
  34. ylabel('Price (£/MWh)');
  35. legend('Location', 'Best');
  36. grid on;
  37.  
  38. hold off;
  39. Task 2
  40. % 'Real Wind Output' is the column representing the actual wind output
  41. actual_wind_output = data.('RealWindOutput');
  42.  
  43. % Persistence forecast for 3.5 hours
  44. forecast_horizon = 3.5 * 2; % 2 data points per hour
  45. predicted_wind_output = [actual_wind_output(1:forecast_horizon); actual_wind_output(1:end-forecast_horizon)];
  46.  
  47. % Calculate forecast error
  48. forecast_error = actual_wind_output - predicted_wind_output;
  49. % Calculate Mean Absolute Error (MAE)
  50. mae = mean(abs(forecast_error(9:721, 1)));
  51.  
  52. % Display MAE
  53. disp(['Mean Absolute Error (MAE) for a 3.5-hour persistence forecast: ', num2str(mae)]);
  54. % Persistence forecast for 1 hour
  55. forecast_horizon2 = 1 * 2; % 2 data points per hour
  56. predicted_wind_output2 = [actual_wind_output(1:forecast_horizon2); actual_wind_output(1:end-forecast_horizon2)];
  57.  
  58. % Calculate forecast error
  59. forecast_error2 = actual_wind_output - predicted_wind_output2;
  60. % Calculate Mean Absolute Error (MAE)
  61. mae2 = mean(abs(forecast_error2(4:721, 1)));
  62.  
  63. % Display MAE
  64. disp(['Mean Absolute Error (MAE) for a 1-hour persistence forecast: ', num2str(mae2)]);
  65. % Plot the forecast error for 3.5 hours gate closure
  66. figure;
  67. plot(data.date + hours(data.period/2), forecast_error, 'LineWidth', 1.5, 'DisplayName', '3.5 Hours Gate Closure');
  68. hold on;
  69.  
  70. % Plot the forecast error for 1 hour gate closure
  71. plot(data.date + hours(data.period/2), forecast_error2, 'LineWidth', 1.5, 'DisplayName', '1 Hour Gate Closure');
  72.  
  73. title('Forecast Error using Persistence Forecast Technique');
  74. xlabel('Date');
  75. ylabel('Forecast Error (kWh)');
  76. legend('Location', 'Best');
  77. % Update y-axis tick labels to show real values
  78. ax = gca;
  79. ax.YAxis.Exponent = 0;
  80. ytickValues = -14000:2000:14000;  % Adjust range as needed
  81. yticks(ytickValues);
  82. % Set the y-axis limits
  83. ylim([-13000, 13000]);
  84. grid on;
  85. hold off;
  86. Task 3 and 4
  87. ssp = data.SSP;
  88. sbp = data.SBP;
  89.  
  90. % Assume that the start calculation is at the 8th period for fair comparison between
  91. % both gate closure
  92. % Calculate Imbalance Income/Charges for a 3.5-hour persistence forecast
  93.  
  94. imbalance_income_charges = zeros(size(forecast_error) - 8); % Adjust size for starting from 9th period
  95.  
  96. reward = 0;
  97. penalty = 0;
  98.  
  99. % Loop through forecast_error starting from 9th period
  100. for i = 9:length(forecast_error)
  101.     % Check for NaN in forecast_error
  102.     if ~isnan(forecast_error(i))
  103.         if forecast_error(i) >= 0
  104.             imbalance_income_charges(i-8) = forecast_error(i) * ssp(i) / 1000;
  105.             % Calculate Energy Sales Income
  106.             reward = reward + imbalance_income_charges(i-8);
  107.         else
  108.             imbalance_income_charges(i-8) = forecast_error(i) * sbp(i) / 1000;
  109.             % Calculate Total Penalties
  110.             penalty = penalty - imbalance_income_charges(i-8);
  111.         end
  112.     end
  113. end
  114.  
  115. % Calculate Imbalance Income/Charges for a 1-hour persistence forecast
  116.  
  117. imbalance_income_charges2 = zeros(size(forecast_error2) - 8); % Adjust size for starting from 9th period
  118.  
  119. reward2 = 0;
  120. penalty2 = 0;
  121.  
  122. % Loop through forecast_error2 starting from 9th period
  123. for i = 9:length(forecast_error2)
  124.     % Check for NaN in forecast_error2
  125.     if ~isnan(forecast_error2(i))
  126.         if forecast_error2(i) >= 0
  127.             imbalance_income_charges2(i-8) = forecast_error2(i) * ssp(i) / 1000;
  128.             % Calculate Energy Sales Income
  129.             reward2 = reward2 + imbalance_income_charges2(i-8);
  130.         else
  131.             imbalance_income_charges2(i-8) = forecast_error2(i) * sbp(i) / 1000;
  132.             % Calculate Total Penalties
  133.             penalty2 = penalty2 - imbalance_income_charges2(i-8);
  134.         end
  135.     end
  136. end
  137.  
  138. % Plot the results
  139. figure;
  140. plot(data.date(9:end) + hours(data.period(9:end)/2), imbalance_income_charges, 'LineWidth', 1.5);
  141. hold on;
  142. plot(data.date(9:end) + hours(data.period(9:end)/2), imbalance_income_charges2, 'LineWidth', 1.5);
  143. title('Half-Hourly Imbalance Income/Charges');
  144. xlabel('Date');
  145. ylabel('Imbalance Income/Charges (£)')
  146. legend('3.5-hours Gate Closure', '1-hour Gate Closure');
  147. ytickValues = -800:100:400;  % Adjust range as needed
  148. yticks(ytickValues);
  149. ylim([-800, 250]);
  150. grid on;
  151. hold off;
  152.  
  153. % Calculate total penalties for a 3.5-hour persistence forecast
  154. total_penalties = penaltie;
  155. % Calculate Energy Sales Income
  156. % Assume that the market price is the average of SBP and SSP
  157. market_price= (ssp+sbp)./2;
  158. energy_sales_income = sum(predicted_wind_output(9:721, 1) .* market_price(9:721, 1)./1000)+reward;
  159. % Calculate Overall Income for a 3.5-hour persistence forecast
  160. overall_income = energy_sales_income - total_penalties;
  161. fprintf('Energy Sales Income (3.5-hours persistence forecast): £%.2f\n', energy_sales_income);
  162. fprintf('Total Penalties (3.5-hours persistence forecast): £%.2f\n', total_penalties);
  163. fprintf('Overall Income (3.5-hours persistence forecast): £%.2f\n', overall_income);
  164. % Calculate total penalties for a 1-hour persistence forecast
  165. total_penalties2 = penaltie2;
  166. % Calculate Energy Sales Income for a 1-hour persistence forecast
  167. energy_sales_income2 = sum(predicted_wind_output2(9:721, 1) .* market_price(9:721, 1)./1000)+reward2;
  168. % Calculate Overall Income for a 1-hour persistence forecast
  169. overall_income2 = energy_sales_income2 - total_penalties2;
  170. fprintf('Energy Sales Income (1-hour persistence forecast): £%.2f\n', energy_sales_income2);
  171. fprintf('Total Penalties (1-hour persistence forecast): £%.2f\n', total_penalties2);
  172. fprintf('Overall Income (1-hour persistence forecast): £%.2f\n', overall_income2);
  173.  
  174. %Output for task4:
  175. %Energy Sales Income (3.5-hours persistence forecast): £199661.28
  176. %Total Penalties (3.5-hours persistence forecast): £23408.66
  177. %Overall Income (3.5-hours persistence forecast): £176252.62
  178. %Energy Sales Income (1-hour persistence forecast): £191066.94
  179. %Total Penalties (1-hour persistence forecast): £9222.81
  180. %Overall Income (1-hour persistence forecast): £181844.13
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement