% Introduction to Simulink
% TKP4140, Process Control
% made with matlab r2018a
% Aug 27, 2019

clear                                                     % Clear workspace
clc                                                  % Clear command window
close all                                               % Close all figures

%% Plotting preamble that makes nice figures
set(0,'DefaultTextFontName','Times',...
'DefaultTextFontSize',16,...
'DefaultAxesFontName','Times',...
'DefaultAxesFontSize',16,...
'DefaultLineLineWidth',2,...
'DefaultLineMarkerSize',7.75)

%% Define PI-controller parameters
Kc=0.1;                                                   % Controller gain
tauI=20;                                           % Integral time constant

%% Run Simulink file
sim('Tank')         % Matlab version:R2018a
%sim('Tank2014')    % Matlab version: R2014b

% results = sim('Tank') % Matlab version: r2019a

%% Plotting begins here
% Here we are trying to provide different ways to plot

%doc plot % use doc followed by function name to get Matlab documentation 

figure(1)    % always create a figure window when plotting. otherwise Matlab overlaps or delets previous plots
    plot(T1) %T1 is saved as time series
    
    %plot(result.T1) % Matlab version r2019a    
 
    
% Exporting the plots for Latex documents
% readjust before exporting the eps
% print('-depsc2','-r600','fig1.eps') % recommended
% print('-dpdf','-r600','fig1.pdf') % not recommended, but just for knowledge
 
figure(2)
    plot(time, T)  
    %Then you can add more
    %plot(time, T,'red')
    %plot(time, T, '--')
    %plot(time, T, 'k--o')
    %plot(time, T, 'Color', [0 0.5 0]) % define rgb color
    
    % Exporting the plots for Latex documents
% readjust before exporting the eps
% print('-depsc2','-r600','fig2.eps') % recommended
% print('-dpdf','-r600','fig2.pdf') % not recommended, but just for knowledge

figure(3)
    plot(time,u,time,ys,time,T,time,d)
    title('Step in y_s and d') % anything in '' is a string (text)
    xlabel('time [s]')
    ylabel('')
    legend('u','y_s', 'T','d')
 
% Exporting the plots for Latex documents
% readjust before exporting the eps
% print('-depsc2','-r600','fig3.eps') % recommended
% print('-dpdf','-r600','fig3.pdf') % not recommended, but just for knowledge
    
figure(4) % here we are plotting 2 plots in the save figure
    subplot(2,1,1)
        plot(time,T)
        grid
        axis([0 2000 -0.5 1.5])
        xlabel('time [s]')
        ylabel('Output')
    subplot(2,1,2)
        plot(time,ys)
        grid
        axis([0 2000 -0.5 1.5])
        xlabel('time [s]')
        ylabel('Setpoint')

% Exporting the plots for Latex documents
% readjust before exporting the eps
% print('-depsc2','-r600','fig4.eps') % recommended
% print('-dpdf','-r600','fig4.pdf') % not recommended, but just for knowledge
        
        
%% Adding text to your plots

% text('Interpreter','latex','String','$1/e$',...
%'Position',[1000 2])

%text(1500,2,'\itc_B')
%text(1700,2,'\itc_A')