% Script to generate results for Exercise 8
% Johannes Jaeschke
% 10.10.2012
clear all
close all 
clc
load unknownProcess %Load the process model to be used in Simulink

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Instructions:                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The simulink model is in the file "PIcontrolProcess.mdl" It can be used
% for both, P and PI control. 
% to simulate the file, simply run this script in matlab.

% You are asked to 
% 1. a.) Set the process into P control and adjust the Kc such that you 
% have constant oscillations. Using this value of K and the period
% between two peaks, you determine controller settings using the Ziegler 
% nichols rules.
% 1  b.) Show a step response for the sytem controlled by PI
% control using the Ziegler Nichols settings.

% 2. a. )Set the process into P control mode and adjust Kc such that you 
% get an overshoot and an undershoot (no persistent oscillations).
% Use Shams' method to derive a simple first-order+dead-time model, and
% apply the SIMC tuning rules to it.
% 2. b.) Show a step response with the tuning rules obtained by this way.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%           IMPORTANT: Switching between P and PI mode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To change between P and PI control, open the 
% simulinkfile and double-click on the switch-block.




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                       Adjusting controller settings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%To change the controller parameters used, specify the control parameters
%here (the ones given here are just randomly chosen).:
Kc=-0.3291; %(This is for P AND PI control)
taui=54.0388; %(This is ONLY for PI control)

%% Simulate the process
% This command simulates the processwith the controller parameters given 
% above:
sim('PIcontrolProcess.mdl')

%% plot the results results
fig1=figure;
subplot(2,1,1)
plot(time,y,time,yset)
xlabel('time')
ylabel('y,y_s')
legend('y','y_s')
axis([0,time(end) 0 2])
subplot(2,1,2)
plot(time,u)
xlabel('time')
ylabel('u')

%% export the figure to an eps file
title='Randomname'; % set the figure title here
exportfig(fig1,title, 'width',6, 'fontmode','fixed', 'fontsize',12, 'Color', 'cmyk');
return
