% Exam Task
% From response to model structure

clear('all')
s=tf('s');

dt=0.01;    % Sampling time
t=0:dt:40;  % Time horizon
N=length(t);
R=ones(N,1);  % Unit step reference

% System parameters
K=2; % Gain 
tz=-10; % Zero
T1=5; T2=3; % Time constants
tau=5; % Time delay

% Task 1
hp1=K/(1+T1*s); % 1st order model

figure(1)
Y=lsim(hp1,R,t), grid
plot(t,Y), grid, title('Output response: y'), ylabel('y')
xlabel('Time  [sec]'),
print -dpdf fig1

% Task 2
hp2=K*exp(-tau*s)/s^2; % IPTD plant

figure(2)
Y=lsim(hp2,R,t), grid
plot(t,Y), grid, title('Output response: y'), ylabel('y')
xlabel('Time  [sec]')
print -dpdf fig2

% Task 3
T=10; xi=0.2;
hp3=K*(1+tz*s)*exp(-tau*s)/((1+T*s)*(1+5*s));

figure(3)
Y=lsim(hp3,R,t), grid
plot(t,Y), grid, title('Output response: y'), ylabel('y')
xlabel('Time  [sec]')
print -dpdf fig3
Y3=Y; t3=t;

% Task 4
t0=1; xi=0.2;
hp3=K*exp(-tau*s)/(t0^2*s^2+2*t0*xi*s+1);

figure(4)
Y=lsim(hp3,R,t), grid
plot(t,Y), grid, title('Output response: y'), ylabel('y')
xlabel('Time  [sec]')
print -dpdf fig4

%%%%%%%%%%%%%%% Plot the input u(t)
figure(5)
Y=lsim(hp3,R,t), grid
plot(t,R), grid, title('Input signal: u'), ylabel('u')
xlabel('Time  [sec]')
print -dpdf fig5