-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstepinfo.sci
434 lines (428 loc) · 18.9 KB
/
stepinfo.sci
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//function stepinfo(sys)
//This function computes sytem characteristics when step input is given to the system.
//stepinfo(Y,T,YFINAL) computes the following parameters assuming zero initial conditions and no input offset.
// RiseTime: rise time of the system
// SettlingTime: settling time
// SettlingMin: min value of Y once the response has risen
// SettlingMax: max value of Y once the response has risen
// Overshoot: percentage overshoot (relative to YFINAL)
// Undershoot: percentage undershoot
// Peak: peak absolute value of Y
// PeakTime: time at which peak absolute value is reached.
//
//
//stepinfo(Y,T,YFINAL)
//For SISO systems Y and T are vectors of same length NS.
//For MIMO systems with NU inputs, NY outputs, Y should be
//specified as an array of size NS-by-NY-by-NU.
//Similarly, YFINAL should be specified as a NY-by-NU array.
//Then the function returns an output array of each quantity
//of size NY-by-NU.
//
//stepinfo(Y,T) uses the last sampled value of Y as the YFINAL
//stepinfo(Y) assumes T = 1:NS.
//
//stepinfo(sys) calculates the time response characteristics of
//dynamic system sys. The Rise Time, Settling Time etc. are all
//calculated in Time Units of the system.
//
//
//The default value of RiseTimeLims = [0.1 0.9] ie. from 10% to 90% of the final value.
//The user may change the limits by setting RiseTimeLims to the desired values.
//Example :
//If the desired value of rise time is from 5% to 95% of the final value, then the command looks like
//stepinfo(y,t,yfinal,'RiseTimeLims'[0.05 0.95])
//
//Settling time with default 2% limits
//The user may change this by setting SettlingTimeThreshold to the desired value.
//Example :
//If the desired value of settling time threshold is 5% of the final value, then the command looks like
//stepinfo(sys,'SettlingTimeThreshold',0.05)
//
//The function is also capable of giving Rise time high and rise time low. These values are not printed by default but they
//can only be assigned to a variable.
//RiseTimeHigh, RiseTimeLow can be extracted using the function in the following way
//Example :
//[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10] = stepinfo(Y,T,YFINAL)
//Then the variables x9 and x10 will give the values of RiseTimeHigh and RiseTimeLow respectively.
//
//Example:
// s = %s ;
// sys = syslin('c',(s+6)/(s^2+6*s+4)) ;
// stepinfo(sys) ;
//
//
// References :
// http://www.scilab.org/resources/documentation ;
// http://spoken-tutorial.org/
// http://in.mathworks.com/help/control/ ;
// http://in.mathworks.com/help/control/ref/stepinfo.html ;
// https://en.wikipedia.org/wiki/Control_systems;
//
//
// Author(s):
// Sanchit Gupta
//----------------------------------------------------------------------------------------------------------------------------//
//
//
//stepinfo(y,t,yfinal)
//Peak Value, Peak Time
function[peak_value, peak_time] = peak(y,t,yfinal) ;
if abs(yfinal) < %inf
[peak_value, index] = max(y) ;
peak_time = t(index)
else
// System is not stable
peak_value = %inf
peak_time = %inf
end
endfunction
//Function to find maximum and minimum overshoot
function[overshoot, undershoot] = overshoot_and_undershoot(y,t,yfinal)
if abs(yfinal) < %inf
if yfinal==0
overshoot = %inf;
if y(1:length(y))>= 0
undershoot = 0;
else
undershoot = %inf;
end
else
yrel = y/yfinal;
overshoot = 100 * max(0,max(yrel-1));
undershoot = -100 * min(0,min(yrel));
end
else
// System is not stable
overshoot = %nan
undershoot = %nan
end
endfunction
// Function to find Rise Time, settling minimum, settling maximum
// The default value of RiseTimeLims = [0.1 0.9] ie. from 10% to 90% of the final value.
// The user may change the limits by setting RiseTimeLims to the desired values.
// Settling min and Settling maximum are the minimum and maximum values once the response has risen.
function[rise_time, settling_min, settling_max, tHigh,tLow] = risetime(y,t,yfinal,RiseTimeLims)
if abs(yfinal) < %inf
ns = length(t) ;
yLow = y(1) + RiseTimeLims(1)*(yfinal - y(1));
iLow = 1 + find((y(1:ns-1)-yLow).*(y(2:ns)- yLow) <= 0,1);
if isempty(iLow)
// Has not reached the lower
tLow = %nan
elseif (iLow > 1) & (y(iLow)~=y(iLow-1))
// Interpolate for more accuracy
tLow = t(iLow) + (t(iLow)-t(iLow-1))/(y(iLow)-y(iLow-1))*(yLow - y(iLow)) ;
else
tLow = t(iLow) ;
end
yHigh = y(1) + RiseTimeLims(2)*(yfinal-y(1));
iHigh = 1+ find((y(1:ns-1)-yHigh).*(y(2:ns)-yHigh) <= 0,1);
if isempty(iHigh)
// Has not reached the higher limit
tHigh = %nan;
settling_min = %nan ;
settling_max = %nan ;
else
if (iHigh > 1) & (y(iHigh)~=y(iHigh-1))
// Interpolate for more accuracy
tHigh = t(iHigh) + (t(iHigh)-t(iHigh-1))/(y(iHigh)-y(iHigh-1))*(yHigh-y(iHigh));
else
tHigh = t(iHigh);
end
yRisen = y(iHigh:length(y)) ;
settling_min = min(yRisen) ;
settling_max = max(yRisen) ;
end
rise_time = tHigh - tLow ;
else
// System is not stable
tHigh = %inf
tLow = %inf
rise_time = %nan ;
settling_min = %nan ;
settling_max = %nan ;
end
endfunction
// Settling time with default 2% limits
// The user may change this by setting SettlingTimeThreshold to the desired value
function[settlingTime] = settlingtime(y,t,yfinal,SettlingTimeThreshold)
ns = length(t) ;
if abs(yfinal) < %inf
err = abs(y-yfinal);
rev_err = err(length(err):-1:1) ;
tol = SettlingTimeThreshold* max(err);
iSettle = length(err) - find(rev_err > tol, 1) + 1 ;
if isempty(iSettle)
// Pure gain
settlingTime = 0;
elseif iSettle==ns
//Has not settled
settlingTime = %nan;
elseif y(iSettle)~=y(iSettle+1)
// Interpolate for more accuracy
ySettle = yfinal + sign(y(iSettle)-yfinal) * tol;
settlingTime = t(iSettle)+(t(iSettle)-t(iSettle+1))/(y(iSettle)-y(iSettle+1))*(ySettle-y(iSettle));
else
// Discrete time or pure gain
settlingTime = t(iSettle+1);
end
else
// System is not stable
settlingTime = %nan ;
end
endfunction
// Main Step Info Function
function[RiseTime,SettlingTime,SettlingMin,SettlingMax,Overshoot,Undershoot,Peak,PeakTime,RiseTimeHigh,RiseTimeLow] = stepinfo(varargin)
if(length(varargin) >= 3)
for i = 1:length(varargin)-1
if (typeof(varargin(i))=='string') & (varargin(i)=='RiseTimeLims')
if size(varargin(i+1))==[1 2]
RiseTimeLims = varargin(i+1) ;
if (size(RiseTimeLims)<>[1 2])|(RiseTimeLims(1)>RiseTimeLims(2))|(RiseTimeLims(2) > 1)|(RiseTimeLims(1) < 0)
error('The RiseTimeLims must be a real 1-by-2 vector with nondecreasing values between 0 and 1.') ;
end
else
error('The RiseTimeLims must be a real 1-by-2 vector with nondecreasing values between 0 and 1.') ;
end
elseif (typeof(varargin(i))=='string') & (varargin(i)=='SettlingTimeThreshold')
SettlingTimeThreshold = varargin(i+1) ;
if (SettlingTimeThreshold > 1) | (SettlingTimeThreshold < 0)
error('SettlingTimeThreshold should be a scalar between 0 and 1') ;
end
else
// Default values of RiseTimeLims and SettlingTimeThreshold
RiseTimeLims = [0.1 0.9] ;
SettlingTimeThreshold = 0.02 ;
end
end
else
// Default values of RiseTimeLims and SettlingTimeThreshold
RiseTimeLims = [0.1 0.9] ;
SettlingTimeThreshold = 0.02 ;
end
if typeof(varargin(1))== "constant" ;
// Input is an array
[ns ny nu] = size(varargin(1)) ;
for k = 1:1:ny
for j = 1:1:nu
y_temp = varargin(1) ;
y = y_temp(1:ns,k,j)
if (length(varargin) == 1)
// y_temp = varargin(1) ;
// y = y_temp(1:ns,k,j)
t = [1:1:length(y)] ;
yfinal =y(length(y)) ;
elseif (length(varargin)==2)&(typeof(varargin(2)) == 'constant')
//y_temp = varargin(1) ;
//y = y_temp(1:ns,k,j) ;
t = varargin(2) ;
if (length(t) ~= length(y)) then
error("The input arguments Y and T must have compatible sizes.") ;
end
yfinal = y(length(y))
elseif (length(varargin)==3)&(typeof(varargin(2)) == 'constant')&(typeof(varargin(3)) == 'constant')
//y_temp = varargin(1) ;
//y = y_temp(1:ns,k,j) ;
t = varargin(2) ;
if length(t)~= length(y)
error('The input arguments Y and T must have compatible sizes.');
end
yfinal = varargin(3) ;
if size(yfinal) ~= [ny, nu]
error('The input arguments Y and YFINAL must have compatible sizes.');
end
yfinal = yfinal(k,j) ;
elseif (length(varargin)==3)&(typeof(varargin(2)) == 'string')&(typeof(varargin(3)) == 'constant')
t = [1:1:length(y)] ;
yfinal =y(length(y)) ;
elseif (length(varargin)>3)&(typeof(varargin(2))=='constant')&(typeof(varargin(3)) == 'string')
t = varargin(2) ;
if (length(t) <> length(y)) then
error("The input arguments Y and T must have compatible sizes.") ;
end
yfinal = y(length(y))
elseif (length(varargin)>3)&(typeof(varargin(2))=='constant')&(typeof(varargin(3)) == 'constant')
t = varargin(2) ;
if length(t)~= length(y)
error('The input arguments Y and T must have compatible sizes.');
end
yfinal = varargin(3) ;
if size(yfinal) ~= [ny, nu]
error('The input arguments Y and YFINAL must have compatible sizes.');
end
yfinal = yfinal(k,j) ;
else
error('Wrong type of input arguments') ;
end
[RiseTime(k,j), SettlingMin(k,j), SettlingMax(k,j),RiseTimeHigh(k,j),RiseTimeLow(k,j)]= risetime(y,t,yfinal,RiseTimeLims)
SettlingTime(k,j) = settlingtime(y,t,yfinal,SettlingTimeThreshold)
[Overshoot(k,j), Undershoot(k,j)] = overshoot_and_undershoot(y,t,yfinal)
[Peak(k,j), PeakTime(k,j)] = peak(y,t,yfinal)
end
end
elseif (typeof(varargin(1))== "rational" ) & (varargin(1).dt == 'c')
// System is in rational form and continuous-time
sys = varargin(1) ;
[m n] = size(sys) ;
for k = 1:1:m
for j = 1:1:n
sys = varargin(1) ;
sys = sys(k,j) ;
if real(roots(sys.den))< 0
// system is stable
y_temp = csim('step',0:1:1010,sys) ;
yfinal = horner(sys,0.00001)
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit s -> 0, C(z)
for i = [1:1:1000]
temp = y_temp(i:i+9) ;
if (abs(temp- yfinal) < = 0.0001)
break
// Index of settling point
end
end
// Simulate y only till the system settles
t = 0:(i+9)/10000:i+9 ;
y = csim('step',t,sys) ;
else
// system is unstable
t = 0:(2.30*25)/((max(real(roots(sys.den)))+0.001)*10000):(2.30*25)/(max(real(roots(sys.den)))+0.001) ;
// t only till the value of y reaches around 10^25
y = csim('step',t,sys) ;
yfinal = sign(horner(sys,0.00001))*(%inf) ;
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit s -> 0, C(s)
end
[RiseTime(k,j), SettlingMin(k,j), SettlingMax(k,j),RiseTimeHigh(k,j),RiseTimeLow(k,j)]= risetime(y,t,yfinal,RiseTimeLims)
SettlingTime(k,j) = settlingtime(y,t,yfinal,SettlingTimeThreshold)
[Overshoot(k,j), Undershoot(k,j)] = overshoot_and_undershoot(y,t,yfinal)
[Peak(k,j), PeakTime(k,j)] = peak(y,t,yfinal)
end
end
elseif (typeof(varargin(1))=="state-space") & (varargin(1).dt == 'c')
// System is in state-space form and continuous-time
sys_ss = varargin(1) ;
sys = ss2tf(sys_ss)
// Converting to transfer function form
[m n] = size(sys) ;
for k = 1:1:m
for j = 1:1:n
sys = ss2tf(sys_ss) ;
sys = sys(k,j) ;
if real(roots(sys.den))< 0
// system is stable
y_temp = csim('step',0:1:1010,sys) ;
yfinal = horner(sys,0.00001)
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit s -> 0, C(s)
for i = [1:1:1000]
temp = y_temp(i:i+9) ;
if (abs(temp- yfinal) < = 0.0001)
break
end
end
t = 0:(i+9)/10000:i+9 ;
y = csim('step',t,sys) ;
else
// system is unstable
t = 0:(2.30*25)/((max(real(roots(sys.den)))+0.001)*10000):(2.30*25)/(max(real(roots(sys.den)))+0.001) ;
// t only till the value of y reaches around 10^25
y = csim('step',t,sys) ;
yfinal = sign(horner(sys,0.00001))*(%inf) ;
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit s -> 0, C(s)
end
[RiseTime(k,j), SettlingMin(k,j), SettlingMax(k,j),RiseTimeHigh(k,j),RiseTimeLow(k,j)]= risetime(y,t,yfinal,RiseTimeLims)
SettlingTime(k,j) = settlingtime(y,t,yfinal,SettlingTimeThreshold)
[Overshoot(k,j), Undershoot(k,j)] = overshoot_and_undershoot(y,t,yfinal)
[Peak(k,j), PeakTime(k,j)] = peak(y,t,yfinal)
end
end
elseif (typeof(varargin(1))=='rational') & (varargin(1).dt ~='c')
// System is rational and discrete
sys = varargin(1) ;
[m n] = size(sys) ;
for k = 1:1:m
for j = 1:1:n
sys = varargin(1) ;
sys = sys(k,j) ;
//Calculation of sampling time
if sys.dt == 'd'
sampling_time = 1 ;
elseif sys.dt == []
error('Not a valid system') ;
else
sampling_time = sys.dt ;
end
if abs(roots(sys.den))< 1
// system is stable
yfinal = horner(sys,1.0000)
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit z -> 1, C(z)
y = flts(ones(1,100000),sys) ;
t = [0:sampling_time:(length(y)-1)*sampling_time] ;
else
// system is unstable
y = flts(ones(1,100000),sys) ;
t = [0:sampling_time:(length(y)-1)*sampling_time] ;
yfinal = sign(horner(sys,1.0000))*(%inf) ;
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit z -> 1, C(z)
end
[RiseTime(k,j), SettlingMin(k,j), SettlingMax(k,j),RiseTimeHigh(k,j),RiseTimeLow(k,j)]= risetime(y,t,yfinal,RiseTimeLims)
SettlingTime(k,j) = settlingtime(y,t,yfinal,SettlingTimeThreshold)
[Overshoot(k,j), Undershoot(k,j)] = overshoot_and_undershoot(y,t,yfinal)
[Peak(k,j), PeakTime(k,j)] = peak(y,t,yfinal)
end
end
elseif (typeof(varargin(1))=="state-space") & (varargin(1).dt ~= 'c')
// System is in state-space form and discrete
sys_ss = varargin(1) ;
sys = ss2tf(sys_ss)
[m n] = size(sys) ;
for k = 1:1:m
for j = 1:1:n
sys = ss2tf(sys_ss) ;
sys = sys(k,j) ;
// Calculation of sampling time
if sys.dt == 'd'
sampling_time = 1 ;
elseif sys.dt == []
error('Not a valid system') ;
else
sampling_time = sys.dt ;
end
if abs(roots(sys.den))< 1
// system is stable
yfinal = horner(sys,1.0000);
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit z -> 1, C(z)
y = flts(ones(1,100000),sys) ;
t = [0:sampling_time:(length(y)-1)*sampling_time] ;
else
// system is unstable
y = flts(ones(1,100000),sys) ;
t = [0:sampling_time:(length(y)-1)*sampling_time] ;
yfinal = sign(horner(sys,1.0000))*(%inf) ;
//Final Value theorem for calculation of yfinal
//In case of step input final value = limit z -> 1, C(z)
end
[RiseTime(k,j), SettlingMin(k,j), SettlingMax(k,j),RiseTimeHigh(k,j),RiseTimeLow(k,j)]= risetime(y,t,yfinal,RiseTimeLims)
SettlingTime(k,j) = settlingtime(y,t,yfinal,SettlingTimeThreshold)
[Overshoot(k,j), Undershoot(k,j)] = overshoot_and_undershoot(y,t,yfinal)
[Peak(k,j), PeakTime(k,j)] = peak(y,t,yfinal)
end
end
else
error('Wrong type of input argument') ;
end
disp(RiseTime,'RiseTime:') ;
disp(SettlingTime,'SettlingTime:') ;
disp(SettlingMin,'SettlingMin:') ;
disp(SettlingMax,'SettlingMax:') ;
disp(Overshoot,'Overshoot:') ;
disp(Undershoot,'Undershoot:') ;
disp(Peak,'Peak:') ;
disp(PeakTime,'PeakTime:') ;
endfunction