%This script is used to generate bifurication diagram. 
%
% Written by: Knut Åge Meland
% email: knutagemeland@gmail.com
%%

clear all
close all
clc

%%
load bi6.lvm


t=bi6(:,1);
q_air=bi6(:,3);
q_liq=bi6(:,4);
P1=bi6(:,5);
P2=bi6(:,6);
P3=bi6(:,7);
setpoint=bi6(:,11);
valve=bi6(:,12);
%% Filter_data to be used building bifurication diagram
P1f= Butterfilter_4(P1);
P2f= Butterfilter_4(P2);
P3f= Butterfilter_4(P3);
%% plot

figure(1)
plot(t,q_air)
title('Air flowrate')
xlabel('Time[s]')
ylabel('Flowrate[l/min]')


figure(2)
plot(t,q_liq)
title('Liquid flowrate')
xlabel('Time[s]')
ylabel('Flowrate[l/min]')

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

%end fitting


figure(3)
plot(t,P1)
title('Pressure at the mixingpoint (P1)')
xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')


figure(4)
plot(t,P2)
title('Pressure topside (P2)')
xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')


figure(5)
plot(t,P3)
title('Pressure in the buffertank (P3)')
xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')

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

poly(2)
