%pfc_feed_forward2
% true feed forward
clear all
close all
tech=1;tf=1000;
w=1:1:tf;
MV=zeros(1,tf);CV=MV;Pert=MV;pertf=MV;sp=MV;
spert=MV;sm=MV;spertm=sm;
%process
K1= 1; tau1=30 ;a1=exp(-tech/tau1);b1=1-a1;
K2= 2; tau2=30 ;a2=exp(-tech/tau2);b2=1-a2;%disturbance
kf=1; tauf =5; af=exp(-tech/tauf);bf=1-af;
TRBF=70;
h=1;b1h=1-a1^h;
lh=1-exp(-tech*3*h/TRBF);
fl=input('fl =1 with feed forward fl=0 without = ');

K2m=K2*1;

rp=5;%process delay
rt=10;%disturbance delay> process delay
for ii =2+rt+rp:1:tf
   %disturbance
   if ii> 150; Pert(ii)=30;end
   if ii> 500; Pert(ii)=1*(30+20*sin(2*pi*ii/200));end
   pertf(ii)=pertf(ii-1)*af+bf*Pert(ii-1);%disturbance input
   spert(ii)= spert(ii-1)*a2+b2*pertf(ii-1-rt)*K2;%disturbance output
   %process
   sp(ii)=sp(ii-1)*a1+b1*MV(ii-1-rp)*K1;
   CV(ii)=sp(ii)+spert(ii);
   %model of perturbation
   sm(ii)=sm(ii-1)*a1+b1*MV(ii-1)*K1;
   spertm(ii)= spertm(ii-1)*a2+b2*pertf(ii-1-rt)*K2m;
   %process  increment
   spred =CV(ii)+sm(ii)-sm(ii-rp);
   dpert=(pertf(ii-rt+rp)*K2m-spertm(ii))*b2; %delay comp
   %control
   d=( 100-spred)*lh +sm(ii)*b1h-dpert*fl;
   MV(ii)= d/(K1*b1h);
end
if fl==1;plot(w,CV, 'r',w,MV, 'k',w,pertf,'g');else;
 plot(w,CV, 'r',w,MV, 'k',w,pertf,'g')  ;end
hold on
grid on
zoom on
text (410 ,30, 'PERT')
text (410, 70, 'MV')
text (410 ,110, 'CV')
text (900, 10, 'sec')
if fl==1 ;title( ' CONTROL WITH FEED FORWARD /-');end
if fl==0 ;title( ' CONTROL WITHOUT FEED FORWARD/-- ');end


