%This script is used to generate the 100% basis plot. 
%
% Written by: Knut Åge Meland
% email: knutagemeland@gmail.com
%
% Folders needs to be created before the figures can be saved.
% The script uses the custom globalfunction: save2pdf

%%
clear all
close all
clc

set(0,'defaultLineLineWidth', 2)
set(0,'DefaultAxesFontSize',14)
%%
load basis_3_3_100.lvm


t=basis_3_3_100(:,1);
q_air=basis_3_3_100(:,3);
q_liq=basis_3_3_100(:,4);
P1=basis_3_3_100(:,5);
P2=basis_3_3_100(:,6);
P3=basis_3_3_100(:,7);
setpoint=basis_3_3_100(:,11);
valve=basis_3_3_100(:,12);
%plot

figure(1)
plot(t,q_air)
hold on
plot(t,q_liq,'--r')

%fitting polynom
k=1;
y=0;
poly= polyfit(t,q_liq,k);
for i=1:k+1
    y=y+poly(i).*t.^(k+1-i);
end
plot(t, y, ':g')


xlabel('Time[s]')
ylabel('Flow rate[l/min]')
legend('Air flow rate', 'Liquid flow rate', 'Average liquid flow rate');
axis([0, 100, 1.5, 7]);
 
hold off

%end fitting

figure(3)
plot(t,P1,'-b')
hold on

plot(t,P2,'--r')
plot(t,P3,':g')

hold off

xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')
legend('Pressure at the mixingpoint (P1)', 'Pressure at the top (P2)', 'Pressure in the buffertank (P3)');
axis([0, 100, 0, 60]);

figure(6)
plot(t,valve)
title('Valve position')
xlabel('time[s]')
ylabel('Valve position[%]')


%saving figures

for fig=[1,3];
    dir = 'c:\users\knutage\desktop\Master oppgave\Master_source\bilder\matlabfig\basis_3_3_100\';
    filename = ['figure', num2str(fig),'.jpg'];
    saveas(figure(fig), [dir filename]);
    filename = ['figure', num2str(fig),'.pdf'];
    save2pdf([dir, filename]);
end

