POLYCOST Evaluates polynomial generator cost & derivatives. F = POLYCOST(GENCOST, PG) returns the vector of costs evaluated at PG DF = POLYCOST(GENCOST, PG, 1) returns the vector of first derivatives of costs evaluated at PG D2F = POLYCOST(GENCOST, PG, 2) returns the vector of second derivatives of costs evaluated at PG GENCOST must contain only polynomial costs PG is in MW, not p.u. (works for QG too) This is a more effecient implementation that what can be done with MATLAB's built-in POLYVAL and POLYDER functions.
0001 function f = polycost(gencost, Pg, der) 0002 %POLYCOST Evaluates polynomial generator cost & derivatives. 0003 % F = POLYCOST(GENCOST, PG) returns the vector of costs evaluated at PG 0004 % 0005 % DF = POLYCOST(GENCOST, PG, 1) returns the vector of first derivatives 0006 % of costs evaluated at PG 0007 % 0008 % D2F = POLYCOST(GENCOST, PG, 2) returns the vector of second derivatives 0009 % of costs evaluated at PG 0010 % 0011 % GENCOST must contain only polynomial costs 0012 % PG is in MW, not p.u. (works for QG too) 0013 % 0014 % This is a more effecient implementation that what can be done with 0015 % MATLAB's built-in POLYVAL and POLYDER functions. 0016 0017 % MATPOWER 0018 % Copyright (c) 2009-2016, Power Systems Engineering Research Center (PSERC) 0019 % by Ray Zimmerman, PSERC Cornell 0020 % 0021 % This file is part of MATPOWER. 0022 % Covered by the 3-clause BSD License (see LICENSE file for details). 0023 % See https://matpower.org for more info. 0024 0025 %%----- initialize ----- 0026 %% define named indices into data matrices 0027 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost; 0028 0029 if nargin < 3 0030 der = 0; 0031 end 0032 0033 if any(gencost(:, MODEL) == PW_LINEAR) 0034 error('polycost: all costs must be polynomial'); 0035 end 0036 0037 ng = length(Pg); 0038 maxN = max(gencost(:, NCOST)); 0039 minN = min(gencost(:, NCOST)); 0040 0041 %% form coefficient matrix where 1st column is constant term, 2nd linear, etc. 0042 c = zeros(ng, maxN); 0043 for n = minN:maxN 0044 k = find(gencost(:, NCOST) == n); %% cost with n coefficients 0045 c(k, 1:n) = gencost(k, (COST+n-1):-1:COST); 0046 end 0047 0048 %% do derivatives 0049 for d = 1:der 0050 if size(c, 2) >= 2 0051 c = c(:, 2:maxN-d+1); 0052 else 0053 c = zeros(ng, 1); 0054 break; 0055 end 0056 for k = 2:maxN-d 0057 c(:, k) = k * c(:, k); 0058 end 0059 end 0060 0061 %% evaluate polynomial 0062 if isempty(c) 0063 f = zeros(size(Pg)); 0064 else 0065 f = c(:, 1); %% constant term 0066 for k = 2:size(c, 2) 0067 f = f + c(:, k) .* Pg .^ (k-1); 0068 end 0069 end