Code covered by the BSD License

# Optimal reduced-order discrete-time LQG design

### Gerard Van Willigenburg (view profile)

16 May 2008 (Updated )

Solution of the SDOPE by repeated forward and backward iteration

[osc,ldif,sdif]=detosc(osc,trps,l,ls,f);
```% DETOSC : detect oscillation during iteration
%          based on comparison of differences
%          between maxima and minima of long and short term
%
function [osc,ldif,sdif]=detosc(osc,trps,l,ls,f);

if nargin<3; f=0.75; l=100; ls=10;
elseif nargin<4; ls=10; f=0.75;
elseif nargin<5; f=0.75;
end;
if l<ls; error('  ls<l'); end;
f=min(1,f); f=max(f,0.1);

nt=length(trps);
if nt>=l;
trps=trps(nt-l+1:nt);

% test on 10 consequtive sign changes
h=1; k=1;
s=sign(trps(l-k+1)-trps(l-k)); k=k+1;
while k<min(l-1,10) & h==1
sn=sign(trps(l-k+1)-trps(l-k)); k=k+1;
if sn==-s; s=sn; else; h=0; end;
end
%  if osc==1; disp('ten times'); end;

% test on long term and short term
% differences between maxima and minima

if h==1; osc=10;
else
ldif=max(trps)-min(trps);
sdif=max(trps(l-ls+1:l))-min(trps(l-ls+1:l));
if sdif>f*ldif; osc=osc+1; else; osc=0; end;
end
else;
osc=0;
end;
```