├── Battery Modeling, Analysis and Simulation ├── Dayaramani_Deep_HW01.m ├── Dayaramani_Deep_HW01.pdf ├── HW1.pdf └── temp ├── Flight Path Optimization ├── CFTOC.m ├── Flight_Path_Optimization_Report.pdf ├── MPC_PP.m └── temp ├── Forecasting Residential Electricity Power Consumption ├── DAYARAMANI_DEEP_HW04.pdf ├── DAYARAMANI_DEEP_HW4.m ├── HW4.pdf └── temp ├── Optimal Economic Dispatch in Distribution Feeders with Renewables ├── DAYARAMANI_DEEP_HW03.pdf ├── DAYARAMANI_DEEP_HW3.m ├── HW3.pdf └── temp ├── Optimal PHEV Energy Management using Dynamic Programming ├── DAYARAMANI_DEEP_HW5.m ├── DAYARAMANI_DEEP_HW5.pdf ├── HW5.pdf └── temp ├── README.md └── State Estimation in Oil and Gas Well Drilling ├── DAYARAMANI_DEEP_HW02.m ├── DAYARAMANI_DEEP_HW02.pdf ├── HW2.pdf └── temp /Battery Modeling, Analysis and Simulation/Dayaramani_Deep_HW01.m: -------------------------------------------------------------------------------- 1 | %% CE 295 - Energy Systems and Control 2 | % HW 1 : Battery Modeling, Analysis, and Control 3 | % Oski Bear, SID 3032083601 4 | % Deep Dayaramani 5 | 6 | clear; close all; 7 | fs = 15; % Font Size for plots 8 | 9 | %% Part(a): Model Parameters 10 | 11 | %%% Enter model parameters here 12 | % ECM parameters 13 | Q = 3600; % [Coulombs] 14 | R1 = 0.05; % [Ohms] 15 | R2 =0.005 ; % [Ohms] 16 | C = 500; % [Farads] 17 | 18 | % OCV polynomial coefficients 19 | p_0 = 3.4707; 20 | p_1 = 1.6112; 21 | p_2 = -2.6287; 22 | p_3 = 1.7175; 23 | 24 | % Plot nonlinear OCV function 25 | z_vec = linspace(0,1,25); 26 | OCV = p_0 + p_1*z_vec + p_2*z_vec.^2 + p_3*z_vec.^3; 27 | 28 | figure(1); clf; 29 | plot(z_vec, OCV ) 30 | ylabel('OCV [volts]','FontSize',fs) 31 | xlabel('SOC, z [-]','FontSize',fs) 32 | set(gca,'FontSize',fs); 33 | 34 | % Assemble (A,B) state-space matrices 35 | A = [0 0; 0 -1/(C*R2)]; 36 | B = [1/Q; 1/C]; 37 | 38 | % Output states only (dummy variables, not used later) 39 | C_dummy = eye(2); 40 | D_dummy = 0; 41 | 42 | % Create state-space model 43 | ecm_sys = ss( A,B ,C_dummy , D_dummy ); 44 | 45 | %% Part(b): Simulate 46 | 47 | % Create time vector 48 | DeltaT = 1; % Time step size [sec] 49 | t = 0:DeltaT:(10*60); % Total Simulation time (min*sec/min) 50 | 51 | % Input current signals 52 | Current = zeros(size(t))'; 53 | Current(mod(t,40) < 20) = -5; % 20sec 5A pulse discharge 54 | 55 | % Initial conditions 56 | z0 = 0.5; % State-of-charge 57 | V_c0 = 0; % Capacitor voltage 58 | x0 = [z0; V_c0]; % Vectorize initial conditions 59 | 60 | % Simulate linear dynamics (Read documentation on lsim) 61 | [ ~,T ,x ] = lsim( ecm_sys, Current, t ,x0 ); 62 | 63 | % Parse out states 64 | z = x(:,1); 65 | V_c = x(:,2); 66 | 67 | % Compute nonlinear output function [enter output function ] 68 | V_nl = V_c + Current*R1 + (p_0 + p_1*z + p_2*z.^2 + p_3*z.^3); ; 69 | 70 | %%% Compute linearized output function 71 | % Linearization Points 72 | zeq =0.5; % State-of-charge 73 | V_ceq =0 ; % Capacitor voltage 74 | Ieq =0 ; % Current 75 | 76 | V_lin = (p_0 + 0.5*p_1 + 0.25*p_2 + 0.125*p_3)+ V_c + Current*R1 + (p_1 + p_2 + 0.75*p_3)*(z-zeq); ; % Linearized version of output function 77 | 78 | %% Part(b): Plot results 79 | 80 | figure(2); clf; 81 | 82 | % Current 83 | subplot(3,1,1); 84 | plot(T, Current ) 85 | ylabel('Current (amps)', 'FontSize', fs) 86 | 87 | % State-of-charge 88 | subplot(3,1,2); 89 | plot(T,z ) 90 | ylabel('SOC, z[-]', 'FontSize', fs ) 91 | 92 | % Nonlinear and Linear Voltage 93 | subplot(3,1,3); 94 | plot(T,V_nl, 'b', T, V_lin, 'k' ) 95 | ylabel('Voltages (volts)','FontSize',fs ) 96 | legend( 'V nonlinear', 'V linear') 97 | xlabel('Time[min*sec/min]' ); 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /Battery Modeling, Analysis and Simulation/Dayaramani_Deep_HW01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Battery Modeling, Analysis and Simulation/Dayaramani_Deep_HW01.pdf -------------------------------------------------------------------------------- /Battery Modeling, Analysis and Simulation/HW1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Battery Modeling, Analysis and Simulation/HW1.pdf -------------------------------------------------------------------------------- /Battery Modeling, Analysis and Simulation/temp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Flight Path Optimization/CFTOC.m: -------------------------------------------------------------------------------- 1 | function [xopt,uopt,flag] = CFTOC(dyn,Q,R,x0,xf,xlb,xub,ulb,uub,dT,N) 2 | % Using ipopt to solve a constrainted finite time optimal control problem 3 | % for a non-linear system using discretized non-linear dynamic model 4 | 5 | %% Initialize variables and cost 6 | disp('initialize...') 7 | %states:[x ;y ;v ;m ;theta] 8 | %input:[T ;yaw] 9 | x = sdpvar(5,N+1); 10 | u = sdpvar(2,N); 11 | %cost 12 | cost=[]; 13 | 14 | %% Constraints & Cost function 15 | %initial states constraint: 16 | constraint = [x(:,1)==x0,x(1:3,end)==xf(1:3)]; %initial/final state 17 | 18 | for k=1:N 19 | %states dynamics 20 | constraint = constraint + [x(:,k+1)==dyn(x(:,k),u(:,k))]; 21 | 22 | %states constraints 23 | constraint = constraint + [xlb <= x(:,k)<= xub]; 24 | constraint = constraint + [-0.6*dT <= (x(3,k+1)-x(3,k))<= 0.6*dT]; 25 | 26 | %input constraints 27 | constraint = constraint + [ulb <= u(:,k)<= uub]; 28 | %cost 29 | cost=cost+(x(:,k)-xf)'*Q*(x(:,k)-xf)+u(:,k)'*R*u(:,k); 30 | end 31 | 32 | %% slove optimization problem 33 | disp('optimizing...') 34 | %solver settings 35 | options=sdpsettings('solver','ipopt','verbose',0,'showprogress',1); 36 | %options.ipopt.check_derivatives_for_naninf='yes'; 37 | result=optimize(constraint,cost,options); 38 | 39 | %check feasibility 40 | flag=result.problem; 41 | disp(yalmiperror(flag)); 42 | if flag==0 43 | %solution found 44 | xopt=double(x); uopt=double(u); 45 | else 46 | %no feasible solution 47 | xopt=[]; uopt=[]; 48 | end 49 | end 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Flight Path Optimization/Flight_Path_Optimization_Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Flight Path Optimization/Flight_Path_Optimization_Report.pdf -------------------------------------------------------------------------------- /Flight Path Optimization/MPC_PP.m: -------------------------------------------------------------------------------- 1 | clear; 2 | clc; 3 | %% Load Data 4 | tracks=csvread('VRD211.csv',1,1); %load data 5 | xd=[tracks(:,7)';tracks(:,8)';tracks(:,4)'*0.514444]; %store [lon;lat;speed] 6 | tf=size(xd,2)+1; %define time 7 | xd=[xd;zeros(2,tf-1)]; %add new column for 8 | 9 | %% Plot Actual data 10 | figure(1); 11 | plot(xd(1,:)/1000,xd(2,:)/1000,'-o','DisplayName','Actual trajectory'); 12 | xlabel('x [km]'); ylabel('y [km]') 13 | hold on 14 | 15 | figure(2) 16 | subplot(5,1,1) 17 | plot(0:size(xd,2)-1,xd(1,:)/1000,'--','DisplayName','Actual') 18 | hold on 19 | xlabel('time [min]'); ylabel('x [km]'); 20 | grid on 21 | 22 | subplot(5,1,2) 23 | plot(0:size(xd,2)-1,xd(2,:)/1000,'--','DisplayName','Actual') 24 | xlabel('time [min]'); ylabel('y [km]'); 25 | hold on 26 | grid on 27 | 28 | subplot(5,1,3) 29 | plot(0:size(xd,2)-1,xd(3,:),'--','DisplayName','Actual') 30 | xlabel('time [min]'); ylabel('v [m/s]'); 31 | hold on 32 | grid on 33 | 34 | %% Drag Force Parameters: 35 | Cd= 0.04; %Drag force coefficient [-] 36 | rho= 0.38; %air density [kg/m3] 37 | eta= 0.01667*10^-3; %Thrust-specific fuel consumption [kg/(N*s)] 38 | S= 122.6; %[m2]...Aircraft wing area 39 | 40 | %% Star & End location coordinates 41 | %ORD=[272.093, 41.9742]; %start coord 42 | %SFO=[237.617, 37.6211]; %final coord 43 | 44 | %coordinate refer to ORD [m] 45 | %this is a rough conversion from the original WGS84 coordiantes 46 | ORD=[0, 0]; %start coord 47 | SFO=[-2895204.87, -491111.25]; %final coord 48 | 49 | %% Wind approximated funciton coefficient 50 | a1=5.404039761756626e-12; 51 | a2=-7.525095226657606e-09; 52 | a3=-1.0097962636800737e-05; 53 | a4=0.0018023200165759225; 54 | a5=0.30541919780328985; 55 | a6=1.0706181973188272e-12; 56 | a7=8.130657646668963e-09; 57 | a8=1.9566392595939457e-05; 58 | a9=0.013598711210671936; 59 | a10=0.3054111118706383; 60 | a11=-4.4925592769414514e-13; 61 | a12=1.3721355294688644e-12; 62 | a13=-1.9705270758495876e-12; 63 | 64 | b1=6.505580005194991e-12; 65 | b2=-2.3582120860024345e-10; 66 | b3=-2.0098332293991807e-06; 67 | b4=-8.207601052061964e-06; 68 | b5=6.216049027788961; 69 | b6=-2.184365129705414e-12; 70 | b7=-1.574583855440115e-08; 71 | b8=-1.7909052661551478e-05; 72 | b9=0.0358673394470828; 73 | b10=6.216049027919514; 74 | 75 | %% Simulation Process 76 | % Testing different size of time step to see impact on the result by 77 | % reducing total time horizon by a samll number, defined by delta, in each 78 | % loop. One can also choose to change the number of time step (N). 79 | 80 | % Also, Total time horizon (T) can be changed to test a wider time range. 81 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 82 | %% Choose Time parameters 83 | N=30; %Total number of time step 84 | T=4; %Total time horizon [h] 85 | delta=0.1; 86 | %% Cost Matrics 87 | Q=diag([10^3,10^3,0,0,0]); %state cost 88 | R=diag([0,0]); %input cost 89 | 90 | %% Initial/Final States 91 | %x=[x; y; v; m; theta] u=[T; yaw]; 92 | x0=[ORD'; 100; 78000; deg2rad(127)]; %initial states 93 | xf=[SFO'; 0; 0; 0]; %final states 94 | 95 | %% State/Input Constraints 96 | xlb=[-inf; -inf; 0; 37200; -inf]; %states lower bound 97 | xub=[inf; inf; 250; 78000; inf]; %states upper bound 98 | ulb=[0; -2.5*pi/180]; %input lower bound 99 | uub=[120000*2; 2.5*pi/180]; %input upper bound 100 | 101 | %% Main 102 | for ii=0:4 103 | %% Simulation Parameters 104 | dT=(T-delta*ii)*3600/N; %[s] time step 105 | %% Non-linear discrete aircraft dynamic model 106 | %Aircraft dynamics 107 | %x=[x;y;v;m;theta] u=[T;yaw]; 108 | 109 | dyn=@(x,u) [x(1)+dT*(x(3)*cos(x(5))+a1*(x(2)^4/1000^4)+... 110 | a2*(x(2)^3/1000^3)+a3*(x(2)^2/1000^2)+a4*(x(2)/1000)+a5+... 111 | a6*(x(1)^4/1000^4)+a7*(x(1)^3/1000^3)+a8*(x(1)^2/1000^2)+... 112 | a9*(x(1)/1000)+a10+a11*(x(1)^3/1000^3)*(x(2)/1000)+... 113 | a12*(x(1)^2/1000^2)*(x(2)^2/1000^2)+a13*(x(1)/1000)*(x(2)^3/1000^3)); 114 | 115 | x(2)+dT*(x(3)*sin(x(5))+b1*(x(2)^4/1000^4)+... 116 | b2*(x(2)^3/1000^3)+b3*(x(2)^2/1000^2)+b4*(x(2)/1000)+... 117 | b5+b6*(x(1)^4/1000^4)+b7*(x(1)^3/1000^3)+... 118 | b8*(x(1)^2/1000^2)+b9*(x(1)/1000)+b10); 119 | 120 | x(3)+dT*(2*u(1)-Cd*rho*S*(x(3)^2))/(2*x(4)); 121 | 122 | x(4)-dT*eta*u(1); 123 | 124 | x(5)+dT*u(2)]; 125 | 126 | %% Path Planning 127 | tic 128 | [xopt,uopt,flag]=CFTOC(dyn,Q,R,x0,xf,xlb,xub,ulb,uub,dT,N); 129 | toc 130 | 131 | %% Visualization 132 | %trajectory 133 | figure(1); hold on 134 | title(['Flight Trajectory N=' num2str(N)]) 135 | plot(xopt(1,:)/1000,xopt(2,:)/1000,'-*','DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]); 136 | hold on 137 | legend show 138 | grid on 139 | 140 | %states-longtitude 141 | figure(2); hold on 142 | subplot(5,1,1) 143 | plot(0:dT/60:N*dT/60,xopt(1,:)/1000,'DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]); 144 | hold on 145 | xlabel('time [min]'); ylabel('x [km]'); 146 | legend show 147 | grid on 148 | 149 | %states-latitude 150 | subplot(5,1,2) 151 | plot(0:dT/60:N*dT/60,xopt(2,:)/1000,'DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]); 152 | xlabel('time [min]'); ylabel('y [km]'); 153 | legend show 154 | hold on 155 | grid on 156 | 157 | %states-speed 158 | subplot(5,1,3) 159 | plot(0:dT/60:N*dT/60,xopt(3,:),'DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]); 160 | xlabel('time [min]'); ylabel('v [m/s]'); 161 | legend show 162 | hold on 163 | grid on 164 | 165 | %states-mass 166 | subplot(5,1,4) 167 | plot(0:dT/60:N*dT/60,xopt(4,:)/1000,'DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]) 168 | xlabel('time [min]'); ylabel('m [ton]'); 169 | legend show 170 | hold on 171 | grid on 172 | 173 | %states-"heading angle" 174 | subplot(5,1,5) 175 | plot(0:dT/60:N*dT/60,rad2deg(xopt(5,:)),'DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]) 176 | xlabel('time [min]'); ylabel('theta [deg]'); 177 | legend show 178 | hold on 179 | grid on 180 | 181 | %input 182 | figure(3); 183 | subplot(2,1,1) 184 | plot(0:dT/60:N*dT/60-1,uopt(1,:)/1000,'DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]) 185 | xlabel('time [min]'); ylabel('T [kN]'); 186 | legend show 187 | hold on; 188 | grid on 189 | 190 | subplot(2,1,2) 191 | plot(0:dT/60:N*dT/60-1,uopt(2,:),'DisplayName',['Optimized trajectory, T=' num2str(T-delta*ii)]) 192 | xlabel('time [min]'); ylabel('turning rate [rad/s]'); 193 | legend show 194 | hold on 195 | grid on 196 | 197 | end 198 | 199 | 200 | 201 | 202 | 203 | -------------------------------------------------------------------------------- /Flight Path Optimization/temp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Forecasting Residential Electricity Power Consumption/DAYARAMANI_DEEP_HW04.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Forecasting Residential Electricity Power Consumption/DAYARAMANI_DEEP_HW04.pdf -------------------------------------------------------------------------------- /Forecasting Residential Electricity Power Consumption/DAYARAMANI_DEEP_HW4.m: -------------------------------------------------------------------------------- 1 | %% Prob 1 2 | Training_Data_1 = readtable('HW4_Train_Data.csv'); 3 | Training_Data_2 = table2array(Training_Data_1(:,4:end)); 4 | 5 | % part a) 6 | average_kWh = sum(Training_Data_2) / size(Training_Data_2, 1); 7 | bdx_index = 1:size(Training_Data_2, 2); 8 | stand_kWh = std(Training_Data_2); 9 | error_kWh = stand_kWh/sqrt(size(Training_Data_2, 1)); 10 | 11 | figure(); 12 | hold on 13 | bar(bdx_index, average_kWh); 14 | xlim([0.5 23.5]); 15 | xticks(bdx_index); 16 | 17 | errorbar(bdx_index, average_kWh, stand_kWh, 'color', 'r', 'LineStyle', 'none'); 18 | xlabel('Building Number', 'fontsize', 13 ); 19 | ylabel('Average Energy Consumption [kWh]', 'fontsize', 13); 20 | legend('Average Energy Consumption [kWh]', 'error [kWh]'); 21 | %% 22 | % part b) 23 | % building 6 has an abnormally large variance 24 | Training_Data_3 = []; 25 | neg_bldg = []; 26 | for i = 1:size(Training_Data_2, 2) 27 | if all(Training_Data_2(:, i)>=0) 28 | Training_Data_3 = [Training_Data_3 Training_Data_2(:, i)]; 29 | else 30 | neg_bldg = [neg_bldg i]; 31 | end 32 | end 33 | %% 34 | % part c) 35 | WoY = weeknum(datestr(table2array(Training_Data_1(:,3)))); %datetime. 36 | DoW = weekday(datestr(table2array(Training_Data_1(:,3)))); % day of week 37 | HoD = hour(datestr(table2array(Training_Data_1(:,3)))); % hour of day 38 | Training_Data_4D = []; 39 | Training_Data_Temp = [WoY DoW HoD Training_Data_3]; 40 | %% 41 | Training_Data_3_normalized = Training_Data_3./max(Training_Data_3); 42 | 43 | % Re-organize energy data 44 | % building 1 45 | for i = 1:size(Training_Data_3, 2) 46 | for j = 1:size(WoY, 1) 47 | Training_Data_4D(i, WoY(j), DoW(j), HoD(j)+1)... 48 | = Training_Data_3_normalized(j, i); 49 | end 50 | end 51 | %% 52 | % plot figures of kWh 53 | days = [string('Sunday'),string('Monday'),string('Tuesday'),string('Wednesday'),string('Thursday'),string('Friday'),string('Saturday')]; 54 | avg_days = []; 55 | for k = 1:length(days) 56 | figure(); 57 | hold on 58 | title(days(k)); 59 | xlabel('Time of Day'); 60 | ylabel('Normalized Hourly Energy'); 61 | xlim([0 23]); 62 | TempMean = []; 63 | for i = 1:size(Training_Data_3, 2) 64 | for j = 1:max(WoY) 65 | plot(0:23, squeeze(Training_Data_4D(i,j,k,:))); 66 | TempMean = [TempMean squeeze(Training_Data_4D(i,j,k,:))]; 67 | end 68 | end 69 | avg_days = [avg_days, mean(TempMean')]; 70 | plot(0:23, mean(TempMean'), '-k', 'linewidth', 4); 71 | end 72 | 73 | %% 74 | 75 | %question 2 76 | TestData1 = readtable('HW4_Test_Data.xlsx'); 77 | test_days = []; 78 | for i= 1:24:168 79 | test_days = [test_days, TestData1.TestBldg(i:23+i)]; 80 | end 81 | avg_days_2 = []; 82 | for i= 1:24:168 83 | avg_days_2 = [avg_days_2, avg_days(i:i+23)']; 84 | end 85 | days = [string('Sunday'),string('Monday'),string('Tuesday'),string('Wednesday'),string('Thursday'),string('Friday'),string('Saturday')]; 86 | for k = 1:length(days) 87 | figure(); 88 | hold on 89 | title(days(k)); 90 | xlabel('Time of Day'); 91 | ylabel('Normalized Hourly Energy'); 92 | xlim([0 23]); 93 | plot(0:23, avg_days_2(:,k), '-b', 'linewidth', 4); 94 | plot(0:23, test_days(:,k), '-k', 'linewidth', 4); 95 | legend('Predicted data', 'Test data'); 96 | end 97 | %% 98 | %question 2b 99 | list_MAE = []; 100 | for day =1:7 101 | P_avxx = 0; 102 | for hour= 1:24 103 | P_avxx = P_avxx + abs(test_days(hour,day) - avg_days_2(hour,day)); 104 | end 105 | list_MAE = [list_MAE,(P_avxx/24)]; 106 | end 107 | 108 | 109 | for i = 1: length(list_MAE) 110 | fprintf(1,'The MAE of (%1.3s) + is about %1.3f',days(i),list_MAE(i)); 111 | disp(' ') 112 | end 113 | 114 | 115 | %% 116 | %question 3, part a, b 117 | P_arx =[]; 118 | for i = 1:22 119 | P_arx = [P_arx; Training_Data_3_normalized(2:end,i)]; 120 | end 121 | P_avg = []; 122 | for p = 1:22 123 | P_avg = [ P_avg; (avg_days(2:end))']; 124 | for i = 1:50 125 | P_avg = [P_avg; avg_days']; 126 | 127 | end 128 | end 129 | L = 3; 130 | Y = P_arx-P_avg; 131 | Phi = []; 132 | for i = 1:22 133 | d = rotate(Training_Data_3(:,i),L); 134 | Phi = [Phi; d]; 135 | end 136 | %part c 137 | alpha_star = (inv(Phi'*Phi)*Phi')*Y; 138 | fprintf(1,'alpha_1 is about %1.3f',alpha_star(1)); 139 | disp(' '); 140 | fprintf(1,'alpha_2 is about %1.3f',alpha_star(2)); 141 | disp(' '); 142 | fprintf(1,'alpha_3 is about %1.3f',alpha_star(3)); 143 | %part d 144 | Phi_test = []; 145 | d = rotate(TestData1.TestBldg(:),L); 146 | Phi_test = [Phi_test; d]; 147 | 148 | Y_test= Phi_test*alpha_star + (avg_days(2:end))'; 149 | test_days_temp = [TestData1.TestBldg(1,:); Y_test]; 150 | test_days_avx = []; 151 | for i= 1:24:168 152 | test_days_avx = [test_days_avx, test_days_temp(i:23+i)]; 153 | end 154 | days = [string('Sunday'),string('Monday'),string('Tuesday'),string('Wednesday'),string('Thursday'),string('Friday'),string('Saturday')]; 155 | for k = 1:length(days) 156 | figure(); 157 | hold on 158 | title(days(k)); 159 | xlabel('Time of Day'); 160 | ylabel('Normalized Hourly Energy'); 161 | xlim([0 23]); 162 | plot(0:23, avg_days_2(:,k), '-b', 'linewidth', 4); 163 | plot(0:23, test_days(:,k), '-k', 'linewidth', 4); 164 | plot(0:23, test_days_avx(:,k), '-o', 'linewidth', 4); 165 | legend('Predicted data', 'Test data', 'Predicted AVX data'); 166 | end 167 | list_MAE = []; 168 | for day =1:7 169 | P_avxx = 0; 170 | for hour= 1:24 171 | P_avxx = P_avxx + abs(test_days(hour,day) - test_days_avx(hour,day)); 172 | end 173 | list_MAE = [list_MAE,(P_avxx/24)]; 174 | end 175 | 176 | 177 | for i = 1: length(list_MAE) 178 | fprintf(1,'The MAE of (%1.3s) + is about %1.3f',days(i),list_MAE(i)); 179 | disp(' ') 180 | end 181 | 182 | 183 | 184 | %% question 4 185 | 186 | 187 | gamma = 10^-5; %choose small enough gamma 188 | w=[0;0;0]; %initialize 189 | 190 | %Train Model to Obtain w's 191 | for t = 1:3 %perform 3 time steps of gradient descent 192 | temp = 0; 193 | for i=1:22 194 | 195 | P_avxx = Training_Data_3_normalized(:,i); 196 | P_hat = []; 197 | for k = 4:8568 198 | 199 | P_hat(k-3) = avg_days_2(HoD(k)+1,DoW(k))'; 200 | 201 | y_i=P_avxx(k) - P_hat(k-3); 202 | x_i=[P_avxx(k-1), P_avxx(k-2), P_avxx(k-3)]'; 203 | dJdW = (y_i - tanh((w')*x_i))*(1-(tanh((w')*x_i))^2)*x_i; 204 | temp=temp+dJdW; 205 | end 206 | end 207 | w=w+gamma*temp; 208 | end 209 | 210 | fprintf(1,'w_1 is about %1.3f',w(1)); 211 | disp(' '); 212 | fprintf(1,'w_2 is about %1.3f',w(2)); 213 | disp(' '); 214 | fprintf(1,'w_3 is about %1.3f',w(3)); 215 | %Part d 216 | NN = [avg_days_2(1,1)'; avg_days_2(1,2)'; avg_days_2(1,3)']; 217 | for k=4:168 218 | x = [NN(k-1); NN(k-2); NN(k-3)]; 219 | PNN_in = tanh((w)'*x) + avg_days(k)'; 220 | NN = [NN; PNN_in]; 221 | end 222 | NN_plot = reshape(NN,[24,7]); 223 | 224 | 225 | days = [string('Sunday'),string('Monday'),string('Tuesday'),string('Wednesday'),string('Thursday'),string('Friday'),string('Saturday')]; 226 | for k = 1:length(days) 227 | figure(); 228 | hold on 229 | title(days(k)); 230 | xlabel('Time of Day'); 231 | ylabel('Normalized Hourly Energy'); 232 | xlim([0 23]); 233 | plot(0:23, avg_days_2(:,k), '-b', 'linewidth', 4); 234 | plot(0:23, test_days(:,k), '-k', 'linewidth', 4); 235 | plot(0:23, test_days_avx(:,k), '-o', 'linewidth', 4); 236 | plot(0:23, NN_plot(:,k), '-o', 'linewidth', 4); 237 | legend('Predicted data', 'Test data', 'Predicted AVX data','NN_plot predicted'); 238 | end 239 | list_MAE = []; 240 | for day =1:7 241 | P_avxx = 0; 242 | for hour= 1:24 243 | P_avxx = P_avxx + abs(test_days(hour,day) - test_days_avx(hour,day)); 244 | end 245 | list_MAE = [list_MAE,(P_avxx/24)]; 246 | end 247 | 248 | 249 | 250 | for i = 1: length(list_MAE) 251 | fprintf(1,'The MAE of (%1.3s) + is about %1.3f',days(i),list_MAE(i)); 252 | disp(' ') 253 | end 254 | -------------------------------------------------------------------------------- /Forecasting Residential Electricity Power Consumption/HW4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Forecasting Residential Electricity Power Consumption/HW4.pdf -------------------------------------------------------------------------------- /Forecasting Residential Electricity Power Consumption/temp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Optimal Economic Dispatch in Distribution Feeders with Renewables/DAYARAMANI_DEEP_HW03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Optimal Economic Dispatch in Distribution Feeders with Renewables/DAYARAMANI_DEEP_HW03.pdf -------------------------------------------------------------------------------- /Optimal Economic Dispatch in Distribution Feeders with Renewables/DAYARAMANI_DEEP_HW3.m: -------------------------------------------------------------------------------- 1 | %% CE 295 - Energy Systems and Control 2 | % HW 3 : Optimal Economic Dispatch in Distribution Feeders with Renewables 3 | % Deep Dayaramani, SID 3032083601 4 | % Prof. Moura 5 | 6 | 7 | clear; close all; 8 | fs = 15; % Font Size for plots 9 | 10 | %% 13 Node IEEE Test Feeder Parameters 11 | 12 | %%% Node (aka Bus) Data 13 | % l_j^P: Active power consumption [MW] 14 | l_P = [ 0 15 | 0.2 16 | 0 17 | 0.4 18 | 0.17 19 | 0.23 20 | 1.155 21 | 0 22 | 0.17 23 | 0.843 24 | 0 25 | 0.17 26 | 0.128]; 27 | 28 | % l_j^Q: Reactive power consumption [MVAr] 29 | l_Q = [ 0 30 | 0.1160 31 | 0 32 | 0.2900 33 | 0.1250 34 | 0.1320 35 | 0.6600 36 | 0 37 | 0.1510 38 | 0.4620 39 | 0 40 | 0.0800 41 | 0.0860 ]; 42 | 43 | % l_j^S: Apparent power consumption [MVA] 44 | l_S = sqrt(l_P.^2 + l_Q.^2); 45 | 46 | % s_j,max: Maximal generating power [MW] 47 | s_max = [5 48 | 0 49 | 0 50 | 3 51 | 0 52 | 0 53 | 0 54 | 0 55 | 0 56 | 3 57 | 0 58 | 0 59 | 0 ]; 60 | 61 | % c_j: Marginal generation cost [USD/MW] 62 | c = [100 63 | 0 64 | 0 65 | 150 66 | 0 67 | 0 68 | 0 69 | 0 70 | 0 71 | 50 72 | 0 73 | 0 74 | 0 ]; 75 | 76 | % V_min, V_max: Minimum and maximum nodal voltages [V] 77 | %v_min = 0.95; 78 | %v_max = 1.05; 79 | v_min = 0.98; 80 | v_max = 1.02; 81 | %%% Edge (aka Line) Data 82 | % r_ij: Resistance [p.u.] 83 | r = [0 0.007547918 0 0 0 0 0 0 0 0 0 0 0 84 | 0 0 0.0041 0 0.007239685 0 0.007547918 0 0 0 0 0 0 85 | 0 0 0 0 0 0 0 0 0 0 0 0 0 86 | 0 0 0 0 0 0 0 0 0 0 0 0 0 87 | 0 0 0 0 0 0.004343811 0 0 0 0 0 0 0 88 | 0 0 0 0 0 0 0 0 0 0 0 0 0 89 | 0 0 0 0 0 0 0 0.003773959 0 0 0.004322245 0 0 90 | 0 0 0 0 0 0 0 0 0 0 0 0 0 91 | 0 0 0 0 0 0 0 0 0 0.00434686 0 0 0 92 | 0 0 0 0 0 0 0 0 0 0 0 0 0 93 | 0 0 0 0 0 0 0 0 0 0 0 0.004343157 0.01169764 94 | 0 0 0 0 0 0 0 0 0 0 0 0 0 95 | 0 0 0 0 0 0 0 0 0 0 0 0 0 ]; 96 | 97 | % x_ij: Reactance [p.u.] 98 | x = [0 0.022173236 0 0 0 0 0 0 0 0 0 0 0 99 | 0 0 0.0064 0 0.007336076 0 0.022173236 0 0 0 0 0 0 100 | 0 0 0 0 0 0 0 0 0 0 0 0 0 101 | 0 0 0 0 0 0 0 0 0 0 0 0 0 102 | 0 0 0 0 0 0.004401645 0 0 0 0 0 0 0 103 | 0 0 0 0 0 0 0 0 0 0 0 0 0 104 | 0 0 0 0 0 0 0 0.011086618 0 0 0.004433667 0 0 105 | 0 0 0 0 0 0 0 0 0 0 0 0 0 106 | 0 0 0 0 0 0 0 0 0 0.002430473 0 0 0 107 | 0 0 0 0 0 0 0 0 0 0 0 0 0 108 | 0 0 0 0 0 0 0 0 0 0 0 0.004402952 0.004490848 109 | 0 0 0 0 0 0 0 0 0 0 0 0 0 110 | 0 0 0 0 0 0 0 0 0 0 0 0 0 ]; 111 | 112 | % I_max_ij: Maximal line current [p.u.] 113 | I_max = [0 3.0441 0 0 0 0 0 0 0 0 0 0 0 114 | 0 0 1.4178 0 0.9591 0 3.0441 0 0 0 0 0 0 115 | 0 0 0 3.1275 0 0 0 0 0 0 0 0 0 116 | 0 0 0 0 0 0 0 0 0 0 0 0 0 117 | 0 0 0 0 0 0.9591 0 0 0 0 0 0 0 118 | 0 0 0 0 0 0 0 0 0 0 0 0 0 119 | 0 0 0 0 0 0 0 3.0441 3.1275 0 0.9591 0 0 120 | 0 0 0 0 0 0 0 0 0 0 0 0 0 121 | 0 0 0 0 0 0 0 0 0 1.37193 0 0 0 122 | 0 0 0 0 0 0 0 0 0 0 0 0 0 123 | 0 0 0 0 0 0 0 0 0 0 0 0.9591 1.2927 124 | 0 0 0 0 0 0 0 0 0 0 0 0 0 125 | 0 0 0 0 0 0 0 0 0 0 0 0 0 ]; 126 | 127 | % A_ij: Adjacency matrix; A_ij = 1 if i is parent of j 128 | A = [0 1 0 0 0 0 0 0 0 0 0 0 0 129 | 0 0 1 0 1 0 1 0 0 0 0 0 0 130 | 0 0 0 1 0 0 0 0 0 0 0 0 0 131 | 0 0 0 0 0 0 0 0 0 0 0 0 0 132 | 0 0 0 0 0 1 0 0 0 0 0 0 0 133 | 0 0 0 0 0 0 0 0 0 0 0 0 0 134 | 0 0 0 0 0 0 0 1 1 0 1 0 0 135 | 0 0 0 0 0 0 0 0 0 0 0 0 0 136 | 0 0 0 0 0 0 0 0 0 1 0 0 0 137 | 0 0 0 0 0 0 0 0 0 0 0 0 0 138 | 0 0 0 0 0 0 0 0 0 0 0 1 1 139 | 0 0 0 0 0 0 0 0 0 0 0 0 0 140 | 0 0 0 0 0 0 0 0 0 0 0 0 0 ]; 141 | 142 | 143 | %%% Set Data (add +1 everywhere for Matlab indexing) 144 | % \rho(j): Parent node of node j 145 | rho = [ 1 146 | 1 147 | 2 148 | 3 149 | 2 150 | 5 151 | 2 152 | 7 153 | 7 154 | 9 155 | 7 156 | 11 157 | 11 ]; 158 | 159 | 160 | %% Problem 1 161 | 162 | % Plot active and reactive power consumption 163 | figure(1); 164 | bar([0:12;0:12]',[l_P,l_Q]); 165 | legend ('Active power consumption [MW]','Reactive power consumption [MVAr]') 166 | xlabel('node') 167 | ylabel('power consumption [MW]/[MVAr]') 168 | 169 | %% Problem 2 170 | 171 | % Assumptions: 172 | % - Disregard the entire network diagram 173 | % - Balance supply and demand, without any network considerations 174 | % - Goal is to minimize generation costs, given by c^T s 175 | 176 | % Solve with CVX 177 | cvx_begin 178 | variables q(13) s(13) p(13) % declare your optimization variables here 179 | minimize(c'*s) % objective function here 180 | subject to % constraints 181 | % Balance power generation with power consumption 182 | sum(l_S) == sum(s); 183 | % Loop over each node 184 | for jj = 1:13 185 | % Non-negative power generation 186 | p(jj)>=0; 187 | q(jj)>=0; 188 | % Compute apparent power from active & reactive power 189 | s(jj)>= norm([p(jj),q(jj)],2); 190 | end 191 | % Apparent Power Limits 192 | s<=s_max; 193 | cvx_end 194 | 195 | % Output Results 196 | fprintf(1,'------------------- PROBLEM 2 --------------------\n'); 197 | fprintf(1,'--------------------------------------------------\n'); 198 | fprintf(1,'Minimum Generating Cost : %4.2f USD\n',cvx_optval); 199 | fprintf(1,'\n'); 200 | fprintf(1,'Node 0 [Grid] Gen Power : p_0 = %1.3f MW | q_0 = %1.3f MW | s_0 = %1.3f MW\n',p(1),q(1),s(1)); 201 | fprintf(1,'Node 3 [Gas] Gen Power : p_3 = %1.3f MW | q_3 = %1.3f MW | s_3 = %1.3f MW\n',p(4),q(4),s(4)); 202 | fprintf(1,'Node 9 [Solar] Gen Power : p_9 = %1.3f MW | q_9 = %1.3f MW | s_9 = %1.3f MW\n',p(10),q(10),s(10)); 203 | fprintf(1,'\n'); 204 | fprintf(1,'Total active power : %1.3f MW consumed | %1.3f MW generated\n',sum(l_P),sum(p)); 205 | fprintf(1,'Total reactive power : %1.3f MVAr consumed | %1.3f MVAr generated\n',sum(l_Q),sum(q)); 206 | fprintf(1,'Total apparent power : %1.3f MVA consumed | %1.3f MVA generated\n',sum(l_S),sum(s)); 207 | 208 | 209 | 210 | %% Problem 3 211 | 212 | % Assumptions: 213 | % - Disregard L_ij, the squared magnitude of complex line current 214 | % - Disregard nodal voltage equation 215 | % - Disregard nodal voltage limits 216 | % - Disregard maximum line current 217 | % - Goal is to minimize generation costs, given by c^T s 218 | 219 | % Solve with CVX 220 | cvx_begin 221 | variables p(13) q(13) s(13) P(13,13) Q(13,13) 222 | dual variable mu_s 223 | minimize( c'*s ) 224 | subject to 225 | 226 | % Boundary condition for power line flows 227 | P( 1 , 1 ) == 0; 228 | Q( 1 , 1 ) == 0; 229 | 230 | % Loop over each node 231 | for jj = 1:13 232 | p(jj)>=0; 233 | q(jj)>=0; 234 | % Parent node, i = \rho(j) 235 | i=rho(jj); 236 | % Line Power Flows 237 | P(i,jj) == (l_P(jj)-p(jj))+ A(jj,:)*P(jj,:)'; 238 | Q(i,jj) == (l_Q(jj)-q(jj))+ A(jj,:)*Q(jj,:)'; 239 | % Compute apparent power from active & reactive power 240 | s(jj) >= norm([p(jj),q(jj)],2); 241 | 242 | 243 | end 244 | 245 | % Apparent Power Limits 246 | mu_s:s <= s_max 247 | 248 | cvx_end 249 | 250 | % Output Results 251 | fprintf(1,'------------------- PROBLEM 3 --------------------\n'); 252 | fprintf(1,'--------------------------------------------------\n'); 253 | fprintf(1,'Minimum Generating Cost : %4.2f USD\n',cvx_optval); 254 | fprintf(1,'\n'); 255 | fprintf(1,'Node 0 [Grid] Gen Power : p_0 = %1.3f MW | q_0 = %1.3f MW | s_0 = %1.3f MW || mu_s0 = %3.0f USD/MW\n',p(1),q(1),s(1),mu_s(1)); 256 | fprintf(1,'Node 3 [Gas] Gen Power : p_3 = %1.3f MW | q_3 = %1.3f MW | s_3 = %1.3f MW || mu_s3 = %3.0f USD/MW\n',p(4),q(4),s(4),mu_s(4)); 257 | fprintf(1,'Node 9 [Solar] Gen Power : p_9 = %1.3f MW | q_9 = %1.3f MW | s_9 = %1.3f MW || mu_s9 = %3.0f USD/MW\n',p(10),q(10),s(10),mu_s(10)); 258 | fprintf(1,'\n'); 259 | fprintf(1,'Total active power : %1.3f MW consumed | %1.3f MW generated\n',sum(l_P),sum(p)); 260 | fprintf(1,'Total reactive power : %1.3f MVAr consumed | %1.3f MVAr generated\n',sum(l_Q),sum(q)); 261 | fprintf(1,'Total apparent power : %1.3f MVA consumed | %1.3f MVA generated\n',sum(l_S),sum(s)); 262 | 263 | %% Problem 4 264 | 265 | % Assumptions: 266 | % - Add back all previously disregarded terms and constraints 267 | % - Relax squared line current equation into inequality 268 | % - Goal is to minimize generation costs, given by c^T s 269 | 270 | % Solve with CVX 271 | cvx_begin 272 | variables q(13) s(13) Q(13,13) P(13,13) L(13,13) V(13) p(13) 273 | dual variables mu_ss{13} mu_s mu_q{13} mu_Lub{13} mu_p{13} mu_L{13} mu_Vlb mu_Vub 274 | minimize(c'*s ) 275 | subject to 276 | % Boundary condition for power line flows 277 | P( 1 , 1 ) == 0; 278 | Q( 1 , 1 ) == 0; 279 | % Boundary condition for squared line current 280 | L( 1 , 1 ) == 0; 281 | % Fix node 0 voltage to be 1 "per unit" (p.u.) 282 | V(1) == 1; 283 | % Loop over each node 284 | for jj = 1:13 285 | mu_p{jj}:p(jj)>=0; 286 | mu_q{jj}:q(jj)>=0; 287 | % Parent node, i = \rho(j) 288 | i=rho(jj); 289 | % Line Power Flows 290 | P(i,jj)==(l_P(jj)-p(jj))+ r(i,jj)*L(i,jj)+ A(jj,:)*P(jj,:)'; 291 | Q(i,jj)==(l_Q(jj)-q(jj))+ x(i,jj)*L(i,jj)+ A(jj,:)*Q(jj,:)'; 292 | % Nodal voltage 293 | V(jj)==V(i)+(r(i,jj)^2+x(i,jj)^2)*L(i,jj)-2*(r(i,jj)*P(i,jj)+x(i,jj)*Q(i,jj)); 294 | % Squared current magnitude on lines 295 | mu_L{jj}:L(i,jj)>=quad_over_lin([P(i,jj);Q(i,jj)],V(jj)); 296 | % Compute apparent power from active & reactive power 297 | mu_ss{jj}:s(jj)>= norm([p(jj),q(jj)],2); 298 | % Squared line current limits 299 | mu_Lub{jj}:L(i,jj)<=I_max(i,jj)^2; 300 | end 301 | % Nodal voltage limits 302 | mu_Vlb:v_min^2<=V; 303 | mu_Vub:V<=v_max^2; 304 | % Apparent Power Limits 305 | mu_s: s<=s_max; 306 | 307 | 308 | cvx_end 309 | 310 | % Output Results 311 | fprintf(1,'------------------- PROBLEM 4 --------------------\n'); 312 | fprintf(1,'--------------------------------------------------\n'); 313 | fprintf(1,'Minimum Generating Cost : %4.2f USD\n',cvx_optval); 314 | fprintf(1,'\n'); 315 | fprintf(1,'Node 0 [Grid] Gen Power : p_0 = %1.3f MW | q_0 = %1.3f MW | s_0 = %1.3f MW || mu_s0 = %3.0f USD/MW\n',p(1),q(1),s(1),mu_s(1)); 316 | fprintf(1,'Node 3 [Gas] Gen Power : p_3 = %1.3f MW | q_3 = %1.3f MW | s_3 = %1.3f MW || mu_s3 = %3.0f USD/MW\n',p(4),q(4),s(4),mu_s(4)); 317 | fprintf(1,'Node 9 [Solar] Gen Power : p_9 = %1.3f MW | q_9 = %1.3f MW | s_9 = %1.3f MW || mu_s9 = %3.0f USD/MW\n',p(10),q(10),s(10),mu_s(10)); 318 | fprintf(1,'\n'); 319 | fprintf(1,'Total active power : %1.3f MW consumed | %1.3f MW generated\n',sum(l_P),sum(p)); 320 | fprintf(1,'Total reactive power : %1.3f MVAr consumed | %1.3f MVAr generated\n',sum(l_Q),sum(q)); 321 | fprintf(1,'Total apparent power : %1.3f MVA consumed | %1.3f MVA generated\n',sum(l_S),sum(s)); 322 | fprintf(1,'\n'); 323 | for jj = 1:13 324 | fprintf(1,'Node %2.0f Voltage : %1.3f p.u.\n',jj,sqrt(V(jj))); 325 | end 326 | 327 | 328 | -------------------------------------------------------------------------------- /Optimal Economic Dispatch in Distribution Feeders with Renewables/HW3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Optimal Economic Dispatch in Distribution Feeders with Renewables/HW3.pdf -------------------------------------------------------------------------------- /Optimal Economic Dispatch in Distribution Feeders with Renewables/temp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Optimal PHEV Energy Management using Dynamic Programming/DAYARAMANI_DEEP_HW5.m: -------------------------------------------------------------------------------- 1 | %% CE 295 - Energy Systems and Control 2 | % HW 5 : Optimal Energy Management of PHEV via Dynamic Programming 3 | % Deeo Dayaramani, SID 3032083601 4 | % Prof. Moura 5 | 6 | 7 | 8 | clear; close all; 9 | fs = 15; % Font Size for plots 10 | 11 | %% Parameters and Data 12 | 13 | % Time step 14 | Delta_t = 1; 15 | 16 | % Fuel consumption in grams per unit energy 17 | alph = 1e-4; % [g/(s-W)] 18 | 19 | Qcap = 5*3600; % [A-s = Coulombs] 20 | V_oc = 330; % [volts] 21 | 22 | % Limits on Batt Power, Eng Power, SOC 23 | P_batt_max = 15e3; % [W] 24 | P_eng_max = 35e3; % [W] 25 | 26 | SOC_min = 0.25; % [-] 27 | SOC_max = 0.9; % [-] 28 | 29 | 30 | % Plot Power Demand Data 31 | M = csvread('UDDS_Pdem.csv',1,0); 32 | t = M(:,1); 33 | P_dem = M(:,2)*1e3; % convert from kW to W 34 | v_dc = M(:,3); 35 | 36 | figure(1); clf; 37 | 38 | subplot(2,1,1); 39 | % plot speed 40 | plot(t, v_dc, '-k') 41 | xlabel('Time in seconds') 42 | ylabel('Drive Cycle Speed (m/s)') 43 | subplot(2,1,2) 44 | % plot power demand 45 | plot(t, P_dem, '-b') 46 | ylabel('Power Demand in kW') 47 | xlabel('Time in seconds') 48 | % Plot Engine efficiency Curve 49 | eff = []; 50 | for i= 1:length(P_dem) 51 | eff(i) = eta_eng(P_dem(i)); 52 | end 53 | figure(2); clf; 54 | plot(P_dem, eff, '-k') % plot efficiency versus engine power, for total range of engine powers 55 | 56 | %% Grid State and Preallocate 57 | SOC_grid = (SOC_min:0.005:SOC_max)'; 58 | 59 | % Grid size 60 | ns = length(SOC_grid); % No. of states 61 | 62 | % Planning horizon (time steps) 63 | N = length(t); 64 | 65 | % Preallocate Value Function (rows index state, columns index time) 66 | V = inf*ones(ns,N+1); 67 | 68 | % Preallocate Control (rows index state, columns index time) 69 | u_star = zeros(ns,N); 70 | 71 | %% Solve DP 72 | tic; 73 | % Boundary Condition of Value Function (Principle of Optimality) 74 | V(:,N+1) = 0; 75 | % Iterate backward in time 76 | for k = N:-1:1 77 | % Iterate over SOC 78 | for idx = 1:ns 79 | bound1 = ((-SOC_max + SOC_grid(idx))*Qcap*V_oc/Delta_t); 80 | bound2 = -P_batt_max; 81 | bound3 = P_dem(k) - P_eng_max; 82 | bound4 = ((-SOC_min + SOC_grid(idx))*Qcap*V_oc/Delta_t); 83 | bound5 = P_batt_max; 84 | bound6 = P_dem(k); 85 | % Find dominant bounds 86 | lb = max([bound1 bound2 bound3]); 87 | ub = min([bound4 bound5 bound6]); 88 | % Grid Battery Power between dominant bounds 89 | P_batt_grid = linspace(lb,ub,200)'; 90 | % Compute engine power (vectorized for all P_batt_grid) 91 | P_eng = P_dem(k) - P_batt_grid; 92 | % Cost-per-time-step (vectorized for all P_batt_grid) 93 | g_k = alph*Delta_t*P_eng./eta_eng(P_eng); 94 | % Calculate next SOC (vectorized for all P_batt_grid) 95 | SOC_nxt = SOC_grid(idx)-Delta_t/Qcap/V_oc*P_batt_grid; 96 | % Compute value function at nxt time step (need to interpolate) 97 | V_nxt = interp1(SOC_grid,V(:,k+1),SOC_nxt,'linear'); 98 | % Value Function (Principle of Optimality) 99 | [V(idx, k), ind] = min(g_k+V_nxt); 100 | % Save Optimal Control 101 | u_star(idx,k) = P_batt_grid(ind); 102 | end 103 | end 104 | solveTime = toc; 105 | fprintf(1,'DP Solver Time %2.2f sec \n',solveTime); 106 | 107 | %% Simulate Results 108 | 109 | % Preallocate 110 | SOC_sim = zeros(N,1); 111 | P_batt_sim = zeros(N,1); 112 | P_eng_sim = zeros(N,1); 113 | J_sim = zeros(N,1); 114 | 115 | % Initialize 116 | SOC_0 = 0.75; 117 | SOC_sim(1) = SOC_0; 118 | 119 | % Simulate PHEV Dynamics 120 | for k = 1:(N-1) 121 | 122 | % Use optimal battery power, for given SOC (need to interpolate) 123 | P_batt_sim(k) = interp1(SOC_grid, u_star(:,k), SOC_sim(k) ); 124 | 125 | % Compute engine power 126 | P_eng_sim(k) = P_dem(k)-P_batt_sim(k); 127 | 128 | % Fuel Consumption 129 | J_sim(k) = alph*Delta_t*P_eng_sim(k)/eta_eng(P_eng_sim(k)); 130 | 131 | % Time-step SOC dynamics 132 | SOC_sim(k+1) = SOC_sim(k) -Delta_t/Qcap/V_oc*P_batt_sim(k); 133 | 134 | end 135 | 136 | fprintf(1,'Total Fuel Consumption %2.2f kg \n',sum(J_sim)/1e3); 137 | 138 | %% Plot Results 139 | figure(3); clf; 140 | 141 | subplot(4,1,1); 142 | % UDDS speed versus time 143 | plot(t, v_dc, '-k'); 144 | xlabel('Time in seconds'); 145 | ylabel('UDDS Speed in m/s'); 146 | title('UDDS Speed vs time') 147 | subplot(4,1,2); 148 | % SOC versus time 149 | plot(t, SOC_sim, '-b'); 150 | 151 | xlabel('Time in seconds'); 152 | ylabel('SOC of the Battery'); 153 | title('SOC of Battery vs time') 154 | subplot(4,1,3); 155 | % Accumulated fuel consumption [g] versus time 156 | for i = 1:length(J_sim) 157 | J_new(i) = sum(J_sim(1:i)); 158 | end 159 | plot(t, J_new,'-r'); 160 | 161 | xlabel('Time in seconds'); 162 | ylabel('Accumulated Fuel Consumption in g'); 163 | title('Accumulated Fuel Consumption vs Time') 164 | subplot(4,1,4); 165 | % Battery and engine power [kW] versus time 166 | hold on 167 | plot(t,P_batt_sim,'-b'); 168 | plot(t,P_eng_sim,'-k'); 169 | hold off 170 | 171 | xlabel('Time in seconds'); 172 | ylabel('Power in kW'); 173 | legend('Battery Power', 'Engine Power'); 174 | title('Battery and Engine Power vs Time') 175 | %% 176 | if sum(SOC_sim>SOC_max | SOC_sim=-P_batt_max) 182 | disp('P_batt constriants sarsfied'); 183 | end 184 | end 185 | 186 | if (P_eng_sim<=P_eng_max) 187 | if (P_eng_sim>=0) 188 | disp('P_eng constriants sarsfied') 189 | end 190 | end 191 | 192 | %% 193 | SOC_0 = 0.9:-0.15:0.3; 194 | figure 195 | subplot(2,1,1) 196 | grid on; hold on; 197 | xlabel('time in seconds'); ylabel('Final SOC of Battery'); 198 | J=zeros(length(SOC_0),1); 199 | 200 | for j=1:length(SOC_0) 201 | SOC_sim(1) = SOC_0(j); 202 | 203 | % Simulate PHEV Dynamics 204 | for k = 1:(N-1) 205 | 206 | % Use optimal battery power, for given SOC (need to interpolate) 207 | P_batt_sim(k) = interp1(SOC_grid,u_star(:,k),SOC_sim(k),'linear'); 208 | 209 | % Compute engine power 210 | P_eng_sim(k) = P_dem(k)-P_batt_sim(k); 211 | 212 | % Fuel Consumption 213 | J_sim(k) = alph*Delta_t*(P_eng_sim(k)/eta_eng(P_eng_sim(k))); 214 | 215 | % Time-step SOC dynamics 216 | SOC_sim(k+1) = SOC_sim(k)-(Delta_t/(Qcap*V_oc))*P_batt_sim(k); 217 | end 218 | subplot(2,1,1) 219 | plot(t,SOC_sim,'DisplayName',['SOC_0=' num2str(SOC_0(j))]); 220 | J(j)=sum(J_sim/1000); 221 | end 222 | legend show 223 | 224 | subplot(2,1,2) 225 | plot(SOC_0,J,'-*'); 226 | grid on; hold on; 227 | xlabel({'$SOC_0$'},'interpreter','latex'); ylabel('TFC [kg]'); 228 | -------------------------------------------------------------------------------- /Optimal PHEV Energy Management using Dynamic Programming/DAYARAMANI_DEEP_HW5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Optimal PHEV Energy Management using Dynamic Programming/DAYARAMANI_DEEP_HW5.pdf -------------------------------------------------------------------------------- /Optimal PHEV Energy Management using Dynamic Programming/HW5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/Optimal PHEV Energy Management using Dynamic Programming/HW5.pdf -------------------------------------------------------------------------------- /Optimal PHEV Energy Management using Dynamic Programming/temp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repo has 6 projects: 2 | 1. Battery Modeling, Analysis and Simulation 3 | 2. State Estimation in Oil and Gas Well Drilling 4 | 3. Optimal Economic Dispatch in Distribution Feeders with Renewables 5 | 4. Forecasting Residential Electricity Power Consumption 6 | 5. Optimal PHEV Energy Management via Dynamic Programming 7 | 6. Final Project- Flight Path Optimization 8 | -------------------------------------------------------------------------------- /State Estimation in Oil and Gas Well Drilling/DAYARAMANI_DEEP_HW02.m: -------------------------------------------------------------------------------- 1 | %% CE 295 - Energy Systems and Control 2 | % HW 2 : State Estimation in Oil Well Drilling 3 | % Deep Dayaramani, SID 3032083601 4 | % Prof. Moura 5 | 6 | clear; close all; 7 | fs = 15; % Font Size for plots 8 | 9 | %% Drill String Parameters 10 | 11 | p.J_T =100 ; % Table/top rotational inertia 12 | p.J_B = 25 ; % Bottom/bit rotational inertia 13 | p.k =2 ; % Spring constant 14 | p.b = 5; % Drag coefficient 15 | 16 | %% Problem 2 - Observability Analysis 17 | 18 | % State space matrices 19 | A4 = [0 , 0 , 1 , 0;0 , 0 , 0 , 1 ;-p.k/p.J_T , p.k/p.J_T , -p.b/p.J_T , 0 ; p.k/p.J_B , -p.k/p.J_B , 0 , -p.b/p.J_B ]; 20 | B4 = [0,0;0,0; 1/p.J_T,0;0,-1/p.J_B]; 21 | C4 = [0,0,1,0]; 22 | 23 | % Compute observability Matrix for 4-state system and rank 24 | O4 = [C4; C4*A4; C4*A4*A4; C4*A4*A4*A4]; 25 | disp('Rank of Observability Matrix for four-state system') 26 | rank(O4) 27 | 28 | % New A Matrix, for 3-state system 29 | A =[0, 1,-1;-p.k/p.J_T,-p.b/p.J_T, 0 ; p.k/p.J_B, 0, -p.b/p.J_B,]; 30 | B =[0,0;1/p.J_T,0;0,-1/p.J_B]; 31 | C =[0,1,0]; 32 | 33 | % Observability Matrix for 3-state system and rank 34 | O =[C; C*A; C*A*A]; 35 | disp('Rank of Observability Matrix for three-state system') 36 | rank(O) 37 | 38 | %% Problem 3 - Measurement Data 39 | data = csvread('HW2_Data.csv'); 40 | t = data(:,1); % t : time vector [sec] 41 | y_m = data(:,2); % y_m : measured table velocity [radians/sec] 42 | T = data(:,3); % T : table torque [N-m] 43 | omega_B_true = data(:,4); % \omega_B : true rotational speed of bit [radians/sec] 44 | 45 | figure(1); clf; 46 | 47 | subplot(2,1,1); 48 | plot(t,T,'k'); 49 | legend('Table Torque T(t) vs time'); 50 | xlabel('time (s)'); 51 | ylabel('Table Torque T(t)- [N-m]'); 52 | % Plot table torque 53 | 54 | subplot(2,1,2); 55 | plot(t,y_m,'b'); 56 | legend('Measured Table Velocity vs time'); 57 | xlabel('time (s)'); 58 | ylabel('Measured Table Velocity [radians/sec]'); 59 | % Plot measured table velocity 60 | 61 | %% Problem 4 - Luenberger Observer 62 | 63 | % Eigenvalues of open-loop system 64 | disp('Eigenvalues of 3-state system:') 65 | eig(A) 66 | 67 | % Desired poles of estimation error system 68 | % They should have negative real parts 69 | % Complex conjugate pairs 70 | lam = [ -0.2502 + 0.8958i, -0.2502 - 0.8958i, -0.2497 + 0.0000i]; 71 | 72 | % Compute observer gain (See Remark 3.1 in Notes. Use "place" command) 73 | L = place(A', C', lam)'; 74 | 75 | % State-space Matrices for Luenberger Observer 76 | A_lobs = A-L*C ; 77 | B_lobs = [B(:,1), L]; 78 | C_lobs = C; 79 | sys_lobs = ss( A_lobs ,B_lobs , C_lobs ,0); 80 | 81 | % Inputs to observer 82 | u = [T ,y_m]; 83 | 84 | % Initial Conditions for Luenberger Observer 85 | x_hat0 = [0 ;0 ;0 ]; 86 | 87 | % Simulate Response 88 | [y,t,x_hat] = lsim(sys_lobs,u,t,x_hat0); 89 | 90 | % Parse out states 91 | theta_hat = x_hat(:,1); 92 | omega_T_hat = x_hat(:,2); 93 | omega_B_hat = x_hat(:,3); 94 | 95 | % Plot Results 96 | figure(2); clf; 97 | 98 | subplot(2,1,1); 99 | plot(t, omega_B_hat,'k',t, omega_B_true, 'b'); 100 | xlabel('time (s)'); 101 | ylabel('True Rotational Speed of Bit (radians/sec) ') 102 | legend('Estimated Rotational Speed of Bit', 'True Rotational Speed of Bit'); 103 | % Plot true and estimated bit velocity 104 | sum = 0; 105 | for i = 1:length(omega_B_hat) 106 | sum = sum + (omega_B_true(i) - omega_B_hat(i))^2; 107 | end 108 | rmse = sqrt((1/length(t))*(sum)); 109 | subplot(2,1,2); 110 | plot(t,omega_B_true- omega_B_hat,'b'); 111 | legend('Error between true and estimated bit velocity') 112 | xlabel('time (s)') 113 | ylabel({'$$\omega_{B}$$ - $$\hat{\omega_{B}}$$'}, 'Interpreter', 'Latex') 114 | % Plot error between true and estimated bit velocity 115 | 116 | 117 | %% Problem 5 - Kalman Filter 118 | % Noise Covariances 119 | W = [0,0,0;0,0,0;0,0,0.0805] ; % You design this one. 120 | N = 0.02 ; 121 | Sig0 = eye(3); 122 | 123 | % Input data 124 | input_data = [t, T, y_m]; 125 | 126 | % Initial Condition 127 | x_hat0 = [0 ;0 ;0 ]; 128 | states0 = [x_hat0; reshape(Sig0,[9 1])]; 129 | 130 | % Simulate Kalman Filter 131 | % This coding is a little complicated for novice Matlab users. 132 | % Try to reverse engineer what I've done here. 133 | [tsim,states] = ode45(@(t,x) ode_kf(t,x,A,B(:,1),C,input_data,W,N),t,states0); 134 | 135 | % Parse States 136 | theta_hat = states(:,1); 137 | omega_T_hat = states(:,2); 138 | omega_B_hat = states(:,3); 139 | Sig33 = states(:,end) ; % Parse out the (3,3) element of Sigma only! 140 | Sigma = reshape(states(end,4:end),[3 3]); 141 | L_end = Sigma*C'*inv(N); 142 | disp(eig(A-L_end*C)); 143 | % Compute the upper and lower bounds as described in Problem 5c. 144 | omega_B_hat_upperbound = omega_B_hat + sqrt(Sig33); 145 | omega_B_hat_lowerbound = omega_B_hat - sqrt(Sig33); 146 | 147 | % Plot Results 148 | figure(3); clf; 149 | 150 | subplot(2,1,1); 151 | plot(tsim, omega_B_true, 'b', tsim, omega_B_hat, 'g',... 152 | tsim, omega_B_hat - sqrt(Sig33), 'k-.', tsim, omega_B_hat + sqrt(Sig33), 'k-.'); 153 | 154 | xlabel('time (s)'); 155 | ylabel('Rotational Speed of Bit (radians/sec) ') 156 | legend('Estimated Rotational Speed of Bit', 'True Rotational Speed of Bit', 'One standard deviation upper bound', 'One Standard Deviation Lower Bound'); 157 | 158 | % Plot true and estimated bit velocity 159 | % Plot estimated bit velocity plus/minus one sigma 160 | 161 | subplot(2,1,2); 162 | plot(tsim, omega_B_true - omega_B_hat); 163 | % Plot error between true and estimated bit velocity 164 | legend('Error between true and estimated bit velocity') 165 | xlabel('time (s)') 166 | ylabel({'$$\omega_{B}$$ - $$\hat{\omega_{B}}$$'}, 'Interpreter', 'Latex') 167 | 168 | 169 | omega_B_tilde = omega_B_true - omega_B_hat; 170 | RMSE = sqrt(mean(omega_B_tilde.^2)); 171 | fprintf(1,'Kalman Filter RMSE: %1.4f rad/s\n',RMSE); 172 | 173 | 174 | %% Problem 6 - Extended Kalman Filter 175 | % New parameters 176 | p.k1 = 2; 177 | p.k2 =5 ; 178 | W = [0,0,0;0,0,0;0,0,0.0805] ; % You design this one. 179 | N = 0.02 ; 180 | Sig0 = eye(3); 181 | 182 | % Input data 183 | input_data = [t, T, y_m]; 184 | 185 | % Initial Condition 186 | x_hat0 = [0 ;0 ;0 ]; 187 | states0 = [x_hat0; reshape(Sig0,[9 1])]; 188 | 189 | 190 | % Simulate Kalman Filter 191 | % This coding is a little complicated for novice Matlab users. 192 | % Try to reverse engineer what I've done here. 193 | [tsim,states] = ode45(@(t,x) ode_ekf(t,x,A,B(:,1),C,input_data,W,N,p.k2,p.J_T,p.J_B),t,states0); 194 | 195 | % Parse States 196 | theta_hat = states(:,1); 197 | omega_T_hat = states(:,2); 198 | omega_B_hat = states(:,3); 199 | Sig33 = states(:,end) ; % Parse out the (3,3) element of Sigma only! 200 | Sigma = reshape(states(end,4:end),[3 3]); 201 | L_end = Sigma*C'*inv(N); 202 | disp(eig(A-L_end*C)); 203 | % Compute the upper and lower bounds as described in Problem 5c. 204 | omega_B_hat_upperbound = omega_B_hat + sqrt(Sig33); 205 | omega_B_hat_lowerbound = omega_B_hat - sqrt(Sig33); 206 | 207 | % Plot Results 208 | figure(4); clf; 209 | 210 | subplot(2,1,1); 211 | plot(tsim, omega_B_true, 'b', tsim, omega_B_hat, 'g',... 212 | tsim, omega_B_hat - sqrt(Sig33), 'k-.', tsim, omega_B_hat + sqrt(Sig33), 'k-.'); 213 | 214 | xlabel('time (s)'); 215 | ylabel('Rotational Speed of Bit (radians/sec) ') 216 | legend('Estimated Rotational Speed of Bit', 'True Rotational Speed of Bit', 'One standard deviation upper bound', 'One Standard Deviation Lower Bound'); 217 | 218 | % Plot true and estimated bit velocity 219 | % Plot estimated bit velocity plus/minus one sigma 220 | 221 | subplot(2,1,2); 222 | plot(tsim, omega_B_true - omega_B_hat); 223 | % Plot error between true and estimated bit velocity 224 | legend('Error between true and estimated bit velocity') 225 | xlabel('time (s)') 226 | ylabel({'$$\omega_{B}$$ - $$\hat{\omega_{B}}$$'}, 'Interpreter', 'Latex') 227 | 228 | 229 | omega_B_tilde = omega_B_true - omega_B_hat; 230 | RMSE = sqrt(mean(omega_B_tilde.^2)); 231 | fprintf(1,'Extended Kalman Filter RMSE: %1.4f rad/s\n',RMSE); 232 | % now you are on your own! 233 | function [xdot] = ode_ekf(t,x,A,B,C,input_data,W,N,k2,JT,JB) 234 | 235 | % Parse States 236 | x_hat = x(1:3); 237 | Sig = reshape(x(4:end) ,[3 3]); % Need to reshape Sigma from vector to matrix 238 | 239 | % Parse and interpolate input signal data 240 | % This is a coding trick for time-varying input data. 241 | it = input_data(:,1); 242 | iT = input_data(:,2); 243 | iy_m = input_data(:,3); 244 | 245 | T = interp1(it,iT,t); 246 | y_m = interp1(it,iy_m,t); 247 | 248 | u= [T,(x_hat(1,:)).^3]; 249 | F = A + [0,0,0; -3*k2*(x_hat(1,:)^2)/JT,0,0;3*k2*(x_hat(1,:)^2)/JB,0,0]; 250 | H = C; 251 | % Compute Kalman Gain (Look at Chapter 3, Section 4) 252 | L = Sig*H'*inv(N); 253 | 254 | % Kalman Filter equations 255 | x_hat_dot = (A-L*C)*x_hat+ B*T+ L*y_m; 256 | 257 | % Riccati Equation for Sigma 258 | Sig_dot = Sig*F'+F*Sig+W-Sig*H'*inv(N)*H*Sig; 259 | 260 | % Concatenate LHS 261 | xdot = [x_hat_dot; reshape(Sig_dot, [9 1])]; 262 | end 263 | function [xdot] = ode_kf(t,x,A,B,C,input_data,W,N) 264 | 265 | % Parse States 266 | x_hat = x(1:3); 267 | Sig = reshape(x(4:end),[3 3]); % Need to reshape Sigma from vector to matrix 268 | 269 | % Parse and interpolate input signal data 270 | % This is a coding trick for time-varying input data. 271 | it = input_data(:,1); 272 | iT = input_data(:,2); 273 | iy_m = input_data(:,3); 274 | 275 | T = interp1(it,iT,t); 276 | y_m = interp1(it,iy_m,t); 277 | % Compute Kalman Gain (Look at Chapter 3, Section 4) 278 | L = Sig*C'*inv(N); 279 | 280 | % Kalman Filter equations 281 | x_hat_dot = (A-L*C)*x_hat+ B*T+ L*y_m; 282 | 283 | % Riccati Equation for Sigma 284 | Sig_dot = Sig*A'+A*Sig+W-Sig*C'*inv(N)*C*Sig; 285 | 286 | % Concatenate LHS 287 | xdot = [x_hat_dot; reshape(Sig_dot, [9 1])]; 288 | end 289 | -------------------------------------------------------------------------------- /State Estimation in Oil and Gas Well Drilling/DAYARAMANI_DEEP_HW02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/State Estimation in Oil and Gas Well Drilling/DAYARAMANI_DEEP_HW02.pdf -------------------------------------------------------------------------------- /State Estimation in Oil and Gas Well Drilling/HW2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deep-daya/Energy_Systems_and_Control-Projects/94303fc2aea98ab46ac1618bcc11dbf8765989f4/State Estimation in Oil and Gas Well Drilling/HW2.pdf -------------------------------------------------------------------------------- /State Estimation in Oil and Gas Well Drilling/temp: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------