POLY2PWL Converts polynomial cost variable to piecewise linear. PWLCOST = POLY2PWL(POLYCOST, PMIN, PMAX, NPTS) converts the polynomial cost variable POLYCOST into a piece-wise linear cost by evaluating at NPTS evenly spaced points between PMIN and PMAX. If the range does not include 0, then it is evaluated at 0 and NPTS-1 evenly spaced points between PMIN and PMAX.
0001 function pwlcost = poly2pwl(polycost, Pmin, Pmax, npts) 0002 %POLY2PWL Converts polynomial cost variable to piecewise linear. 0003 % PWLCOST = POLY2PWL(POLYCOST, PMIN, PMAX, NPTS) converts the polynomial 0004 % cost variable POLYCOST into a piece-wise linear cost by evaluating at 0005 % NPTS evenly spaced points between PMIN and PMAX. If the range does not 0006 % include 0, then it is evaluated at 0 and NPTS-1 evenly spaced points 0007 % between PMIN and PMAX. 0008 0009 % MATPOWER 0010 % Copyright (c) 1996-2016, Power Systems Engineering Research Center (PSERC) 0011 % by Ray Zimmerman, PSERC Cornell 0012 % 0013 % This file is part of MATPOWER. 0014 % Covered by the 3-clause BSD License (see LICENSE file for details). 0015 % See https://matpower.org for more info. 0016 0017 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost; 0018 0019 pwlcost = polycost; 0020 [m, n] = size(polycost); %% size of piece being changed 0021 pwlcost(:, MODEL) = PW_LINEAR; %% change cost model 0022 pwlcost(:, COST:n) = 0; %% zero out old data 0023 pwlcost(:, NCOST) = npts; %% change number of data points 0024 pwlcost(1, COST+2*(npts-1)+1) = 0; %% expand as needed 0025 0026 for i = 1:m 0027 if Pmin(i) > 0 0028 step = (Pmax(i) - Pmin(i)) / (npts - 2); 0029 xx = [0 Pmin(i):step:Pmax(i)]; 0030 elseif Pmax(i) < 0 0031 step = (Pmax(i) - Pmin(i)) / (npts - 2); 0032 xx = [Pmin(i):step:Pmax(i) 0]; 0033 else 0034 step = (Pmax(i) - Pmin(i)) / (npts - 1); 0035 xx = (Pmin(i):step:Pmax(i)); 0036 end 0037 yy = totcost(polycost(i, :), xx); 0038 pwlcost(i, COST:2:(COST + 2*(npts-1) )) = xx; 0039 pwlcost(i, (COST+1):2:(COST + 2*(npts-1) + 1)) = yy; 0040 end