%Section 5.7 Non-causal controllers
%Original from  bjorngl@smekk.tmih.no Mon Jul 31 12:52:32 1995
%
% *****************************************************
% *** Matlab code to check example on page 181/182. ***
% *****************************************************
% 
% Modified by: Kjetil Havre, 3/12-1995.
%

clear all
close all

z      = 1;		% Constant z in transfer function.
t_step = 0;		% Unit step at time = t_step.
Gnum   = [-1 z];	% Numerator of transfer function. 
Gden   = [ 1 z];	% Denominator of transfer function.

t = [-5:0.01:5];	% Time vector.
n = length(t);		% Number of time steps.
m = find(t==t_step);	% Find index at step, i.e. step at t(m).

% GENERATE REFERENCE.
r = [zeros(1,m) ones(1,n-m)]; 

% GENERATE u FROM UNSTABLE AND NON-CAUSAL CONTROLLERS.
u_unstable  = [zeros(1,m) 1-2*exp(z*(t(m+1:n)-t(m)))];
u_noncausal = [2*exp(z*(t(1:m)-t(m))) ones(1,n-m)];
u_real=r;

% SIMULATE USING lsim (in Control Systems Toolbox).
[y_unstable,x]  = lsim(Gnum,Gden,u_unstable,t);
[y_noncausal,x] = lsim(Gnum,Gden,u_noncausal,t);
[y_real,x] = lsim(Gnum,Gden,u_real,t);

% PLOT RESULTS.
figure(1)
[Pp] = get(gcf, 'PaperPosition');
set(gcf, 'Units','inches')
set(gcf, 'PaperPosition', [Pp(1) Pp(2) Pp(3) Pp(4)/1.2]);
set(gcf, 'Position', [1 7.0 Pp(3) Pp(4)/1.2]);


subplot(322)
plot(t,y_unstable)
axis([min(t) max(t) -1 1.1])
title('Output unstable controller:')
xlabel('')

subplot(321)
plot(t,u_unstable)
axis([min(t) max(t) -10 1])
title('Input unstable controller:')
xlabel('')

subplot(324)
plot(t,y_noncausal)
axis([min(t) max(t) -1 1.1])
title('Output non-causal controller:')
xlabel('')

subplot(323)
plot(t,u_noncausal)
axis([min(t) max(t) -0.1 2.1])
title('Input non-causal controller:')
xlabel('')

subplot(326)
plot(t,y_real)
axis([min(t) max(t) -1 1.1])
title('Output practical controller:')
xlabel('Time')

subplot(325)
plot(t,u_real)
axis([min(t) max(t) -0.1 2.1])
title('Input practical controller:')
xlabel('Time')

[Ap] = get(gca, 'Position');
