├── 2016-05-28_2016-05-29_residentialHouseLoad.csv ├── Example31_final.m ├── Example32.m ├── Example35_easy.m ├── Example3_6.m ├── Example3_7_baseloadexample.m ├── Example3_8_baseloadexample.m ├── Figure_slowcooker.m ├── PDFs.xlsx ├── README.md ├── Troom_record1.mat ├── Troom_record2.mat ├── Troom_record3.mat ├── Troom_record4.mat ├── Troom_record5.mat ├── cal_RCpara.m ├── example32_cal_TonToff.m ├── example33_Errorcorrection_figure1.m ├── example33_Errorcorrection_part1.m ├── example33_Errorcorrection_part1_G.m ├── example33_Errorcorrection_part2.m ├── example33_Errorcorrection_part3.m ├── example33_Errorcorrection_part4.m ├── example34_randomization.m ├── example35_randomizationMethod2.m ├── figureexample32_final_left.fig ├── figureexample32_final_right.fig ├── figureexample33_FigureC.fig ├── moduel_spaceheater.m └── slowcooker.m /2016-05-28_2016-05-29_residentialHouseLoad.csv: -------------------------------------------------------------------------------- 1 | consumptionPower,consumptionEnergy 2 | 164,384738803 3 | 163,384787834 4 | 161,384836366 5 | 156,384883831 6 | 40,384897375 7 | 40,384909363 8 | 40,384921323 9 | 174,384939068 10 | 162,384988086 11 | 162,385036819 12 | 158,385084844 13 | 40,385114054 14 | 40,385126030 15 | 40,385137910 16 | 40,385149843 17 | 163,385186718 18 | 162,385235518 19 | 159,385283646 20 | 40,385328768 21 | 40,385340711 22 | 40,385352679 23 | 40,385364629 24 | 171,385383930 25 | 162,385432814 26 | 161,385481411 27 | 158,385529331 28 | 40,385556171 29 | 40,385568155 30 | 40,385580113 31 | 40,385592118 32 | 162,385629825 33 | 162,385678574 34 | 160,385726805 35 | 40,385771183 36 | 40,385783157 37 | 40,385795140 38 | 40,385807120 39 | 171,385825991 40 | 162,385874817 41 | 160,385923295 42 | 158,385971218 43 | 40,386004110 44 | 40,386016086 45 | 40,386028067 46 | 40,386040030 47 | 162,386070294 48 | 162,386119031 49 | 159,386167224 50 | 40,386209601 51 | 40,386221544 52 | 40,386233530 53 | 40,386245504 54 | 166,386267400 55 | 162,386315967 56 | 160,386364257 57 | 157,386411873 58 | 40,386433830 59 | 40,386445804 60 | 40,386457790 61 | 40,386469796 62 | 163,386509345 63 | 161,386557837 64 | 158,386605781 65 | 40,386638815 66 | 40,386650776 67 | 40,386662757 68 | 40,386674759 69 | 161,386703720 70 | 161,386752298 71 | 159,386800332 72 | 40,386844205 73 | 40,386856181 74 | 40,386868149 75 | 40,386880113 76 | 172,386897589 77 | 161,386946092 78 | 159,386993966 79 | 153,387040929 80 | 40,387056635 81 | 40,387068626 82 | 40,387080605 83 | 40,387092569 84 | 167,387137935 85 | 256,387189439 86 | 254,387266674 87 | 163,387323956 88 | 50,387357570 89 | 43,387372324 90 | 160,387412308 91 | 156,387459768 92 | 43,387493575 93 | 43,387506486 94 | 160,387535472 95 | 415,387620900 96 | 297,387741943 97 | 296,387798671 98 | 40,387841723 99 | 40,387853662 100 | 40,387866848 101 | 161,387915749 102 | 160,387964891 103 | 156,388016372 104 | 46,388040354 105 | 40,388065507 106 | 40,388077512 107 | 40,388093070 108 | 162,388134163 109 | 160,388182403 110 | 157,388230020 111 | 40,388257528 112 | 40,388269493 113 | 40,388281468 114 | 40,388293426 115 | 162,388328443 116 | 471,388460239 117 | 159,388525079 118 | 40,388558659 119 | 197,388596331 120 | 207,388656871 121 | 216,388719955 122 | 334,388802097 123 | 309,388900023 124 | 314,388992974 125 | 191,389080198 126 | 192,389138869 127 | 184,389195079 128 | 252,389266411 129 | 382,389348549 130 | 336,389454235 131 | 288,389545013 132 | 275,389630972 133 | 156,389688673 134 | 160,389736470 135 | 158,389785083 136 | 168,389832705 137 | 976,389945270 138 | 1012,390107706 139 | 1119,390394183 140 | 1093,390613036 141 | 189,390777175 142 | 348,390842725 143 | 306,390910542 144 | 293,390995667 145 | 287,391078114 146 | 143,391134457 147 | 141,391177820 148 | 257,391245322 149 | 313,391332899 150 | 195,391418974 151 | 193,391476915 152 | 192,391534986 153 | 193,391592746 154 | 327,391658344 155 | 311,391753694 156 | 311,391847147 157 | 309,391940506 158 | 197,392013277 159 | 40,392058915 160 | 45,392073249 161 | 56,392087545 162 | 181,392130135 163 | 174,392182743 164 | 177,392234819 165 | 62,392281623 166 | 62,392300193 167 | 40,392318700 168 | 40,392330708 169 | 170,392349929 170 | 163,392398746 171 | 160,392447071 172 | 157,392494863 173 | 40,392522472 174 | 40,392534461 175 | 40,392546421 176 | 40,392558358 177 | 162,392593942 178 | 161,392642534 179 | 160,392690609 180 | 156,392738077 181 | 43,392753613 182 | 43,392766460 183 | 43,392779331 184 | 43,392792224 185 | 166,392841422 186 | 185,392892901 187 | 169,392945601 188 | 52,392990559 189 | 49,393005881 190 | 52,393021267 191 | 51,393036484 192 | 179,393062030 193 | 174,393114194 194 | 170,393166225 195 | 177,393219526 196 | 165,393270483 197 | 71,393301064 198 | 53,393321030 199 | 176,393348693 200 | 193,393402334 201 | 168,393456134 202 | 56,393504101 203 | 60,393521398 204 | 179,393543678 205 | 163,393593344 206 | 346,393661347 207 | 207,393743420 208 | 210,393809117 209 | 224,393876791 210 | 219,393941869 211 | 362,394035701 212 | 344,394141646 213 | 348,394246604 214 | 355,394352900 215 | 236,394428536 216 | 226,394498378 217 | 222,394565126 218 | 331,394634160 219 | 348,394737483 220 | 333,394840217 221 | 346,394942405 222 | 223,395037365 223 | 219,395103741 224 | 210,395167573 225 | 203,395228377 226 | 322,395304545 227 | 324,395403457 228 | 319,395500720 229 | 320,395596765 230 | 208,395671761 231 | 197,395732211 232 | 204,395792090 233 | 200,395851747 234 | 330,395941815 235 | 321,396039148 236 | 327,396136638 237 | 316,396232397 238 | 207,396295581 239 | 169,396327365 240 | 162,396376553 241 | 158,396424563 242 | 42,396467176 243 | 42,396479837 244 | 164,396504258 245 | 159,396552647 246 | 156,396600065 247 | 39,396629110 248 | 38,396640706 249 | 38,396652220 250 | 38,396663757 251 | 162,396700046 252 | 470,396752147 253 | 302,396870831 254 | 286,396959884 255 | 161,397013758 256 | 161,397063801 257 | 652,397247604 258 | 157,397435398 259 | 156,397482495 260 | 308,397565784 261 | 192,397655369 262 | 321,397739476 263 | 314,397834560 264 | 459,397949647 265 | 463,398087365 266 | 422,398224167 267 | 454,398359289 268 | 453,398494225 269 | 338,398606713 270 | 337,398707133 271 | 338,398807448 272 | 475,398912833 273 | 462,399053039 274 | 459,399191707 275 | 470,399331489 276 | 335,399467211 277 | 218,399533980 278 | 212,399599329 279 | 217,399664778 280 | 346,399740722 281 | 335,399844192 282 | 342,399947578 283 | 344,400049946 284 | 220,400140975 285 | 73,400195224 286 | 303,400281891 287 | 302,400373478 288 | 170,400445715 289 | 170,400496957 290 | 168,400547787 291 | -------------------------------------------------------------------------------- /Example31_final.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Code written 9/21/2017 3 | % Copyright North Carolina State University 4 | 5 | clear all 6 | close all 7 | for ii = 1:3 8 | % Simulation setup 9 | Tout = [0 5 10]; 10 | color = ['rkb']; 11 | Time = 60 % in minutes 12 | Timestep = 1 % second 13 | N_timestep = Time*60/Timestep; 14 | Tset = 20 % degree C 15 | TDB = 1 %degree C 16 | Prated = 5020 %W 17 | Area = 228 % in m^2 18 | Vair = 228*5 % m^3 19 | DensityAir = 1.225 % kg/m^3 20 | Cp_air = 1005 % J/kg.k 21 | Cv_air = 780 % J/kg.k 22 | C_funiture = 1000 % J/kg.k 23 | V_furniture = 1000 %kg 24 | UA = 111 %W/degree C 25 | UAmass = 3924 %W/degree C 26 | 27 | R1 = 1/UA 28 | R2 = 1/UAmass 29 | Ca = Vair*DensityAir*Cv_air 30 | Cm = V_furniture*C_funiture 31 | 32 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 33 | B = [1/(R1*Ca) 1/Ca ;0 0] 34 | C = [1 0; 0 1] 35 | D = [0; 0] 36 | 37 | status = zeros(1, Time); 38 | Q = zeros(1, Time); 39 | 40 | x0 = [20; 20] 41 | if x0(1) > (Tset + TDB/2) 42 | status(1) = 0; 43 | elseif x0(1)<(Tset - TDB/2) 44 | status(1) = 1; 45 | end 46 | if status(1) == 1 47 | Q0(1) = Prated*Timestep; 48 | else 49 | Q0(1) = 0; 50 | end 51 | u0 = [Tout(ii); Q0] 52 | 53 | for i = 2:1:N_timestep 54 | DT = A*x0 + B*u0; 55 | x1 = x0+DT; 56 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 57 | status(i) = 0; 58 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 59 | status(i) = 1; 60 | else 61 | status(i) = status(i-1); 62 | end 63 | if status(i) == 1 64 | Q(i) = Prated*Timestep; 65 | else 66 | Q(i) = 0; 67 | end 68 | x0 = x1; 69 | u0 = [Tout(ii); Q(i)]; 70 | Troom_record(i-1) = x1(1); 71 | Tmass_record(i-1) = x1(2); 72 | end 73 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 74 | figure(1) 75 | set(gcf,'DefaultAxesFontSize',10) %<--------set character size 76 | set(gcf,'DefaultTextFontSize',10) 77 | plot(Time,Troom_record,color(ii)) 78 | % plot(Tmass_record,'b') 79 | xlabel('Time (minute)') 80 | ylabel('Temperature (^oC)') 81 | hold on 82 | end 83 | legend('T=0^oC','T=5^oC','T=10^oC') -------------------------------------------------------------------------------- /Example32.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Code written 9/21/2017 3 | % Copyright North Carolina State University 4 | 5 | clear all 6 | close all 7 | for ii = 1:3 8 | % Simulation setup 9 | Tout = [0 5 10]; 10 | color = ['rkb']; 11 | Time = 60 % in minutes 12 | Timestep = 1 % second 13 | N_timestep = Time*60/Timestep; 14 | Tset = 20 % degree C 15 | TDB = 1 %degree C 16 | Prated = 5020 %W 17 | Area = 228 % in m^2 18 | Vair = 228*5 % m^3 19 | DensityAir = 1.225 % kg/m^3 20 | Cp_air = 1005 % J/kg.k 21 | Cv_air = 780 % J/kg.k 22 | C_funiture = 1000 % J/kg.k 23 | V_furniture = 1000 %kg 24 | UA = 333 %W/degree C <-------------------- 25 | UAmass = 3924 %W/degree C 26 | 27 | R1 = 1/UA 28 | R2 = 1/UAmass 29 | Ca = Vair*DensityAir*Cv_air 30 | Cm = V_furniture*C_funiture 31 | 32 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 33 | B = [1/(R1*Ca) 1/Ca ;0 0] 34 | C = [1 0; 0 1] 35 | D = [0; 0] 36 | 37 | status = zeros(1, Time); 38 | Q = zeros(1, Time); 39 | 40 | x0 = [20; 20] 41 | if x0(1) > (Tset + TDB/2) 42 | status(1) = 0; 43 | elseif x0(1)<(Tset - TDB/2) 44 | status(1) = 1; 45 | end 46 | if status(1) == 1 47 | Q0(1) = Prated*Timestep; 48 | else 49 | Q0(1) = 0; 50 | end 51 | u0 = [Tout(ii); Q0] 52 | 53 | for i = 2:1:N_timestep 54 | DT = A*x0 + B*u0; 55 | x1 = x0+DT; 56 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 57 | status(i) = 0; 58 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 59 | status(i) = 1; 60 | else 61 | status(i) = status(i-1); 62 | end 63 | if status(i) == 1 64 | Q(i) = Prated*Timestep; 65 | else 66 | Q(i) = 0; 67 | end 68 | x0 = x1; 69 | u0 = [Tout(ii); Q(i)]; 70 | Troom_record(i-1) = x1(1); 71 | Tmass_record(i-1) = x1(2); 72 | end 73 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 74 | figure(1) 75 | set(gcf,'DefaultAxesFontSize',10) %<--------set character size 76 | set(gcf,'DefaultTextFontSize',10) 77 | plot(Time,Troom_record,color(ii)) 78 | % plot(Tmass_record,'b') 79 | xlabel('Time (minute)') 80 | ylabel('Temperature (^oC)') 81 | hold on 82 | end 83 | legend('T=0^oC','T=5^oC','T=10^oC') -------------------------------------------------------------------------------- /Example35_easy.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Code written 9/21/2017 3 | % Copyright North Carolina State University 4 | 5 | clear all 6 | close all 7 | 8 | Pwasherrated = 0.4; 9 | Pdryerrated = 6; 10 | 11 | %probability density function 12 | Prob_dayofweek = [0.1 0.1 0.1 0.05 0.05 0.3 0.3 ]; 13 | Prob_timeoftheday_wkday = [0 0 0 0 0 0 0 0.01 0.01 0.1 0.1 0.06 0.06 0.06 0.06 0.06 0.06 0 0.06 0.1 0.1 0.1 0.06 0]; 14 | Prob_timeoftheday_wkend = [0 0 0 0 0 0 0 0.1 0.1 0.1 0.1 0.07 0.05 0.04 0.03 0.02 0.03 0.04 0.06 0.15 0.08 0.02 0.01 0]; 15 | Prob_loading =[0.1 0.2 0.35 0.3 0.04 0.01]; 16 | Prob_dryonafterwashingdone = [0.5 0.25 0.15 0.07 0.02 0.01]; 17 | 18 | 19 | Washeronduration = [30:1:60]; 20 | dryerontime = [45:1:75]; 21 | Tnextloading = [-5:1:5]; 22 | dryonafterwashingdone = [5 10 15 20 25 30]; 23 | 24 | for i = 1:1:length(Prob_dayofweek) 25 | PCF_dayofweek(i) = sum(Prob_dayofweek(1:i)); 26 | end 27 | 28 | for i = 1:1:length(Prob_timeoftheday_wkend) 29 | PCF_timeoftheday_wkend(i) = sum(Prob_timeoftheday_wkend(1:i)); 30 | end 31 | 32 | for i = 1:1:length(Prob_timeoftheday_wkday) 33 | PCF_timeoftheday_wkday(i) = sum(Prob_timeoftheday_wkday(1:i)); 34 | end 35 | 36 | for i = 1:1:length(Prob_loading) 37 | PCF_loading(i) = sum(Prob_loading(1:i)); 38 | end 39 | 40 | for i = 1:1:length(Prob_dryonafterwashingdone) 41 | PCF_dryonafterwashingdone(i) = sum(Prob_dryonafterwashingdone(1:i)); 42 | end 43 | 44 | P_washer = zeros(1000,24*60*7); 45 | P_dryer = zeros(1000,24*60*7); 46 | 47 | for i = 1:1000 48 | All_rand = rand(10,1); 49 | dayofweek = min(find(PCF_dayofweek>=All_rand(1))); 50 | if dayofweek <=5 51 | timeofdayvalues = find(PCF_timeoftheday_wkday>=All_rand(2)); 52 | for j =1:1: length(timeofdayvalues) 53 | if Prob_timeoftheday_wkday(timeofdayvalues(j)) ~=0 54 | timeofday = timeofdayvalues(j); 55 | break 56 | end 57 | end 58 | else 59 | timeofdayvalues = find(PCF_timeoftheday_wkend>=All_rand(2)); 60 | for j =1:1: length(timeofdayvalues) 61 | if Prob_timeoftheday_wkend(timeofdayvalues(j)) ~=0 62 | timeofday = timeofdayvalues(j); 63 | break 64 | end 65 | end 66 | end 67 | minuteofhour = ceil(All_rand(3)*60); 68 | T_washeron(i) = (dayofweek-1)*24*50 + timeofday*60 + minuteofhour; 69 | T_washeroff(i) = T_washeron(i) + floor(All_rand(4)*30) + 30; 70 | P_washer(i,T_washeron(i):T_washeroff(i)) = Pwasherrated; 71 | 72 | T_dryeron_value = dryonafterwashingdone(min(find(PCF_dryonafterwashingdone>=All_rand(5)))); 73 | 74 | T_dryeron(i) = T_washeroff(i) + T_dryeron_value - floor(All_rand(6)*5); 75 | T_dryeroff(i) = T_dryeron(i) + floor(All_rand(7)*30) + 45; 76 | P_dryer(i,T_dryeron(i):T_dryeroff(i)) = Pdryerrated; 77 | % figure(1) 78 | % plot([1:1:24*60*7]/60,P_washer(i,:)) 79 | % hold on 80 | % plot([1:1:24*60*7]/60,P_dryer(i,:)) 81 | % xlim([1 24*7]) 82 | 83 | end 84 | 85 | Totalwasher = sum(P_washer); 86 | Totaldryer = sum(P_dryer); 87 | figure(2) 88 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 89 | set(gcf,'DefaultTextFontSize',16) 90 | plot([1:1:24*60*7]/60,Totalwasher,'b') 91 | hold on 92 | plot([1:1:24*60*7]/60,Totaldryer,'r') 93 | xlim([1 24*7]) 94 | xlabel('Time (minute)') 95 | ylabel('Power (kW)') 96 | legend('washer','dryer') 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /Example3_6.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Code written 9/21/2017 3 | % Copyright North Carolina State University 4 | 5 | clear all 6 | close all 7 | 8 | % The power ratings of the washer and dryer 9 | 10 | P_washer = zeros(1000,24*60*8); 11 | P_dryer = zeros(1000,24*60*8); 12 | 13 | %probability density functions 14 | Prob_dayofweek = [0.1 0.1 0.1 0.05 0.05 0.3 0.3 ]; 15 | Prob_timeoftheday_wkday = [0 0 0 0 0 0 0 0.01 0.01 0.1 0.1 0.06 0.06 0.06 0.06 0.06 0.06 0 0.06 0.1 0.1 0.1 0.06 0]; 16 | Prob_timeoftheday_wkend = [0 0 0 0 0 0 0 0.1 0.1 0.1 0.1 0.07 0.05 0.04 0.03 0.02 0.03 0.04 0.06 0.15 0.08 0.02 0.01 0]; 17 | Prob_loading =[0.1 0.2 0.35 0.3 0.04 0.01]; 18 | Prob_dryonafterwashingdone = [0.5 0.25 0.15 0.07 0.02 0.01]; 19 | 20 | % Constants 21 | Washeronduration = [30:1:60]; % Washer on duration is between 30-40 minutes 22 | dryerontime = [45:1:75]; % dryer on duration is between 45-75 minutes 23 | Tnextloading = [-5:1:5]; % The next loading will begin within five minutes before or after the dryer is on 24 | dryonafterwashingdone = [5 10 15 20 25 30]; % dryer will always be on after a loading is finished and its on 25 | 26 | % Calculate the cumulated probability functions 27 | for i = 1:1:length(Prob_dayofweek) 28 | PCF_dayofweek(i) = sum(Prob_dayofweek(1:i)); 29 | end 30 | for i = 1:1:length(Prob_timeoftheday_wkend) 31 | PCF_timeoftheday_wkend(i) = sum(Prob_timeoftheday_wkend(1:i)); 32 | end 33 | for i = 1:1:length(Prob_timeoftheday_wkday) 34 | PCF_timeoftheday_wkday(i) = sum(Prob_timeoftheday_wkday(1:i)); 35 | end 36 | for i = 1:1:length(Prob_loading) 37 | PCF_loading(i) = sum(Prob_loading(1:i)); 38 | end 39 | for i = 1:1:length(Prob_dryonafterwashingdone) 40 | PCF_dryonafterwashingdone(i) = sum(Prob_dryonafterwashingdone(1:i)); 41 | end 42 | 43 | %simulate 1000 washer and dryer pairs 44 | for i = 1:1000 45 | All_rand = rand(20,1); 46 | Pwasherrated = (300 + floor(200*All_rand(20)))/1000; 47 | Pdryerrated = (3000 + floor(4000*All_rand(20)))/1000; 48 | 49 | dayofweek = min(find(PCF_dayofweek>=All_rand(1))); 50 | if dayofweek <=5 51 | timeofdayvalues = find(PCF_timeoftheday_wkday>=All_rand(2)); 52 | for j =1:1: length(timeofdayvalues) 53 | if Prob_timeoftheday_wkday(timeofdayvalues(j)) ~=0 54 | timeofday = timeofdayvalues(j); 55 | break 56 | end 57 | end 58 | else 59 | timeofdayvalues = find(PCF_timeoftheday_wkend>=All_rand(2)); 60 | for j =1:1: length(timeofdayvalues) 61 | if Prob_timeoftheday_wkend(timeofdayvalues(j)) ~=0 62 | timeofday = timeofdayvalues(j); 63 | break 64 | end 65 | end 66 | end 67 | minuteofhour = ceil(All_rand(3)*60); 68 | T_washeron = (dayofweek-1)*24*60 + timeofday*60 + minuteofhour; 69 | T_washeroff = T_washeron + floor(All_rand(4)*30) + 30; 70 | P_washer(i,T_washeron:T_washeroff) = Pwasherrated; 71 | 72 | T_dryeron_value = dryonafterwashingdone(min(find(PCF_dryonafterwashingdone>=All_rand(5)))); 73 | 74 | T_dryeron = T_washeroff + T_dryeron_value - floor(All_rand(6)*5); 75 | T_dryeroff = T_dryeron + floor(All_rand(7)*30) + 45; 76 | P_dryer(i,T_dryeron:T_dryeroff) = Pdryerrated; 77 | % figure(1) 78 | % plot([1:1:24*60*7]/60,P_washer(i,:)) 79 | % hold on 80 | % plot([1:1:24*60*7]/60,P_dryer(i,:)) 81 | % xlim([1 24*7]) 82 | 83 | No_loading = min(find(Prob_loading >= All_rand(8))); 84 | 85 | for jj = 2:No_loading 86 | T_washeron = T_dryeroff - 5 + floor(10*All_rand(9)); 87 | T_washeroff = T_washeron + floor(All_rand(4)*30) + 30; 88 | P_washer(i,T_washeron:T_washeroff) = Pwasherrated; 89 | 90 | T_dryeron_value = dryonafterwashingdone(min(find(PCF_dryonafterwashingdone>=All_rand(10)))); 91 | T_dryeron = T_washeroff + T_dryeron_value - floor(All_rand(12)*5); 92 | T_dryeroff = T_dryeron + floor(All_rand(13)*30) + 45; 93 | P_dryer(i,T_dryeron:T_dryeroff) = Pdryerrated; 94 | end 95 | end 96 | 97 | Totalwasher = sum(P_washer); 98 | Totaldryer = sum(P_dryer); 99 | figure(2) 100 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 101 | set(gcf,'DefaultTextFontSize',16) 102 | subplot(1,2,1) 103 | plot([1:1:24*60*8]/60,Totalwasher,'b') 104 | hold on 105 | plot([1:1:24*60*8]/60,Totaldryer,'r') 106 | 107 | xlabel('Time (minute)') 108 | ylabel('Power (kW)') 109 | xlim([72 96]) 110 | ylim([0 50]) 111 | legend('washer','dryer') 112 | title('(a) Wednesday') 113 | 114 | subplot(1,2,2) 115 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 116 | set(gcf,'DefaultTextFontSize',16) 117 | plot([1:1:24*60*8]/60,Totalwasher,'b') 118 | hold on 119 | plot([1:1:24*60*8]/60,Totaldryer,'r') 120 | 121 | xlabel('Time (minute)') 122 | ylabel('Power (kW)') 123 | xlim([120 144]) 124 | % ylim([0 100]) 125 | legend('washer','dryer') 126 | title('(b) Saturday') 127 | 128 | figure(3) 129 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 130 | set(gcf,'DefaultTextFontSize',16) 131 | plot([1:1:24*60*8]/60,Totalwasher,'b') 132 | hold on 133 | plot([1:1:24*60*8]/60,Totaldryer,'r') 134 | xlim([0 24*8]) 135 | xlabel('Time (minute)') 136 | ylabel('Power (kW)') 137 | 138 | legend('washer','dryer') 139 | title('(c) Monday to Sunday') 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /Example3_7_baseloadexample.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Code written 9/21/2017 3 | % Copyright North Carolina State University 4 | 5 | clear all 6 | close all 7 | 8 | % The power ratings of the washer and dryer 9 | filenames = {'2016-05-26_2016-05-27_minutes.csv';'2016-05-27_2016-05-28_minutes.csv';'2016-05-28_2016-05-29_minutes.csv';'2016-05-29_2016-05-30_minutes.csv'} 10 | j_index = 1; 11 | feature = zeros(200,1); 12 | switching = zeros(1000,1); 13 | Time_onoff = zeros(1000,300); 14 | fitprevious = 0; 15 | n_day = 4; 16 | t = [0:5:5*12*24]; 17 | 18 | for ii = 1:n_day 19 | data = xlsread(filenames{ii}); 20 | power_values = data(:,1); 21 | figure(ii) 22 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 23 | set(gcf,'DefaultTextFontSize',14) 24 | plot(t/60,power_values) 25 | hold on 26 | xlabel('Time of the day') 27 | ylabel('Power (Watt)') 28 | 29 | end 30 | 31 | xlim([0 24]) 32 | 33 | clear all 34 | load house1.mat 35 | 36 | -------------------------------------------------------------------------------- /Example3_8_baseloadexample.m: -------------------------------------------------------------------------------- 1 | % Sample code written by Dr. Ning LU 2 | % Code written by Dr. Ning Lu for DR book 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | 7 | clear all 8 | close all 9 | 10 | 11 | load house1.mat 12 | [x,y] = size(House_power); 13 | start_time = 309; %this is oct 1, 2015 14 | end_time = 309+24*2*365; %this is oct 1, 2015 15 | 16 | start_time = 309; %this is oct 1, 2015 17 | end_time = 309+24*2; %this is oct 1, 2015 18 | 19 | for i = 1:7 20 | start_time = 309+24*2*(164+i); %this is oct 1, 2015 21 | end_time = 309+24*2*(165+i); %this is oct 1, 2015 22 | 23 | figure(1) 24 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 25 | set(gcf,'DefaultTextFontSize',14) 26 | plot([0:1:end_time-start_time]/2,House_power(start_time:1:end_time)) 27 | xlabel('Time of the day') 28 | ylabel('Power (kW)') 29 | hold on 30 | xlim([0 24]) 31 | end 32 | 33 | legend('M','T','W','TH','F','Sat','Sun') 34 | text(1, 3,'A week in April') 35 | -------------------------------------------------------------------------------- /Figure_slowcooker.m: -------------------------------------------------------------------------------- 1 | clear all 2 | close all 3 | 4 | 5 | simulation_time = 24*60*60; 6 | t = [1:1:simulation_time]'; 7 | 8 | P_total = zeros(simulation_time,1); 9 | P_slowcooker = zeros(simulation_time,1); 10 | 11 | t_s = 7*60*60+30*60; 12 | t_e = t_s + 10*60*60; 13 | 14 | mode =2; 15 | 16 | [P, Q] = slowcooker(t_s, t_e, mode); 17 | P_slowcooker(t_s:t_e-1)= P; 18 | 19 | figure(1) 20 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 21 | set(gcf,'DefaultTextFontSize',16) 22 | plot(t/60/60, P_slowcooker) 23 | 24 | 25 | -------------------------------------------------------------------------------- /PDFs.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/PDFs.xlsx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DemandResponseBookChapter3 2 | This Repository contains the matlab codes to generate the figures in Chapter 3 of the book "Demand Response in Smart Grids". 3 | -------------------------------------------------------------------------------- /Troom_record1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/Troom_record1.mat -------------------------------------------------------------------------------- /Troom_record2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/Troom_record2.mat -------------------------------------------------------------------------------- /Troom_record3.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/Troom_record3.mat -------------------------------------------------------------------------------- /Troom_record4.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/Troom_record4.mat -------------------------------------------------------------------------------- /Troom_record5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/Troom_record5.mat -------------------------------------------------------------------------------- /cal_RCpara.m: -------------------------------------------------------------------------------- 1 | function [R, C] = cal_RCpara(Tmax, Tmin, Tout, trise, tfall, Q, heater_mode) 2 | 3 | disp('******If calculate heater parameters, heater_mode is 1. Otherwise it is 0.') 4 | disp('******Q is positive if heating; Q is negative if cooling!!!!') 5 | % if the heater mode 6 | if heater_mode == 1 7 | RC = 1/(log((Tmax-Tout)/(Tmin-Tout))/tfall); 8 | R = (Tmax-Tout + (Tout-Tmin)*exp(-trise/RC))/(Q*(1-exp(-trise/RC))); 9 | C = RC/R; 10 | elseif heater_mode == 0 11 | RC = 1/(log((Tmin-Tout)/(Tmax-Tout))/trise); 12 | R = (Tmin-Tout + (Tout-Tmax)*exp(-tfall/RC))/(Q*(1-exp(-tfall/RC))); 13 | C = RC/R; 14 | else 15 | error('Please select 1 or 0 for heater_mode'); 16 | display('If calculate heater parameters, heater_mode is 1. Otherwise it is 0.'); 17 | end -------------------------------------------------------------------------------- /example32_cal_TonToff.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Code written 9/21/2017 3 | % Copyright North Carolina State University 4 | 5 | clear all 6 | close all 7 | 8 | for ii = 1:5 9 | % Simulation setup 10 | Tout = [-10 -5 0 5 10]; 11 | color = ['rgbcm']; 12 | Time = 60 % in minutes 13 | Timestep = 1 % second 14 | N_timestep = Time*60/Timestep; 15 | Tset = 20 % degree C 16 | TDB = 1 %degree C 17 | Prated = 5020 %W 18 | Area = 228 % in m^2 19 | Vair = 228*5 % m^3 20 | DensityAir = 1.225 % kg/m^3 21 | Cp_air = 1005 % J/kg.k 22 | Cv_air = 780 % J/kg.k 23 | C_funiture = 1000 % J/kg.k 24 | V_furniture = 1000 %kg 25 | UA = 111 %W/degree C 26 | UAmass = 3924 %W/degree C 27 | T_on=0; 28 | T_off=0; 29 | 30 | R1 = 1/UA 31 | R2 = 1/UAmass 32 | Ca = Vair*DensityAir*Cv_air 33 | Cm = V_furniture*C_funiture 34 | 35 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 36 | B = [1/(R1*Ca) 1/Ca ;0 0] 37 | C = [1 0; 0 1] 38 | D = [0; 0] 39 | 40 | status = zeros(1, Time); 41 | Q = zeros(1, Time); 42 | 43 | x0 = [19.5; 19.5] 44 | if x0(1) > (Tset + TDB/2) 45 | status(1) = 0; 46 | elseif x0(1)<(Tset - TDB/2) 47 | status(1) = 1; 48 | end 49 | if status(1) == 1 50 | Q0(1) = Prated*Timestep; 51 | else 52 | Q0(1) = 0; 53 | end 54 | u0 = [Tout(ii); Q0] 55 | 56 | for i = 2:1:N_timestep 57 | DT = A*x0 + B*u0; 58 | x1 = x0+DT; 59 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 60 | status(i) = 0; 61 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 62 | status(i) = 1; 63 | else 64 | status(i) = status(i-1); 65 | end 66 | if status(i) == 1 67 | Q(i) = Prated*Timestep; 68 | T_on(i,1) = T_on(i-1,1)+1; 69 | T_off(i,1) = 0; 70 | else 71 | Q(i) = 0; 72 | T_off(i,1) = T_off(i-1,1)+1; 73 | T_on(i,1) = 0; 74 | end 75 | x0 = x1; 76 | u0 = [Tout(ii); Q(i)]; 77 | Troom_record(i-1,1) = x1(1); 78 | Tmass_record(i-1,1) = x1(2); 79 | end 80 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 81 | 82 | figure(1) 83 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 84 | set(gcf,'DefaultTextFontSize',14) 85 | plot(Time,Troom_record,color(ii)) 86 | % plot(Tmass_record,'b') 87 | xlabel('Time (minute)') 88 | ylabel('Temperature (^oC)') 89 | hold on 90 | title('Room Temperature Profiles') 91 | 92 | 93 | Toutcal(ii) = Tout(ii); 94 | Toncal(ii) = max(T_on(1000:end)); 95 | Toffcal(ii) = max(T_off); 96 | 97 | filename = ['Troom_record' num2str(ii)]; 98 | save(filename, 'Troom_record','T_on', 'T_off','Toutcal') 99 | 100 | 101 | end 102 | 103 | figure(1) 104 | legend('T=-10^oC','T=-5^oC', 'T=0^oC','T=5^oC','T=10^oC') 105 | 106 | figure(2) 107 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 108 | set(gcf,'DefaultTextFontSize',14) 109 | plot(Toutcal, Toncal,'ro') 110 | hold on 111 | plot(Toutcal, Toffcal,'b^') 112 | title('On/Off Time versus T_{outdoor}') 113 | legend('\tau_{on}','\tau_{off}') 114 | xlabel('T_{outdoor} (^oC)') 115 | ylabel('Time (second)') 116 | 117 | p1=polyfit(Toutcal,Toncal,3); 118 | Toutfit = [-15:1:15]; 119 | Tonfit = polyval(p1,Toutfit) 120 | plot(Toutfit,Tonfit,'m') 121 | 122 | p2=polyfit(Toutcal,Toffcal,3); 123 | Toutfit = [-15:1:15]; 124 | Tofffit = polyval(p2,Toutfit) 125 | plot(Toutfit,Tofffit,'c') 126 | -------------------------------------------------------------------------------- /example33_Errorcorrection_figure1.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Example 3.3 - produce figure 3.7 (a) and (b) 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | clear all 7 | close all 8 | 9 | hours = 3 10 | % Simulation setup 11 | Outdoor_temp_start = 0 12 | Outdoor_temp_end = 5 13 | color = ['rgbcm']; 14 | Time = 60*hours % in minutes 15 | Timestep = 1 % second 16 | N_timestep = Time*60/Timestep; 17 | Tout = linspace(Outdoor_temp_start,Outdoor_temp_end,N_timestep); 18 | %figure(121), plot(Tout) 19 | 20 | %% 21 | Tset = 20 % degree C 22 | TDB = 1 %degree C 23 | Troom0 = 20; 24 | Prated = 5020 %W 25 | Area = 228 % in m^2 26 | Vair = 228*5 % m^3 27 | DensityAir = 1.225 % kg/m^3 28 | Cp_air = 1005 % J/kg.k 29 | Cv_air = 780 % J/kg.k 30 | C_funiture = 1000 % J/kg.k 31 | V_furniture = 1000 %kg 32 | UA = 111 %W/degree C 33 | UAmass = 3924 %W/degree C 34 | 35 | T_on=0; 36 | T_off=0; 37 | 38 | R1 = 1/UA 39 | R2 = 1/UAmass 40 | Ca = Vair*DensityAir*Cv_air 41 | Cm = V_furniture*C_funiture 42 | 43 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 44 | B = [1/(R1*Ca) 1/Ca ;0 0] 45 | C = [1 0; 0 1] 46 | D = [0; 0] 47 | 48 | status = zeros(1, Time); 49 | Q = zeros(1, Time); 50 | x0 = [Troom0;Troom0]; 51 | 52 | if x0(1) > (Tset + TDB/2) 53 | status(1) = 0; 54 | elseif x0(1)<(Tset - TDB/2) 55 | status(1) = 1; 56 | end 57 | if status(1) == 1 58 | Q0(1) = Prated*Timestep; 59 | else 60 | Q0(1) = 0; 61 | end 62 | u0 = [Tout(1); Q0] 63 | 64 | for i = 2:1:N_timestep 65 | DT = A*x0 + B*u0; 66 | x1 = x0+DT; 67 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 68 | status(i) = 0; 69 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 70 | status(i) = 1; 71 | else 72 | status(i) = status(i-1); 73 | end 74 | if status(i) == 1 75 | Q(i) = Prated*Timestep; 76 | T_on(i,1) = T_on(i-1,1)+1; 77 | T_off(i,1) = 0; 78 | else 79 | Q(i) = 0; 80 | T_off(i,1) = T_off(i-1,1)+1; 81 | T_on(i,1) = 0; 82 | end 83 | x0 = x1; 84 | u0 = [Tout(i); Q(i)]; 85 | Troom_record(i-1,1) = x1(1); 86 | Tmass_record(i-1,1) = x1(2); 87 | end 88 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 89 | 90 | figure(1) 91 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 92 | set(gcf,'DefaultTextFontSize',14) 93 | plot(Time,Troom_record,color(1),'LineWidth',1) 94 | % plot(Tmass_record,'b') 95 | xlabel('Time (minute)') 96 | ylabel('Temperature (^oC)') 97 | hold on 98 | title('(a) SS-RCQ versus Linearized ETP') 99 | xlim([0 60*60*hours]/60) 100 | 101 | %% 102 | % calculate the room temperature using a linear model 103 | 104 | Ton_0degree = 538; % in second 105 | Toff_0degree = 680; % in second 106 | dT_on = TDB/Ton_0degree; 107 | dT_off = -TDB/Toff_0degree; 108 | x00 = x0(1); 109 | 110 | if x00 > (Tset + TDB/2) 111 | status(1) = 0; 112 | elseif x00<(Tset - TDB/2) 113 | status(1) = 1; 114 | end 115 | if status(1) == 1 116 | Q0(1) = Prated*Timestep; 117 | DT_linear = TDB/Ton_0degree; 118 | else 119 | Q0(1) = 0; 120 | DT_linear = -TDB/Toff_0degree; 121 | end 122 | 123 | u0 = [Tout(1); Q0] 124 | 125 | 126 | for i = 2:1:N_timestep 127 | x11 = x00+DT_linear; 128 | 129 | DT_actual = A*x0 + B*u0; 130 | x1 = x0+DT_actual; 131 | 132 | if (x11>(Tset + TDB/2)) && (status(i-1) == 1) 133 | status(i) = 0; 134 | elseif (x11< (Tset - TDB/2)) && status(i-1) == 0 135 | status(i) = 1; 136 | else 137 | status(i) = status(i-1); 138 | end 139 | if status(i) == 1 140 | Q(i) = Prated*Timestep; 141 | DT_linear = TDB/Ton_0degree; 142 | T_on(i,1) = T_on(i-1,1)+1; 143 | T_off(i,1) = 0; 144 | else 145 | Q(i) = 0; 146 | DT_linear = -TDB/Toff_0degree; 147 | T_off(i,1) = T_off(i-1,1)+1; 148 | T_on(i,1) = 0; 149 | end 150 | x00=x11; 151 | x0 = x1; 152 | u0 = [Tout(i); Q(i)]; 153 | Troom_record_simple(i-1,1) = x11; 154 | Troom_record_actual(i-1,1) = x1(1); 155 | 156 | end 157 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 158 | 159 | figure(1) 160 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 161 | set(gcf,'DefaultTextFontSize',14) 162 | plot(Time,Troom_record_simple,'b','LineWidth',1) 163 | % plot(Tmass_record,'b') 164 | xlabel('Time (minute)') 165 | ylabel('Temperature (^oC)') 166 | legend('SS-RCQ','Linear ETP') 167 | hold on 168 | ylim([19 22]) 169 | 170 | 171 | figure(2) 172 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 173 | set(gcf,'DefaultTextFontSize',14) 174 | plot(Time,Troom_record_actual,'r','LineWidth',1) 175 | hold on 176 | plot(Time,Troom_record_simple,'b','LineWidth',1) 177 | % plot(Tmass_record,'b') 178 | xlabel('Time (minute)') 179 | ylabel('Temperature (^oC)') 180 | hold on 181 | 182 | 183 | title('(b) Linearized ETP with no Error Correction') 184 | legend('SS-RCQ','Linear ETP') 185 | xlim([0 60*60*hours]/60) 186 | ylim([19 22]) 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /example33_Errorcorrection_part1.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Example 3.3 - produce figure 3.7 (a) and (b) 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | clear all 7 | close all 8 | 9 | % Simulation setup 10 | Outdoor_temp_start = 0 11 | Outdoor_temp_end = 5 12 | color = ['rgbcm']; 13 | Time = 60*3 % in minutes 14 | Timestep = 1 % second 15 | N_timestep = Time*60/Timestep; 16 | Tout = linspace(Outdoor_temp_start,Outdoor_temp_end,N_timestep); 17 | 18 | 19 | Tset = 20 % degree C 20 | TDB = 1 %degree C 21 | Troom0 = 20; 22 | Prated = 5020 %W 23 | Area = 228 % in m^2 24 | Vair = 228*5 % m^3 25 | DensityAir = 1.225 % kg/m^3 26 | Cp_air = 1005 % J/kg.k 27 | Cv_air = 780 % J/kg.k 28 | C_funiture = 1000 % J/kg.k 29 | V_furniture = 1000 %kg 30 | UA = 111 %W/degree C 31 | UAmass = 3924 %W/degree C 32 | 33 | T_on=0; 34 | T_off=0; 35 | 36 | R1 = 1/UA 37 | R2 = 1/UAmass 38 | Ca = Vair*DensityAir*Cv_air 39 | Cm = V_furniture*C_funiture 40 | 41 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 42 | B = [1/(R1*Ca) 1/Ca ;0 0] 43 | C = [1 0; 0 1] 44 | D = [0; 0] 45 | 46 | status = zeros(1, Time); 47 | Q = zeros(1, Time); 48 | x0 = [Troom0;Troom0]; 49 | 50 | if x0(1) > (Tset + TDB/2) 51 | status(1) = 0; 52 | elseif x0(1)<(Tset - TDB/2) 53 | status(1) = 1; 54 | end 55 | if status(1) == 1 56 | Q0(1) = Prated*Timestep; 57 | else 58 | Q0(1) = 0; 59 | end 60 | u0 = [Tout(1); Q0] 61 | 62 | for i = 2:1:N_timestep 63 | DT = A*x0 + B*u0; 64 | x1 = x0+DT; 65 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 66 | status(i) = 0; 67 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 68 | status(i) = 1; 69 | else 70 | status(i) = status(i-1); 71 | end 72 | if status(i) == 1 73 | Q(i) = Prated*Timestep; 74 | T_on(i,1) = T_on(i-1,1)+1; 75 | T_off(i,1) = 0; 76 | else 77 | Q(i) = 0; 78 | T_off(i,1) = T_off(i-1,1)+1; 79 | T_on(i,1) = 0; 80 | end 81 | x0 = x1; 82 | u0 = [Tout(i); Q(i)]; 83 | Troom_record(i-1,1) = x1(1); 84 | Tmass_record(i-1,1) = x1(2); 85 | end 86 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 87 | 88 | figure(1) 89 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 90 | set(gcf,'DefaultTextFontSize',14) 91 | plot(Time,Troom_record,color(1),'LineWidth',1) 92 | % plot(Tmass_record,'b') 93 | xlabel('Time (minute)') 94 | ylabel('Temperature (^oC)') 95 | hold on 96 | title('(a) SS-RCQ versus Linearized ETP') 97 | xlim([0 60*60*3]/60) 98 | 99 | 100 | % calculate the room temperature using a linear model 101 | 102 | Ton_0degree = 538; % in second 103 | Toff_0degree = 680; % in second 104 | dT_on = TDB/Ton_0degree; 105 | dT_off = -TDB/Toff_0degree; 106 | x00 = x0(1); 107 | 108 | if x00 > (Tset + TDB/2) 109 | status(1) = 0; 110 | elseif x00<(Tset - TDB/2) 111 | status(1) = 1; 112 | end 113 | if status(1) == 1 114 | Q0(1) = Prated*Timestep; 115 | DT_linear = TDB/Ton_0degree; 116 | else 117 | Q0(1) = 0; 118 | DT_linear = -TDB/Toff_0degree; 119 | end 120 | 121 | u0 = [Tout(1); Q0] 122 | 123 | 124 | for i = 2:1:N_timestep 125 | x11 = x00+DT_linear; 126 | 127 | DT_actual = A*x0 + B*u0; 128 | x1 = x0+DT_actual; 129 | 130 | if (x11>(Tset + TDB/2)) && (status(i-1) == 1) 131 | status(i) = 0; 132 | elseif (x11< (Tset - TDB/2)) && status(i-1) == 0 133 | status(i) = 1; 134 | else 135 | status(i) = status(i-1); 136 | end 137 | if status(i) == 1 138 | Q(i) = Prated*Timestep; 139 | DT_linear = TDB/Ton_0degree; 140 | T_on(i,1) = T_on(i-1,1)+1; 141 | T_off(i,1) = 0; 142 | else 143 | Q(i) = 0; 144 | DT_linear = -TDB/Toff_0degree; 145 | T_off(i,1) = T_off(i-1,1)+1; 146 | T_on(i,1) = 0; 147 | end 148 | x00=x11; 149 | x0 = x1; 150 | u0 = [Tout(i); Q(i)]; 151 | Troom_record_simple(i-1,1) = x11; 152 | Troom_record_actual(i-1,1) = x1(1); 153 | 154 | end 155 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 156 | 157 | figure(1) 158 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 159 | set(gcf,'DefaultTextFontSize',14) 160 | plot(Time,Troom_record_simple,'b','LineWidth',1) 161 | % plot(Tmass_record,'b') 162 | xlabel('Time (minute)') 163 | ylabel('Temperature (^oC)') 164 | legend('SS-RCQ','Linear ETP') 165 | hold on 166 | ylim([19 22]) 167 | 168 | 169 | figure(2) 170 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 171 | set(gcf,'DefaultTextFontSize',14) 172 | plot(Time,Troom_record_actual,'r','LineWidth',1) 173 | hold on 174 | plot(Time,Troom_record_simple,'b','LineWidth',1) 175 | % plot(Tmass_record,'b') 176 | xlabel('Time (minute)') 177 | ylabel('Temperature (^oC)') 178 | hold on 179 | 180 | 181 | title('(b) Linearized ETP with no Error Correction') 182 | legend('SS-RCQ','Linear ETP') 183 | xlim([0 60*60*3]/60) 184 | ylim([19 22]) 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | -------------------------------------------------------------------------------- /example33_Errorcorrection_part1_G.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Example 3.3 - produce figure 3.7 (a) and (b) 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | clear all 7 | close all 8 | 9 | % Simulation setup 10 | Outdoor_temp_start = 0 11 | Outdoor_temp_end = 5 12 | color = ['rgbcm']; 13 | Time = 60*3 % in minutes 14 | Timestep = 1 % second 15 | N_timestep = Time*60/Timestep; 16 | Tout = linspace(Outdoor_temp_start,Outdoor_temp_end,N_timestep); 17 | %figure(121), plot(Tout) 18 | 19 | %% 20 | Tset = 20 % degree C 21 | TDB = 1 %degree C 22 | Troom0 = 20; 23 | Prated = 5020 %W 24 | Area = 228 % in m^2 25 | Vair = 228*5 % m^3 26 | DensityAir = 1.225 % kg/m^3 27 | Cp_air = 1005 % J/kg.k 28 | Cv_air = 780 % J/kg.k 29 | C_funiture = 1000 % J/kg.k 30 | V_furniture = 1000 %kg 31 | UA = 111 %W/degree C 32 | UAmass = 3924 %W/degree C 33 | 34 | T_on=0; 35 | T_off=0; 36 | 37 | R1 = 1/UA 38 | R2 = 1/UAmass 39 | Ca = Vair*DensityAir*Cv_air 40 | Cm = V_furniture*C_funiture 41 | 42 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 43 | B = [1/(R1*Ca) 1/Ca ;0 0] 44 | C = [1 0; 0 1] 45 | D = [0; 0] 46 | 47 | status = zeros(1, Time); 48 | Q = zeros(1, Time); 49 | x0 = [Troom0;Troom0]; 50 | 51 | if x0(1) > (Tset + TDB/2) 52 | status(1) = 0; 53 | elseif x0(1)<(Tset - TDB/2) 54 | status(1) = 1; 55 | end 56 | if status(1) == 1 57 | Q0(1) = Prated*Timestep; 58 | else 59 | Q0(1) = 0; 60 | end 61 | u0 = [Tout(1); Q0] 62 | 63 | for i = 2:1:N_timestep 64 | DT = A*x0 + B*u0; 65 | x1 = x0+DT; 66 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 67 | status(i) = 0; 68 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 69 | status(i) = 1; 70 | else 71 | status(i) = status(i-1); 72 | end 73 | if status(i) == 1 74 | Q(i) = Prated*Timestep; 75 | T_on(i,1) = T_on(i-1,1)+1; 76 | T_off(i,1) = 0; 77 | else 78 | Q(i) = 0; 79 | T_off(i,1) = T_off(i-1,1)+1; 80 | T_on(i,1) = 0; 81 | end 82 | x0 = x1; 83 | u0 = [Tout(i); Q(i)]; 84 | Troom_record(i-1,1) = x1(1); 85 | Tmass_record(i-1,1) = x1(2); 86 | end 87 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 88 | 89 | figure(1) 90 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 91 | set(gcf,'DefaultTextFontSize',14) 92 | plot(Time,Troom_record,color(1),'LineWidth',1) 93 | % plot(Tmass_record,'b') 94 | xlabel('Time (minute)') 95 | ylabel('Temperature (^oC)') 96 | hold on 97 | title('(a) SS-RCQ versus Linearized ETP') 98 | xlim([0 60*60*3]/60) 99 | 100 | 101 | % calculate the room temperature using a linear model 102 | 103 | Ton_0degree = 538; % in second 104 | Toff_0degree = 680; % in second 105 | dT_on = TDB/Ton_0degree; 106 | dT_off = -TDB/Toff_0degree; 107 | x00 = x0(1); 108 | 109 | if x00 > (Tset + TDB/2) 110 | status(1) = 0; 111 | elseif x00<(Tset - TDB/2) 112 | status(1) = 1; 113 | end 114 | if status(1) == 1 115 | Q0(1) = Prated*Timestep; 116 | DT_linear = TDB/Ton_0degree; 117 | else 118 | Q0(1) = 0; 119 | DT_linear = -TDB/Toff_0degree; 120 | end 121 | 122 | u0 = [Tout(1); Q0] 123 | 124 | 125 | for i = 2:1:N_timestep 126 | x11 = x00+DT_linear; 127 | 128 | DT_actual = A*x0 + B*u0; 129 | x1 = x0+DT_actual; 130 | 131 | if (x11>(Tset + TDB/2)) && (status(i-1) == 1) 132 | status(i) = 0; 133 | elseif (x11< (Tset - TDB/2)) && status(i-1) == 0 134 | status(i) = 1; 135 | else 136 | status(i) = status(i-1); 137 | end 138 | if status(i) == 1 139 | Q(i) = Prated*Timestep; 140 | DT_linear = TDB/Ton_0degree; 141 | T_on(i,1) = T_on(i-1,1)+1; 142 | T_off(i,1) = 0; 143 | else 144 | Q(i) = 0; 145 | DT_linear = -TDB/Toff_0degree; 146 | T_off(i,1) = T_off(i-1,1)+1; 147 | T_on(i,1) = 0; 148 | end 149 | x00=x11; 150 | x0 = x1; 151 | u0 = [Tout(i); Q(i)]; 152 | Troom_record_simple(i-1,1) = x11; 153 | Troom_record_actual(i-1,1) = x1(1); 154 | 155 | end 156 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 157 | 158 | figure(1) 159 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 160 | set(gcf,'DefaultTextFontSize',14) 161 | plot(Time,Troom_record_simple,'b','LineWidth',1) 162 | % plot(Tmass_record,'b') 163 | xlabel('Time (minute)') 164 | ylabel('Temperature (^oC)') 165 | legend('SS-RCQ','Linear ETP') 166 | hold on 167 | ylim([19 22]) 168 | 169 | 170 | figure(2) 171 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 172 | set(gcf,'DefaultTextFontSize',14) 173 | plot(Time,Troom_record_actual,'r','LineWidth',1) 174 | hold on 175 | plot(Time,Troom_record_simple,'b','LineWidth',1) 176 | % plot(Tmass_record,'b') 177 | xlabel('Time (minute)') 178 | ylabel('Temperature (^oC)') 179 | hold on 180 | 181 | 182 | title('(b) Linearized ETP with no Error Correction') 183 | legend('SS-RCQ','Linear ETP') 184 | xlim([0 60*60*3]/60) 185 | ylim([19 22]) 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | -------------------------------------------------------------------------------- /example33_Errorcorrection_part2.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Example 3.3 - produce figure 3.7(c) 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | clear all 7 | close all 8 | 9 | % Simulation setup 10 | Outdoor_temp_start = 0 11 | Outdoor_temp_end = 5 12 | color = ['rgbcm']; 13 | Time = 60*3 % in minutes 14 | Timestep = 1 % second 15 | N_timestep = Time*60/Timestep; 16 | Tout = linspace(Outdoor_temp_start,Outdoor_temp_end,N_timestep); 17 | Tset = 20 % degree C 18 | TDB = 1 %degree C 19 | Troom0 = 20; 20 | Prated = 5020 %W 21 | Area = 228 % in m^2 22 | Vair = 228*5 % m^3 23 | DensityAir = 1.225 % kg/m^3 24 | Cp_air = 1005 % J/kg.k 25 | Cv_air = 780 % J/kg.k 26 | C_funiture = 1000 % J/kg.k 27 | V_furniture = 1000 %kg 28 | UA = 111 %W/degree C 29 | UAmass = 3924 %W/degree C 30 | T_on=0; 31 | T_off=0; 32 | 33 | R1 = 1/UA 34 | R2 = 1/UAmass 35 | Ca = Vair*DensityAir*Cv_air 36 | Cm = V_furniture*C_funiture 37 | 38 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 39 | B = [1/(R1*Ca) 1/Ca ;0 0] 40 | C = [1 0; 0 1] 41 | D = [0; 0] 42 | 43 | status = zeros(1, Time); 44 | Q = zeros(1, Time); 45 | x0 = [Troom0;Troom0]; 46 | 47 | if x0(1) > (Tset + TDB/2) 48 | status(1) = 0; 49 | elseif x0(1)<(Tset - TDB/2) 50 | status(1) = 1; 51 | end 52 | if status(1) == 1 53 | Q0(1) = Prated*Timestep; 54 | else 55 | Q0(1) = 0; 56 | end 57 | u0 = [Tout(1); Q0] 58 | 59 | for i = 2:1:N_timestep 60 | DT = A*x0 + B*u0; 61 | x1 = x0+DT; 62 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 63 | status(i) = 0; 64 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 65 | status(i) = 1; 66 | else 67 | status(i) = status(i-1); 68 | end 69 | if status(i) == 1 70 | Q(i) = Prated*Timestep; 71 | T_on(i,1) = T_on(i-1,1)+1; 72 | T_off(i,1) = 0; 73 | else 74 | Q(i) = 0; 75 | T_off(i,1) = T_off(i-1,1)+1; 76 | T_on(i,1) = 0; 77 | end 78 | x0 = x1; 79 | u0 = [Tout(i); Q(i)]; 80 | Troom_record(i-1,1) = x1(1); 81 | Tmass_record(i-1,1) = x1(2); 82 | end 83 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 84 | 85 | figure(1) 86 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 87 | set(gcf,'DefaultTextFontSize',14) 88 | plot(Time,Troom_record,color(1),'LineWidth',2) 89 | % plot(Tmass_record,'b') 90 | xlabel('Time (minute)') 91 | ylabel('Temperature (^oC)') 92 | hold on 93 | title('Room Temperature Profiles') 94 | xlim([0 60*60*3]/60) 95 | 96 | 97 | % calculate the room temperature using a linear model 98 | Ton_0degree = 538; % in second 99 | Toff_0degree = 680; % in second 100 | dT_on = TDB/Ton_0degree; 101 | dT_off = -TDB/Toff_0degree; 102 | x00 = x0(1); 103 | 104 | if x00 > (Tset + TDB/2) 105 | status(1) = 0; 106 | elseif x00<(Tset - TDB/2) 107 | status(1) = 1; 108 | end 109 | if status(1) == 1 110 | Q0(1) = Prated*Timestep; 111 | DT_linear = TDB/Ton_0degree; 112 | else 113 | Q0(1) = 0; 114 | DT_linear = -TDB/Toff_0degree; 115 | end 116 | u0 = [Tout(1); Q0] 117 | 118 | for i = 2:1:N_timestep 119 | x11 = x00+DT_linear; 120 | 121 | DT_actual = A*x0 + B*u0; 122 | x1 = x0+DT_actual; 123 | 124 | if (x11>(Tset + TDB/2)) && (status(i-1) == 1) 125 | status(i) = 0; 126 | elseif (x11< (Tset - TDB/2)) && status(i-1) == 0 127 | status(i) = 1; 128 | else 129 | status(i) = status(i-1); 130 | end 131 | if status(i) == 1 132 | Q(i) = Prated*Timestep; 133 | DT_linear = TDB/Ton_0degree; 134 | T_on(i,1) = T_on(i-1,1)+1; 135 | T_off(i,1) = 0; 136 | else 137 | Q(i) = 0; 138 | DT_linear = -TDB/Toff_0degree; 139 | T_off(i,1) = T_off(i-1,1)+1; 140 | T_on(i,1) = 0; 141 | end 142 | 143 | if i == 900 | i == 1800 | i == 2700 | i == 3600 | i == 4500 | i == 5400 | i == 6300 ... 144 | | i == 7200 | i == 8100 | i == 9000 | i == 9900 | i == 10800 145 | x00=x1(1); 146 | x0 = x1; 147 | figure(2) 148 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 149 | set(gcf,'DefaultTextFontSize',16) 150 | plot(i/60,x1(1),'r^') 151 | hold on 152 | plot(i/60,x11,'bo') 153 | 154 | else 155 | x00=x11; 156 | x0 = x1; 157 | end 158 | 159 | u0 = [Tout(i); Q(i)]; 160 | Troom_record_simple(i-1,1) = x11; 161 | Troom_record_actual(i-1,1) = x1(1); 162 | 163 | end 164 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 165 | 166 | figure(1) 167 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 168 | set(gcf,'DefaultTextFontSize',14) 169 | plot(Time,Troom_record_simple,'b--','LineWidth',2) 170 | % plot(Tmass_record,'b') 171 | xlabel('Time (minute)') 172 | ylabel('Temperature (^oC)') 173 | legend('SS-RCQ','Linear ETP') 174 | hold on 175 | 176 | 177 | figure(2) 178 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 179 | set(gcf,'DefaultTextFontSize',16) 180 | plot(Time,Troom_record_actual,'r') 181 | hold on 182 | plot(Time,Troom_record_simple,'b') 183 | % plot(Tmass_record,'b') 184 | xlabel('Time (minute)') 185 | ylabel('Temperature (^oC)') 186 | hold on 187 | 188 | 189 | title('(c) Linearized ETP with 15-miunute Error Correction') 190 | legend('SS-RCQ','Linear ETP') 191 | xlim([0 60*60*3]/60) 192 | ylim([19 22]) 193 | 194 | 195 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /example33_Errorcorrection_part3.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Example 3.3 - produce figure 3.7(d) 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | clear all 7 | close all 8 | 9 | % Simulation setup 10 | Outdoor_temp_start = 0 11 | Outdoor_temp_end = 5 12 | color = ['rgbcm']; 13 | Time = 60*3 % in minutes 14 | Timestep = 1 % second 15 | N_timestep = Time*60/Timestep; 16 | Tout = linspace(Outdoor_temp_start,Outdoor_temp_end,N_timestep); 17 | 18 | 19 | Tset = 20 % degree C 20 | TDB = 1 %degree C 21 | Troom0 = 20; 22 | Prated = 5020 %W 23 | Area = 228 % in m^2 24 | Vair = 228*5 % m^3 25 | DensityAir = 1.225 % kg/m^3 26 | Cp_air = 1005 % J/kg.k 27 | Cv_air = 780 % J/kg.k 28 | C_funiture = 1000 % J/kg.k 29 | V_furniture = 1000 %kg 30 | UA = 111 %W/degree C 31 | UAmass = 3924 %W/degree C 32 | 33 | T_on=0; 34 | T_off=0; 35 | 36 | R1 = 1/UA 37 | R2 = 1/UAmass 38 | Ca = Vair*DensityAir*Cv_air 39 | Cm = V_furniture*C_funiture 40 | 41 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 42 | B = [1/(R1*Ca) 1/Ca ;0 0] 43 | C = [1 0; 0 1] 44 | D = [0; 0] 45 | 46 | status = zeros(1, Time); 47 | Q = zeros(1, Time); 48 | x0 = [Troom0;Troom0]; 49 | 50 | if x0(1) > (Tset + TDB/2) 51 | status(1) = 0; 52 | elseif x0(1)<(Tset - TDB/2) 53 | status(1) = 1; 54 | end 55 | if status(1) == 1 56 | Q0(1) = Prated*Timestep; 57 | else 58 | Q0(1) = 0; 59 | end 60 | u0 = [Tout(1); Q0] 61 | 62 | for i = 2:1:N_timestep 63 | DT = A*x0 + B*u0; 64 | x1 = x0+DT; 65 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 66 | status(i) = 0; 67 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 68 | status(i) = 1; 69 | else 70 | status(i) = status(i-1); 71 | end 72 | if status(i) == 1 73 | Q(i) = Prated*Timestep; 74 | T_on(i,1) = T_on(i-1,1)+1; 75 | T_off(i,1) = 0; 76 | else 77 | Q(i) = 0; 78 | T_off(i,1) = T_off(i-1,1)+1; 79 | T_on(i,1) = 0; 80 | end 81 | 82 | 83 | x0 = x1; 84 | u0 = [Tout(i); Q(i)]; 85 | Troom_record(i-1,1) = x1(1); 86 | Tmass_record(i-1,1) = x1(2); 87 | 88 | 89 | end 90 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 91 | 92 | figure(1) 93 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 94 | set(gcf,'DefaultTextFontSize',14) 95 | plot(Time,Troom_record,color(1),'LineWidth',2) 96 | % plot(Tmass_record,'b') 97 | xlabel('Time (minute)') 98 | ylabel('Temperature (^oC)') 99 | hold on 100 | title('Room Temperature Profiles') 101 | xlim([0 60*60*3]/60) 102 | 103 | 104 | % calculate the room temperature using a linear model 105 | 106 | Ton_0degree = 538; % in second 107 | Toff_0degree = 680; % in second 108 | dT_on = TDB/Ton_0degree; 109 | dT_off = -TDB/Toff_0degree; 110 | x00 = x0(1); 111 | 112 | if x00 > (Tset + TDB/2) 113 | status(1) = 0; 114 | elseif x00<(Tset - TDB/2) 115 | status(1) = 1; 116 | end 117 | if status(1) == 1 118 | Q0(1) = Prated*Timestep; 119 | DT_linear = TDB/Ton_0degree; 120 | else 121 | Q0(1) = 0; 122 | DT_linear = -TDB/Toff_0degree; 123 | end 124 | 125 | u0 = [Tout(1); Q0] 126 | 127 | 128 | for i = 2:1:N_timestep 129 | x11 = x00+DT_linear; 130 | 131 | DT_actual = A*x0 + B*u0; 132 | x1 = x0+DT_actual; 133 | 134 | if (x11>(Tset + TDB/2)) && (status(i-1) == 1) 135 | status(i) = 0; 136 | elseif (x11< (Tset - TDB/2)) && status(i-1) == 0 137 | status(i) = 1; 138 | else 139 | status(i) = status(i-1); 140 | end 141 | if status(i) == 1 142 | Q(i) = Prated*Timestep; 143 | DT_linear = TDB/Ton_0degree; 144 | T_on(i,1) = T_on(i-1,1)+1; 145 | T_off(i,1) = 0; 146 | else 147 | Q(i) = 0; 148 | DT_linear = -TDB/Toff_0degree; 149 | T_off(i,1) = T_off(i-1,1)+1; 150 | T_on(i,1) = 0; 151 | end 152 | 153 | if i == 1800 | i == 3600 | i == 5400 | i == 7200 | i == 9000 | i == 10800 154 | x00=x1(1); 155 | x0 = x1; 156 | figure(2) 157 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 158 | set(gcf,'DefaultTextFontSize',16) 159 | plot(i/60,x1(1),'r^') 160 | hold on 161 | plot(i/60,x11,'bo') 162 | 163 | else 164 | x00=x11; 165 | x0 = x1; 166 | end 167 | 168 | u0 = [Tout(i); Q(i)]; 169 | Troom_record_simple(i-1,1) = x11; 170 | Troom_record_actual(i-1,1) = x1(1); 171 | 172 | end 173 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 174 | 175 | figure(1) 176 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 177 | set(gcf,'DefaultTextFontSize',14) 178 | plot(Time,Troom_record_simple,'b--','LineWidth',2) 179 | % plot(Tmass_record,'b') 180 | xlabel('Time (minute)') 181 | ylabel('Temperature (^oC)') 182 | legend('SS-RCQ','Linear ETP') 183 | hold on 184 | 185 | 186 | figure(2) 187 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 188 | set(gcf,'DefaultTextFontSize',16) 189 | plot(Time,Troom_record_actual,'r') 190 | hold on 191 | plot(Time,Troom_record_simple,'b') 192 | % plot(Tmass_record,'b') 193 | xlabel('Time (minute)') 194 | ylabel('Temperature (^oC)') 195 | hold on 196 | 197 | 198 | title('(d) Linearized ETP with 30-miunute Error Correction') 199 | legend('SS-RCQ','Linear ETP') 200 | xlim([0 60*60*3]/60) 201 | ylim([19 22]) 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | -------------------------------------------------------------------------------- /example33_Errorcorrection_part4.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Example 3.3 - produce figure 3.7(e) 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | clear all 7 | close all 8 | 9 | % Simulation setup 10 | Outdoor_temp_start = 0 11 | Outdoor_temp_end = 5 12 | color = ['rgbcm']; 13 | Time = 60*3 % in minutes 14 | Timestep = 1 % second 15 | N_timestep = Time*60/Timestep; 16 | Tout = linspace(Outdoor_temp_start,Outdoor_temp_end,N_timestep); 17 | 18 | 19 | Tset = 20 % degree C 20 | TDB = 1 %degree C 21 | Troom0 = 20; 22 | Prated = 5020 %W 23 | Area = 228 % in m^2 24 | Vair = 228*5 % m^3 25 | DensityAir = 1.225 % kg/m^3 26 | Cp_air = 1005 % J/kg.k 27 | Cv_air = 780 % J/kg.k 28 | C_funiture = 1000 % J/kg.k 29 | V_furniture = 1000 %kg 30 | UA = 111 %W/degree C 31 | UAmass = 3924 %W/degree C 32 | 33 | T_on=0; 34 | T_off=0; 35 | 36 | R1 = 1/UA 37 | R2 = 1/UAmass 38 | Ca = Vair*DensityAir*Cv_air 39 | Cm = V_furniture*C_funiture 40 | 41 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)] 42 | B = [1/(R1*Ca) 1/Ca ;0 0] 43 | C = [1 0; 0 1] 44 | D = [0; 0] 45 | 46 | status = zeros(1, Time); 47 | Q = zeros(1, Time); 48 | x0 = [Troom0;Troom0]; 49 | 50 | if x0(1) > (Tset + TDB/2) 51 | status(1) = 0; 52 | elseif x0(1)<(Tset - TDB/2) 53 | status(1) = 1; 54 | end 55 | if status(1) == 1 56 | Q0(1) = Prated*Timestep; 57 | else 58 | Q0(1) = 0; 59 | end 60 | u0 = [Tout(1); Q0] 61 | 62 | for i = 2:1:N_timestep 63 | DT = A*x0 + B*u0; 64 | x1 = x0+DT; 65 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 66 | status(i) = 0; 67 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 68 | status(i) = 1; 69 | else 70 | status(i) = status(i-1); 71 | end 72 | if status(i) == 1 73 | Q(i) = Prated*Timestep; 74 | T_on(i,1) = T_on(i-1,1)+1; 75 | T_off(i,1) = 0; 76 | else 77 | Q(i) = 0; 78 | T_off(i,1) = T_off(i-1,1)+1; 79 | T_on(i,1) = 0; 80 | end 81 | 82 | 83 | x0 = x1; 84 | u0 = [Tout(i); Q(i)]; 85 | Troom_record(i-1,1) = x1(1); 86 | Tmass_record(i-1,1) = x1(2); 87 | 88 | 89 | end 90 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 91 | 92 | figure(1) 93 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 94 | set(gcf,'DefaultTextFontSize',14) 95 | plot(Time,Troom_record,color(1),'LineWidth',2) 96 | % plot(Tmass_record,'b') 97 | xlabel('Time (minute)') 98 | ylabel('Temperature (^oC)') 99 | hold on 100 | title('Room Temperature Profiles') 101 | xlim([0 60*60*3]/60) 102 | 103 | 104 | % calculate the room temperature using a linear model 105 | 106 | Ton_0degree = 538; % in second 107 | Toff_0degree = 680; % in second 108 | dT_on = TDB/Ton_0degree; 109 | dT_off = -TDB/Toff_0degree; 110 | x00 = x0(1); 111 | 112 | if x00 > (Tset + TDB/2) 113 | status(1) = 0; 114 | elseif x00<(Tset - TDB/2) 115 | status(1) = 1; 116 | end 117 | if status(1) == 1 118 | Q0(1) = Prated*Timestep; 119 | DT_linear = TDB/Ton_0degree; 120 | else 121 | Q0(1) = 0; 122 | DT_linear = -TDB/Toff_0degree; 123 | end 124 | 125 | u0 = [Tout(1); Q0] 126 | 127 | 128 | for i = 2:1:N_timestep 129 | x11 = x00+DT_linear; 130 | 131 | DT_actual = A*x0 + B*u0; 132 | x1 = x0+DT_actual; 133 | 134 | if (x11>(Tset + TDB/2)) && (status(i-1) == 1) 135 | status(i) = 0; 136 | elseif (x11< (Tset - TDB/2)) && status(i-1) == 0 137 | status(i) = 1; 138 | else 139 | status(i) = status(i-1); 140 | end 141 | if status(i) == 1 142 | Q(i) = Prated*Timestep; 143 | DT_linear = TDB/Ton_0degree; 144 | T_on(i,1) = T_on(i-1,1)+1; 145 | T_off(i,1) = 0; 146 | else 147 | Q(i) = 0; 148 | DT_linear = -TDB/Toff_0degree; 149 | T_off(i,1) = T_off(i-1,1)+1; 150 | T_on(i,1) = 0; 151 | end 152 | 153 | if i == 3600 | i == 7200 | i == 10800 154 | x00=x1(1); 155 | x0 = x1; 156 | figure(2) 157 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 158 | set(gcf,'DefaultTextFontSize',16) 159 | plot(i/60,x1(1),'r^') 160 | hold on 161 | plot(i/60,x11,'bo') 162 | 163 | else 164 | x00=x11; 165 | x0 = x1; 166 | end 167 | 168 | u0 = [Tout(i); Q(i)]; 169 | Troom_record_simple(i-1,1) = x11; 170 | Troom_record_actual(i-1,1) = x1(1); 171 | 172 | end 173 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 174 | 175 | figure(1) 176 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 177 | set(gcf,'DefaultTextFontSize',14) 178 | plot(Time,Troom_record_simple,'b--','LineWidth',2) 179 | % plot(Tmass_record,'b') 180 | xlabel('Time (minute)') 181 | ylabel('Temperature (^oC)') 182 | legend('SS-RCQ','Linear ETP') 183 | hold on 184 | 185 | 186 | figure(2) 187 | set(gcf,'DefaultAxesFontSize',16) %<--------set character size 188 | set(gcf,'DefaultTextFontSize',16) 189 | plot(Time,Troom_record_actual,'r') 190 | hold on 191 | plot(Time,Troom_record_simple,'b') 192 | % plot(Tmass_record,'b') 193 | xlabel('Time (minute)') 194 | ylabel('Temperature (^oC)') 195 | hold on 196 | 197 | 198 | title('(e) Linearized ETP with 60-miunute Error Correction') 199 | legend('SS-RCQ','Linear ETP') 200 | xlim([0 60*60*3]/60) 201 | ylim([19 22]) 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | -------------------------------------------------------------------------------- /example34_randomization.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu for DR book 2 | % Example 3.4 3 | % Code written 9/21/2017 4 | % Copyright North Carolina State University 5 | 6 | clear all 7 | close all 8 | 9 | for ii = 1:1000 10 | % Simulation setup 11 | Tout = [0]; 12 | color = ['rgbcm']; 13 | Time = 60; % in minutes 14 | Timestep = 1; % second 15 | N_timestep = Time*60/Timestep; 16 | Tset = 20; % degree C 17 | TDB = 1; %degree C 18 | Prated = 5020; %W 19 | Area = 228; % in m^2 20 | Vair = 228*5; % m^3 21 | DensityAir = 1.225; % kg/m^3 22 | Cp_air = 1005; % J/kg.k 23 | Cv_air = 780; % J/kg.k 24 | C_funiture = 1000; % J/kg.k 25 | V_furniture = 1000; %kg 26 | UA = 111; %W/degree C 27 | UAmass = 3924; %W/degree C 28 | T_on=0; 29 | T_off=0; 30 | 31 | Prated = Prated + Prated*0.05*(rand(1)-0.5); %W 32 | R1 = 1/(UA + UA*0.05*(rand(1)-0.5)); 33 | R2 = 1/(UAmass + UAmass*0.05*(rand(1)-0.5)); 34 | Ca = (Vair + Vair*0.05*(rand(1)-0.5))*DensityAir*Cv_air ; 35 | Cm = (V_furniture + V_furniture*0.05*(rand(1)-0.5))*C_funiture; 36 | 37 | A = [-(1/(R2*Ca)+1/(R1*Ca)) 1/(R2*Ca); 1/(R2*Cm) -1/(R2*Cm)]; 38 | B = [1/(R1*Ca) 1/Ca ;0 0]; 39 | C = [1 0; 0 1]; 40 | D = [0; 0]; 41 | 42 | status = zeros(1, Time); 43 | Q = zeros(1, Time); 44 | 45 | x0 = [19.5; 19.5]; 46 | if x0(1) > (Tset + TDB/2) 47 | status(1) = 0; 48 | elseif x0(1)<(Tset - TDB/2) 49 | status(1) = 1; 50 | end 51 | if status(1) == 1 52 | Q0(1) = Prated*Timestep; 53 | else 54 | Q0(1) = 0; 55 | end 56 | u0 = [Tout; Q0]; 57 | 58 | for i = 2:1:N_timestep 59 | DT = A*x0 + B*u0; 60 | x1 = x0+DT; 61 | if (x1(1)>(Tset + TDB/2)) && (status(i-1) == 1) 62 | status(i) = 0; 63 | elseif (x1(1)< (Tset - TDB/2)) && status(i-1) == 0 64 | status(i) = 1; 65 | else 66 | status(i) = status(i-1); 67 | end 68 | if status(i) == 1 69 | Q(i) = Prated*Timestep; 70 | T_on(i,1) = T_on(i-1,1)+1; 71 | T_off(i,1) = 0; 72 | else 73 | Q(i) = 0; 74 | T_off(i,1) = T_off(i-1,1)+1; 75 | T_on(i,1) = 0; 76 | end 77 | x0 = x1; 78 | u0 = [Tout; Q(i)]; 79 | Troom_record(i-1,1) = x1(1); 80 | Tmass_record(i-1,1) = x1(2); 81 | end 82 | Time = [0:Timestep:((N_timestep-2)*Timestep)]/60; 83 | 84 | % figure(1) 85 | % set(gcf,'DefaultAxesFontSize',14) %<--------set character size 86 | % set(gcf,'DefaultTextFontSize',14) 87 | % plot(Time,Troom_record,color(ii)) 88 | % % plot(Tmass_record,'b') 89 | % xlabel('Time (minute)') 90 | % ylabel('Temperature (^oC)') 91 | % hold on 92 | % title('Room Temperature Profiles') 93 | 94 | 95 | Toutcal(ii) = Tout; 96 | Toncal(ii) = max(T_on(1000:end)); 97 | Toffcal(ii) = max(T_off); 98 | 99 | % filename = ['Troom_record' num2str(ii)]; 100 | % save(filename, 'Troom_record','T_on', 'T_off','Toutcal') 101 | 102 | 103 | end 104 | 105 | % figure(1) 106 | % legend('T=-10^oC','T=-5^oC', 'T=0^oC','T=5^oC','T=10^oC') 107 | 108 | figure(2) 109 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 110 | set(gcf,'DefaultTextFontSize',14) 111 | subplot(1,2,1) 112 | hist(Toncal) 113 | xlabel('Time (second)') 114 | ylabel('number of occurrence') 115 | title('Histogram of \tau_{on}') 116 | 117 | subplot(1,2,2) 118 | hist(Toffcal) 119 | xlabel('Time (second)') 120 | ylabel('number of occurrence') 121 | title('Histogram of \tau_{off}') 122 | 123 | 124 | -------------------------------------------------------------------------------- /example35_randomizationMethod2.m: -------------------------------------------------------------------------------- 1 | 2 | % Code written by Dr. Ning Lu for DR book 3 | % Example 3.5 4 | % Code written 9/21/2017 5 | % Copyright North Carolina State University 6 | 7 | clear all 8 | close all 9 | 10 | Tout = [0]; 11 | color = ['rgbcm']; 12 | Time = 60; % in minutes 13 | Timestep = 1; % second 14 | N_timestep = Time*60/Timestep; 15 | Tset = 20; % degree C 16 | TDB = 1; %degree C 17 | Prated = 5020; %W 18 | Area = 228; % in m^2 19 | Vair = 228*5; % m^3 20 | DensityAir = 1.225; % kg/m^3 21 | Cp_air = 1005; % J/kg.k 22 | Cv_air = 780; % J/kg.k 23 | C_funiture = 1000; % J/kg.k 24 | V_furniture = 1000; %kg 25 | UA = 111; %W/degree C 26 | UAmass = 3924; %W/degree C 27 | 28 | T_on=0; 29 | T_off=0; 30 | 31 | Tmax = Tset + 0.5*TDB 32 | Tmin = Tset - 0.5*TDB 33 | 34 | 35 | for i = 1:5000 36 | trise(i) = 500+100*rand(1); 37 | tfall(i) = 640+80*rand(1); 38 | Q = Prated + Prated*0.05*(rand(1)-0.5); %W 39 | heater_mode = 1; 40 | [R, C] = cal_RCpara(Tmax, Tmin, Tout, trise(i), tfall(i), Q, heater_mode) 41 | R_record(i) = R; 42 | C_record(i) = C; 43 | Q_record(i) = Q; 44 | end 45 | 46 | figure(2) 47 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 48 | set(gcf,'DefaultTextFontSize',14) 49 | subplot(1,2,1) 50 | hist(R_record) 51 | xlabel('Thermal Resistance (^oC/W)') 52 | ylabel('number of occurrence') 53 | title('Histogram of R') 54 | 55 | subplot(1,2,2) 56 | 57 | hist(C_record) 58 | xlabel('Thermal Capacitance (J/^oC)') 59 | ylabel('number of occurrence') 60 | title('Histogram of C') 61 | 62 | 63 | 64 | figure(10) 65 | set(gcf,'DefaultAxesFontSize',14) %<--------set character size 66 | set(gcf,'DefaultTextFontSize',14) 67 | subplot(1,3,1) 68 | hist(trise) 69 | xlabel('Time (second)') 70 | ylabel('number of occurrence') 71 | title('Histogram of \tau_{on}') 72 | subplot(1,3,2) 73 | hist(tfall) 74 | xlabel('Time (second)') 75 | ylabel('number of occurrence') 76 | title('Histogram of \tau_{off}') 77 | subplot(1,3,3) 78 | hist(Q_record) 79 | xlabel('Heat Flow (W)') 80 | ylabel('number of occurrence') 81 | title('Histogram of Q') 82 | 83 | -------------------------------------------------------------------------------- /figureexample32_final_left.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/figureexample32_final_left.fig -------------------------------------------------------------------------------- /figureexample32_final_right.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/figureexample32_final_right.fig -------------------------------------------------------------------------------- /figureexample33_FigureC.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luningluning/DemandResponseBookChapter3/aea14556031459a9e5aed8df500ca79a70c51b1a/figureexample33_FigureC.fig -------------------------------------------------------------------------------- /moduel_spaceheater.m: -------------------------------------------------------------------------------- 1 | function [Troom, SPHstatus, SPHSWstatus] = moduel_spaceheater(t0, t1, Tset, Tout, Troom, SPHstatus, SPHSWstatus) 2 | 3 | %Set the household parameters 4 | load RCQSPH 5 | R=RSPH ; C=CSPH; Q=QSPH; 6 | 7 | THlim = Tset; % This is the setpoint low limit 8 | TLlim = Tset-2; % This is the setpoint high limit 9 | 10 | if SPHSWstatus == 1 11 | if SPHstatus == 0 12 | Y = Tout - (Tout -Troom)*exp(-(t1-t0)/(R*C)); 13 | elseif SPHstatus == 1 14 | Y = Tout+ Q*R - (Tout + Q*R - Troom)*exp(-(t1-t0)/(R*C)); 15 | else 16 | error('SPHstatus has to be either 1 or 0') 17 | end 18 | Troom = Y; 19 | if Troom < TLlim & SPHstatus ~= 1 20 | SPHstatus = 1; 21 | elseif Troom > THlim & SPHstatus ~= 0 22 | SPHstatus = 0; 23 | end 24 | elseif SPHSWstatus == 0 25 | SPHstatus = 0; 26 | Y = Tout - (Tout -Troom)*exp(-(t1-t0)/(R*C)); 27 | Troom = Y; 28 | else 29 | error('SPHSWstatus has to be either 1 or 0'); 30 | end 31 | clear R C Q 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /slowcooker.m: -------------------------------------------------------------------------------- 1 | % Code written by Dr. Ning Lu 2 | % Home Energy Management ToolBox version 2.0 3 | % Appliance model database - second by second data 4 | % copyright by North Carolina State University 5 | % Date: May 17 2016 6 | % This model is developed based on measurement data 7 | % Slow cooker load is resistive. 8 | % User can select the turn on time and turn off time 9 | 10 | 11 | function [P, Q] = slowcooker(t_s, t_e, mode) 12 | %time step is second by second 13 | P0 = 0; 14 | if t_e > t_s 15 | % initialization 16 | t_on = t_e-t_s; 17 | P = zeros(t_on,1); 18 | Q = zeros(t_on,1); 19 | 20 | if mode == 1 % 4~6 hour cooking 21 | P = 296*ones(t_on,1); 22 | elseif mode == 2 % 8~10 hour cooking 23 | n = ceil(t_on/15); 24 | P_cycle = [253 253 138 253 253 138 253 253 137 253 253 156 234 253 207 184]'; 25 | for i = 1:n 26 | P0 = [P0;P_cycle]; 27 | end 28 | P = P0(2:t_on+1); 29 | end 30 | 31 | else 32 | error('appliance turn on time should be greater than zero') 33 | end 34 | --------------------------------------------------------------------------------