-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.m
135 lines (113 loc) · 3.13 KB
/
lab2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
% 3) Quadratic Program Implementation
% -------------------------
% Variables
% Cost of energy sources [$/MWh]
c = [100 90 130 108 111 90 144 87];
% Max total cost of energy [$/MWh]
c_tot = 100;
% Standard deviation of energy sources [$/MWh]
std = [22 30 15 20 30 36 32 40];
% Total energy demand [MWh]
d_tot = 225;
% -------------------------
% Formulation
% Objective function: 0.5x^TQx + R^Tx
% Q matrix
Q = 2 * diag(std.^2);
% R matrix
R = zeros([1 8]);
% Constraints: Ax ? b
% A matrix
A = [% Energy Amount
-1 -1 -1 -1 -1 -1 -1 -1;
% Cost
c(1)-c_tot c(2)-c_tot c(3)-c_tot c(4)-c_tot c(5)-c_tot c(6)-c_tot c(7)-c_tot c(8)-c_tot;
% Demand
-1 0 0 0 0 0 0 0;
0 -1 0 0 0 0 0 0;
0 0 -1 0 0 0 0 0;
0 0 0 -1 0 0 0 0;
0 0 0 0 -1 0 0 0;
0 0 0 0 0 -1 0 0;
0 0 0 0 0 0 -1 0;
0 0 0 0 0 0 0 -1];
% b matrix
b = [-d_tot 0 0 0 0 0 0 0 0 0];
% Solve quadratic program
% Optimized energy amounts from each source
x = quadprog(Q, R, A, b);
% -------------------------
% Problem 3
% Energy amounts from each source utilizing 2012 portfolio
x_3 = ([7.5 8.3 43.4 9 2.3 4.4 0.9 6.3]/100) * d_tot;
% Associated risk
r_3 = 0.5 * x_3 * Q * x_3';
% Associated cost
c_3 = dot(c, x_3);
% -------------------------
% Problem 4
% Total cost ranging from from 50 to 250
n = 100;
c_tot_4 = linspace(88, 250, n);
% Risk associated with total cost range
r_4 = zeros(n, 1);
% Determine optimum energy amount and corresponding risk for each total cost
for i = 1:n
% Constraints: Ax ? b
% A matrix
A = [% Energy Amount
-1 -1 -1 -1 -1 -1 -1 -1;
% Cost
c(1)-c_tot_4(i) c(2)-c_tot_4(i) c(3)-c_tot_4(i) c(4)-c_tot_4(i) c(5)-c_tot_4(i) c(6)-c_tot_4(i) c(7)-c_tot_4(i) c(8)-c_tot_4(i);
% Demand
-1 0 0 0 0 0 0 0;
0 -1 0 0 0 0 0 0;
0 0 -1 0 0 0 0 0;
0 0 0 -1 0 0 0 0;
0 0 0 0 -1 0 0 0;
0 0 0 0 0 -1 0 0;
0 0 0 0 0 0 -1 0;
0 0 0 0 0 0 0 -1];
% Solve quadratic program
% Optimized energy amounts from each source
[x, risk] = quadprog(Q, R, A, b);
r_4(i) = risk;
end
% Plot of total cost vs. risk
plot(c_tot_4, r_4, 'LineWidth', 1.5);
xlabel('Total Energy Cost [$/MWh]');
ylabel('Risk [$^2]');
title('Total Energy Cost vs. Risk');
% -------------------------
% Problem 5
% Constraints: Ax ? b
% A matrix
A = [% Energy Amount
-1 -1 -1 -1 -1 -1 -1 -1;
% Cost
c(1)-c_tot c(2)-c_tot c(3)-c_tot c(4)-c_tot c(5)-c_tot c(6)-c_tot c(7)-c_tot c(8)-c_tot;
% Demand
-1 0 0 0 0 0 0 0;
0 -1 0 0 0 0 0 0;
0 0 -1 0 0 0 0 0;
0 0 0 -1 0 0 0 0;
0 0 0 0 -1 0 0 0;
0 0 0 0 0 -1 0 0;
0 0 0 0 0 0 -1 0;
0 0 0 0 0 0 0 -1;
% Supply Limit
1 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0;
0 0 1 0 0 0 0 0;
0 0 0 1 0 0 0 0;
0 0 0 0 1 0 0 0;
0 0 0 0 0 1 0 0;
0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 1;
% Renewable Portfolio Standard
1/3 1/3 1/3 1/3 (1/3)-1 (1/3)-1 (1/3)-1 (1/3)-1];
% b matrix
b = [-d_tot 0 0 0 0 0 0 0 0 0 40 50 150 35 10 15 200 50 0];
% Solve quadratic program
% Optimized energy amounts from each source
[x_5, r_5] = quadprog(Q, R, A, b);