-
Notifications
You must be signed in to change notification settings - Fork 1
/
OCVPredictSandBox.m
353 lines (281 loc) · 10.4 KB
/
OCVPredictSandBox.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
function [thetaOpt] = OCVPredictSandBox(x,y,varargin)
% Predict the OCV value by fitting ym = U + sum_i a_iexp(b_ix) function,
% subject to U + sum_i a_i = y(0).
%
% Input arguments:
% x: time vector size N x 1
% y: Voltage data set with relaxation size N x 1
%
% Output:
% thetaOpt: Optimum parameters based on constrained optimisation
% theta = [U, a1;..;an, b1;...;bn] size nExp x 2
% The estimated OCV is theta(1)
%
% Copyright (C) W. D. Widanage - WMG, University of Warwick, U.K. 25/12/2016 (The Thing That Should Not be)
% All Rights Reserved
% Software may be used freely for non-comercial purposes only
p = inputParser; % Create an input parse object to handle positional and property-value arguments
% Create variable names and assign default values after checking the value
addRequired(p,'x', @isnumeric);
addRequired(p,'y', @isnumeric);
% Optional parameters
addParameter(p,'nExp',[2:3])
% Re-parse parObj
parse(p, x, y, varargin{:})
if y(end) > y(1) % A discharge pulse
yGP = -y + max(y);
else
yGP = y - min(y);
end
[thetaGP, optModelOrder] = GraphPeelOpt(x,yGP,p.Results);
optNExp = optModelOrder.optModelOrder;
problem.options = optimoptions('fmincon','MaxFunctionEvaluations',2000,'SpecifyObjectiveGradient',false);
problem.solver = 'fmincon';
objFcn = @(theta)sumSqErr(theta,x,y);
problem.objective = objFcn;
if y(end) > y(1) % A discharge pulse
problem.x0 = [max(y); -thetaGP(1:optNExp); thetaGP(optNExp+1:end)];
% problem.lb = [max(y)+1E-3;-inf(optNExp,1)];
else
problem.x0 = [min(y); thetaGP];
end
nonLinCon = @(theta)initalPointConst(theta,y);
problem.nonlcon = nonLinCon;
[thetaOpt, objFcnFmin]= fmincon(problem);
[thetaGA, objFcnGA] = ga(objFcn,length(problem.x0),[],[],[],[],[],[],nonLinCon);
end
function [y,J] = SumOfExpOffSet(theta,x)
% Sum of exponentials with bias y = U + sum_i^M aexp(bx).
%
% Input arguments:
% theta: [U, a1,...,aM,b1,...,bM], size M+1 x 1 with M the number of
% exponentials
% x: Exponent values, size N x 1
%
% Output
% y: function value size N x 1
% J: Jacobian matrix size N x M+1
%
% Copyright (C) W. D. Widanage - WMG, University of Warwick, U.K. 25/12/2016 (Heavy!)
% All Rights Reserved
% Software may be used freely for non-comercial purposes only
p = inputParser; % Create an input parse object to handle positional and property-value arguments
% Create variable names and assign default values after checking the value
addRequired(p,'theta', @isnumeric);
addRequired(p,'x', @isnumeric);
% Re-parse parObj
parse(p, theta, x)
x = p.Results.x(:);
theta = p.Results.theta(:);
nData = length(x);
nExp = length(theta(2:end))/2; % Number of exponentials
U = theta(1);
a = theta(2:nExp+1);
b = theta(nExp+2:end);
y = zeros(nData,1);
for ii = 1:nExp
yTmp = a(ii)*exp(b(ii)*x);
y = y + yTmp;
J1(:,ii) = exp(b(ii)*x);
J2(:,ii) = a(ii)*x.*exp(b(ii)*x);
end
y = y + U;
J = [ones(nData,1),J1,J2];
end
% Equatlity constraint function
function [y0InEq,y0Eq] = initalPointConst(theta,y)
y0InEq = []; % Set inequlatiy output argument to zero for fmincon function
nExp = length(theta(2:end))/2; % Number of exponentials
U = theta(1);
a = theta(2:nExp+1);
y0Eq = U + sum(a) - y(1); % y(0) = U + sum_i a_i
end
% Cost-function
function [sse,gradF] = sumSqErr(theta,x,y)
[ym,J] = SumOfExpOffSet(theta,x);
res = y - ym;
sse = norm(res)^2;
gradF = J'*res;
end
function [thetaOpt0,outOpt] = GraphPeelOpt(x,y,varargin)
% Perfom graph peeling and optimisation to determine number of sum of
% exponentials in data. y = sum aexp(bx). Number of data points should be
% more than 4.
%
% Input arguments:
% x: exponent
% y: function value
%
% Optional input arguments. Create a structure variable with the following field:
% nExp: Set the maximum number of exponenetials to fit up to. Default
% nExp = [1:3]
% plotGP: Set plotGP to 'y' to plot graph-peeling curve. Default 'n'
%
% Outputs:
% thetaOpt: Overall optimum parameters based on the lowest cost-function.
% theta = [a1;..;an, b1;...;bn] size nExp x 2
% outOpt: A structured varaible with the following fields as optional extras
% theta: Optimised set of parameters for each nExp
% cF: Value of cost-function for each nExp
% modelOrder: Optimum number of exponentials (model
% order)
% slowestTimeConst: Slowest time constant for the optimum model
% order
%
% Copyright (C) W. D. Widanage - WMG, University of Warwick, U.K. 16/10/2016 (Calm)
% All Rights Reserved
% Software may be used freely for non-comercial purposes only
p = inputParser; % Create an input parse object to handle positional and property-value arguments
% Create variable names and assign default values after checking the value
addRequired(p,'x', @isnumeric);
addRequired(p,'y', @isnumeric);
% Optional parameters
% addParameter(p,'plotGP','n')
addParameter(p,'nExp',[1:3]);
% Re-parse parObj
parse(p, x, y, varargin{:})
x = p.Results.x(:);
y = p.Results.y(:);
nData = length(x);
nExp = p.Results.nExp; % Maximum number of exponentials
loopCnt = 0; % Initialise
for nn = nExp % Loop over maximum possible number of exp segments
loopCnt = loopCnt + 1; % Loop counter for each nExp
z = y; % Make a copy of the data for graph peeling
idxZero = find(z<=0);
z(idxZero) = 1E-3;
nPts = floor(nData/nn);
for pp = nn:-1:1 % Loop over each exp segment
lnz = log(z); % Log of raw data
sIdx = nPts*(pp-1) + 1;
if pp == nn
eIdx = nData;
else
eIdx = sIdx + nPts - 1;
end
lnzSeg = lnz(sIdx:eIdx);
xSeg = x(sIdx:eIdx);
K = [xSeg,ones(length(lnzSeg),1)];
pLin = Lls(K,lnzSeg); % Slope and intercept
b(pp,1) = pLin(1); % Exponent (slope of line)
a(pp,1) = exp(pLin(2)); % Coefficient
% Peel off exponential term
z = z - a(pp)*exp(b(pp)*x);
idxZero = find(z<=0);
z(idxZero) = 1E-3;
end
% Evalaute CF
ym = SumOfExp([a;b],x);
cF(loopCnt,1) = norm(y-ym)^2;
% Peform parameter optimastion for given graph-peel values
% fh = @SumOfExp;
% optionsExp.Jacobian = 'on';
% optionsExp.termMsg = 'n';
% optionsExp.iterMax = 100;
% [thetaOptTmp,infoTheta] = LMAlgorithm_varIdx(fh, [a;b], x, y, optionsExp);
% aOpt = thetaOptTmp(1:nn);
% bOpt = thetaOptTmp(nn+1:end);
%
% % options = optimset('TolX',0.1);
% % fh = @(lambda)sumExpErr(lambda,x,y);
% % bOpt = fminsearch(fh,b,options);
% % [sse,aOpt] = sumExpErr(bOpt,x,y);
%
% % Store optimised and intial values
% theta(loopCnt,1) = {[aOpt, bOpt]};
theta0(loopCnt,1) = {[a,b]};
% cF(loopCnt,1) = infoTheta.cF_iter(end);
% thetaFracErr(loopCnt,1) = {infoTheta.fracErr};
% % Corrected AIC
% dof = 2*nn+1;
% AIC(loopCnt,1) = nData*log(infoTheta.cF_iter(end)/nData) + 2*dof + 2*dof*(dof+1)/(nData-dof-1);
%
% % Note any poitive exponents to avoid and flag warning
% if any(bOpt>0)
% outOpt.msg = sprintf('Positive exponent for nExp = %d', nn);
% fprintf([outOpt.msg,'\n'])
% avoidExp(loopCnt,1) = true;
% else
% avoidExp(loopCnt,1) = false;
% end
end
% % Only select valid optimised values
% thetaValid = theta(~avoidExp,1);
% thetaFracErrValid = thetaFracErr(~avoidExp,1);
% theta0Valid = theta0(~avoidExp,1);
% nExpValid = nExp(~avoidExp);
% cFValid = cF(~avoidExp);
% AICValid = AIC(~avoidExp);
% Find optimum number of exponentials based on cost function or AIC
[~,idx] = min(cF);
optModelOrder = nExp(idx);
% thetaOpt = thetaValid{idx,1};
% thetaOptFracErr = thetaFracErrValid{idx,1};
thetaOpt0 = theta0{idx,1};
thetaOpt0 = thetaOpt0(:);
% Largest time constant and standard deviation in optimum solution
% [minExponent,idxMin] = min(-thetaOpt(:,2));
% timeConstMax = 1./minExponent;
% fracErrMinExponent = thetaOptFracErr(optModelOrder + idxMin);
% stdMaxTimeConst = timeConstMax*fracErrMinExponent;
% stdMaxTimeCont = (-1./(minExponent+stdMinExponent)) - timeConstMax;
outOpt.theta0 = theta0; % Save all optimised values for each of the nExp
outOpt.cF = cF; % Save the cost-function calue for each of the nExp
outOpt.optModelOrder = optModelOrder; % Save optimum model order
% outOpt.slowestTimeConst = timeConstMax; % Save the slowest time constant
% outOpt.slowestTimeConstStd = stdMaxTimeConst; % Save the slowest time constant std
%
% % Plot cost-fucntion value for each nExp and optimum fit for smallest cF
% if strcmpi(p.Results.plotGP,'y')
% figure
% subplot(2,1,1)
% plot(nExpValid,AICValid,'-o')
% xlabel('Number of exponentials'); ylabel('AIC value')
%
% subplot(2,1,2)
% yOpt = SumOfExp(thetaOpt(:),x);
% yhat = SumOfExp(thetaOpt0(:),x);
%
% plot(x,y,'-o',x,yhat,'- x',x,yOpt,'. -');
% xlabel('x');
% ylabel('y');
% legend('Measured','Graph peel','Optimised');
% title(sprintf('Fit for nExp = %g',nExpValid(idx)))
% end
end
function [y,J] = SumOfExp(theta,x)
% Sum of exponentials y = sum_i^M aexp(bx). i = 1...M.
%
% Input arguments:
% theta: [a1,...,aM,b1,...,bM], size M+1 x 1 with M the number of
% exponentials
% x: Exponent values, size N x 1
%
% Output
% y: function value size N x 1
% J: Jacobian matrix size N x M
%
% Copyright (C) W. D. Widanage - WMG, University of Warwick, U.K. 22/10/2016 (Black!)
% All Rights Reserved
% Software may be used freely for non-comercial purposes only
p = inputParser; % Create an input parse object to handle positional and property-value arguments
% Create variable names and assign default values after checking the value
addRequired(p,'theta', @isnumeric);
addRequired(p,'x', @isnumeric);
% Re-parse parObj
parse(p, theta, x)
x = p.Results.x(:);
theta = p.Results.theta(:);
nData = length(x);
nExp = length(theta)/2;
a = theta(1:nExp);
b = theta(nExp+1:end);
y = zeros(nData,1);
for ii = 1:nExp
yTmp = a(ii)*exp(b(ii)*x);
y = y + yTmp;
J1(:,ii) = exp(b(ii)*x);
J2(:,ii) = a(ii)*x.*exp(b(ii)*x);
end
J = [J1,J2];
end