function []= servacomp(Fi0,R,L,C,P, roz,ATVpredp,ATVpredm,ATVzadp,ATVzadm)
% Vypocte odchylky desky cykliky

%R = 12;
%L = 25;
%P = 6;
%Fi0 = 13;
%roz = 30;
Fipred=linspace(Fi0-roz-ATVpredm,Fi0,roz+1);
Fip=linspace(Fi0,Fi0+roz+ATVpredp,roz+1);
Fipred=[Fipred Fip(2:length(Fip))];
Fizad=linspace(Fi0-roz-ATVzadm,Fi0,roz+1);
Fip=linspace(Fi0,Fi0+roz+ATVzadp,roz+1);
Fizad=[Fizad Fip(2:length(Fip))];
FiSk = [Fi0-roz:1:Fi0+roz];
Fi=FiSk;
size=length(Fi);
sinFi = sin(pi*Fi/180);
cosFi = cos(pi*Fi/180);
sinFi0 = sin(pi*Fi0/180);
cosFi0 = cos(pi*Fi0/180);
ypaka = R*sinFi;
xpaka = R*cosFi;
ypaka0 = R*sinFi0;
xpaka0 = R*cosFi0;
%Vypocet opravdoveho kolektivu
Y0 = ypaka - ypaka0;
Y = L+ypaka;

Fi=Fizad;
size=length(Fi);
sinFi = sin(pi*Fi/180);
cosFi = cos(pi*Fi/180);
sinFi0 = sin(pi*Fi0/180);
cosFi0 = cos(pi*Fi0/180);
ypaka = R*sinFi;
xpaka = R*cosFi;
ypaka0 = R*sinFi0;
xpaka0 = R*cosFi0;
Yzad0 = (L.^2-(R-xpaka).^2).^0.5+ypaka-(L.^2-(R-xpaka0).^2).^0.5-ypaka0;
Yzad = (L.^2-(R-xpaka).^2).^0.5+ypaka;

Fi=Fipred;
size=length(Fi);
sinFi = sin(pi*Fi/180);
cosFi = cos(pi*Fi/180);
sinFi0 = sin(pi*Fi0/180);
cosFi0 = cos(pi*Fi0/180);
ypaka = R*sinFi;
xpaka = R*cosFi;
ypaka0 = R*sinFi0;
xpaka0 = R*cosFi0;
Ypred0 = (L.^2-(xpaka-P).^2).^0.5+ypaka-ypaka0-(L.^2-(xpaka0-P).^2).^0.5;
%Ypred = (L.^2-(xpaka-P).^2).^0.5+ypaka+L-(L.^2-(R*cos(pi*Fi0/180)-P).^2).^0.5;
Ypred = (L.^2-(xpaka-P).^2).^0.5+ypaka+L;

Fi=FiSk;
size=length(Fi);
sinFi = sin(pi*Fi/180);
cosFi = cos(pi*Fi/180);
sinFi0 = sin(pi*Fi0/180);
cosFi0 = cos(pi*Fi0/180);
ypaka = R*sinFi;
xpaka = R*cosFi;
ypaka0 = R*sinFi0;
xpaka0 = R*cosFi0;
%linearizovane smernice kolektivu
alfa0=pi*R/180;
alfa=(pi*R*cos((pi*Fi0)/180))/180;
alfazad=(pi*R*cos((pi*Fi0)/180))/180 - (pi*R*sin((pi*Fi0)/180)*(R - R*cos((pi*Fi0)/180)))/(180*(L^2 - (R - R*cos(1/180*Fi0*pi))^2)^(1/2));
alfapred=(pi*R*cos((pi*Fi0)/180))/180 - (pi*R*sin((pi*Fi0)/180)*(P - R*cos((pi*Fi0)/180)))/(180*(L^2 - (P - R*cos(1/180*Fi0*pi))^2)^(1/2));
disp('smernice linerizace:')
disp(['idealni (pri Fi0=0): ' num2str(alfa0) '   idealni (pri Fi0=' num2str(Fi0) '): ' num2str(alfa) '   idealni zad: ' num2str(alfazad) '   idealni pred: ' num2str(alfapred)] )
%linearizovane prubehy kolektivu
Yideal0 = alfa0.*(Fi-Fi0);
%Yideal = alfa.*Fi+R*sin(pi*Fi0/180)-Fi0*alfa-ypaka0;
Yideal = alfa.*(Fi-Fi0);
%Yidealzad = alfazad.*Fi+(L.^2-(R-R*cos(pi*Fi0/180)).^2).^0.5+R*sin(pi*Fi0/180)-Fi0*alfazad;
Yidealzad = alfazad.*(Fi-Fi0);
%Yidealpred = alfapred.*Fi+(L.^2-(R*cos(pi*Fi0/180)-P).^2).^0.5+R*sin(pi*Fi0/180)+L-(L.^2-(R*cos(pi*Fi0/180)-P).^2).^0.5-Fi0*alfapred;
Yidealpred = alfapred.*(Fi-Fi0);
%Statisticke odchylky mezi sebou
xY0=sum(Y0-Yideal0)/size;
odY0 = (sum((Y0-Yideal0-xY0).^2)/size).^0.5;
disp(['Y-Yideal0 chyba ' num2str(xY0) '  ' num2str(odY0) ' min' num2str(min(Y0-Yideal0)) ' max' num2str(max(Y0-Yideal0)) ' dmax' num2str(max(Y0-Yideal0)-min(Y0-Yideal0))])
xYzad0=sum(Yzad0-Yideal0)/size;
odYzad0 = (sum((Yzad0-Yideal0-xYzad0).^2)/size).^0.5;
disp(['Yzad-Yideal0 chyba ' num2str(xYzad0) '  ' num2str(odYzad0) ' min' num2str(min(Yzad0-Yideal0)) ' max' num2str(max(Yzad0-Yideal0)) ' dmax' num2str(max(Yzad0-Yideal0)-min(Yzad0-Yideal0))])
xYpred0=sum(Ypred0-Yideal0)/size;
odYpred0 = (sum((Ypred0-Yideal0-xYpred0).^2)/size).^0.5;
disp(['Ypred-Yideal0 chyba ' num2str(xYpred0) '  ' num2str(odYpred0) ' min' num2str(min(Ypred0-Yideal0)) ' max' num2str(max(Ypred0-Yideal0)) ' dmax' num2str(max(Ypred0-Yideal0)-min(Ypred0-Yideal0))])
xY=sum(Y0-Yideal)/size;
odY = (sum((Y0-Yideal-xY).^2)/size).^0.5;
disp(['Y-Yideal chyba ' num2str(xY) '  ' num2str(odY) ' min' num2str(min(Y0-Yideal)) ' max' num2str(max(Y0-Yideal)) ' dmax' num2str(max(Y0-Yideal)-min(Y0-Yideal))])
xYzad=sum(Yzad0-Yideal)/size;
odYzad = (sum((Yzad0-Yideal-xYzad).^2)/size).^0.5;
disp(['Yzad-Yideal chyba ' num2str(xYzad) '  ' num2str(odYzad) ' min' num2str(min(Yzad0-Yideal)) ' max' num2str(max(Yzad0-Yideal)) ' dmax' num2str(max(Yzad0-Yideal)-min(Yzad0-Yideal))])
xYpred=sum(Ypred0-Yideal)/size;
odYpred = (sum((Ypred0-Yideal-xYpred).^2)/size).^0.5;
disp(['Ypred-Yideal chyba ' num2str(xYpred) '  ' num2str(odYpred) ' min' num2str(min(Ypred0-Yideal)) ' max' num2str(max(Ypred0-Yideal)) ' dmax' num2str(max(Ypred0-Yideal)-min(Ypred0-Yideal))])
xYmezi=sum(Ypred0-Yzad0)/size;
odYmezi = (sum((Ypred0-Yzad0-xYmezi).^2)/size).^0.5;
disp(['Ypred-Yzad chyba ' num2str(xYmezi) '  ' num2str(odYmezi) ' min' num2str(min(Ypred0-Yzad0)) ' max' num2str(max(Ypred0-Yzad0)) ' dmax' num2str(max(Ypred0-Yzad0)-min(Ypred0-Yzad0))])
%vykresleni grafu
figure()
plot(Fi,Y0,Fi,Yzad0,Fi,Ypred0,Fi,Yideal,Fi,Yidealzad,Fi,Yidealpred,Fi,Yideal0)
title('Pohyb kolektivu');
xlabel('Fi');
ylabel('K');
legend('Zjednoduseny kolektiv','Zadni','Predni','Linearizovany zjednoduseny','Linearizovany zadni','Linearizovany predni','Linearni zjednoduseny pri Fi=0');
figure()
plot(Fi,Y0-Yideal,Fi,Yzad0-Yideal,Fi,Ypred0-Yideal)
title('Odchylky kolektivu(zaklad linearizovany zjednoduseny kol.)');
xlabel('Fi');
ylabel('chyba K');
legend('Zjednoduseny kolektiv','Zadni','Predni');
figure()
plot(Fi,Y0-Yidealpred,Fi,Yzad0-Yidealpred,Fi,Ypred0-Yidealpred)
title('Odchylky kolektivu(zaklad linearizovany predni)');
xlabel('Fi');
ylabel('chyba K');
legend('Zjednoduseny kolektiv','Zadni','Predni');
figure()
plot(Fi,Yzad0-Ypred0,Fi,Y0-Yzad0,Fi,Y0-Ypred0)
title('Odchylky kolektivu mezisebou');
xlabel('Fi');
ylabel('chyba K');
legend('Chyba mezi zadni a predni','Zadni ku linearizovany kol.','Predni ku linearizovany kol.');
%Y0((length(Fi)-1)/2)
%Yzad0((length(Fi)-1)/2)
%Ypred0((length(Fi)-1)/2)
%Yideal((length(Fi)-1)/2)
%Yideal0((length(Fi)-1)/2)
%Yidealzad((length(Fi)-1)/2)
%Yidealpred((length(Fi)-1)/2)
%vykresleni animace

Fi=Fizad;
size=length(Fi);
sinFi = sin(pi*Fi/180);
cosFi = cos(pi*Fi/180);
sinFi0 = sin(pi*Fi0/180);
cosFi0 = cos(pi*Fi0/180);
ypaka = R*sinFi;
xpaka = R*cosFi;
ypaka0 = R*sinFi0;
xpaka0 = R*cosFi0;

figure()
axis([-20 25 -10 35])
hold on
for k=1:5:length(Fi),
    plot([0 xpaka(k) R R-C R-C 0],[0 ypaka(k) Yzad(k) Yzad(k) 0 0])
end
title('Animace zadni');


Fi=Fipred;
size=length(Fi);
sinFi = sin(pi*Fi/180);
cosFi = cos(pi*Fi/180);
sinFi0 = sin(pi*Fi0/180);
cosFi0 = cos(pi*Fi0/180);
ypaka = R*sinFi;
xpaka = R*cosFi;
ypaka0 = R*sinFi0;
xpaka0 = R*cosFi0;
figure()
Ypred = (L.^2-(xpaka-P).^2).^0.5+ypaka;
axis([-20 25 -10 35])
hold on
for k=1:5:length(Fi),
    plot([0 xpaka(k) R-P R-C-P R-C-P 0],[0 ypaka(k) Ypred(k) Ypred(k) 0 0])
end
title('Animace predni');