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

%R = 12;
%L = 25;
%P = 6;
%Fi0 = 13;
%roz = 30;
%Vypocet zbyvajciho rozmeru
Spred=R*sin(pi*Fi0/180)+(L^2-(R*cos(pi*Fi0/180)+P-C)^2)^0.5;
%Spred=(L^2-(R+P-C)^2)^0.5;
Szad=L+R*sin(pi*Fi0/180);
Ppred=P;
Pzad=C-R*cos(pi*Fi0/180);
disp(['Spred = ' num2str(Spred) ' Szad = ' num2str(Szad) ' Ppred = ' num2str(Ppred) ' Pzad = ' num2str(Pzad)])
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=Fipred;
size=length(Fi);
sinFi = sin(pi*Fi/180);
cosFi = cos(pi*Fi/180);
sinFi0 = sin(pi*Fi0/180);
cosFi0 = cos(pi*Fi0/180);
tanFi=tan(1/2.*Fi*pi/180);
tanFi0=tan(1/2.*Fi0*pi/180);
ypaka0 = R*sinFi0;
ypaka = R*sinFi;
xpaka = R*cosFi;
%Vypocet predni cykliky
S=Spred;
%cyklUpred = 2.*atan(((- C.^4*tan(1/2.*Fi*pi/180).^4 - 2.*C.^4.*tan(1/2.*Fi*pi/180).^2 - C.^4 + 2.*C.^2.*L.^2.*tan(1/2.*Fi*pi/180).^4 + 4.*C.^2.*L.^2.*tan(1/2.*Fi*pi/180).^2 + 2.*C.^2.*L.^2 + 2.*C.^2.*P.^2.*tan(1/2.*Fi*pi/180).^4 + 4.*C.^2.*P.^2.*tan(1/2.*Fi*pi/180).^2 + 2.*C.^2.*P.^2 - 4.*C.^2.*P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^2.*S.^2.*tan(1/2.*Fi*pi/180).^4 - 4.*P.^2.*S.^2.*tan(1/2.*Fi*pi/180).^2 - 2.*P.^2.*S.^2 + 4.*P.*R.^3.*tan(1/2.*Fi*pi/180).^4 - 4.*P.*R.^3 - 16.*P.*R.^2.*S.*tan(1/2.*Fi*pi/180).^3 + 16.*P.*R.^2.*S.*tan(1/2.*Fi*pi/180) + 4.*P.*R.*S.^2.*tan(1/2.*Fi*pi/180).^4 - 4.*P.*R.*S.^2 - R.^4.*tan(1/2.*Fi*pi/180).^4 - 2.*R.^4.*tan(1/2.*Fi*pi/180).^2 - R.^4 + 8.*R.^3.*S.*tan(1/2.*Fi*pi/180).^3 + 8.*R.^3.*S.*tan(1/2.*Fi*pi/180) - 2.*R.^2.*S.^2.*tan(1/2.*Fi*pi/180).^4 - 20.*R.^2.*S.^2.*tan(1/2.*Fi*pi/180).^2 - 2.*R.^2.*S.^2 + 8.*R.*S.^3.*tan(1/2.*Fi*pi/180).^3 + 8.*R.*S.^3.*tan(1/2.*Fi*pi/180) - S.^4.*tan(1/2.*Fi*pi/180).^4 - 2.*S.^4.*tan(1/2.*Fi*pi/180).^2 - S.^4).^(1/2) - 2.*C.*S + 4.*C.*R.*tan(1/2.*Fi*pi/180) - 2.*C.*S.*tan(1/2.*Fi*pi/180).^2)./(C.^2.*tan(1/2.*Fi*pi/180).^2 + C.^2 + 2.*C.*P.*tan(1/2.*Fi*pi/180).^2 + 2.*C.*P - 2.*C.*R.*tan(1/2.*Fi*pi/180).^2 + 2.*C.*R - L.^2.*tan(1/2.*Fi*pi/180).^2 - L.^2 + P.^2.*tan(1/2.*Fi*pi/180).^2 + P.^2 - 2.*P.*R.*tan(1/2.*Fi*pi/180).^2 + 2.*P.*R + R.^2.*tan(1/2.*Fi*pi/180).^2 + R.^2 - 4.*R.*S.*tan(1/2.*Fi*pi/180) + S.^2.*tan(1/2.*Fi*pi/180).^2 + S.^2)); %+ 2.*pi;
cyklUpred = 2.*atan(((- C.^4*t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R.^2.*S.*tanFi + 4.*P.*R.*S.^2.*tanFi.^4 - 4.*P.*R.*S.^2 - R.^4.*tanFi.^4 - 2.*R.^4.*tanFi.^2 - R.^4 + 8.*R.^3.*S.*tanFi.^3 + 8.*R.^3.*S.*tanFi - 2.*R.^2.*S.^2.*tanFi.^4 - 20.*R.^2.*S.^2.*tanFi.^2 - 2.*R.^2.*S.^2 + 8.*R.*S.^3.*tanFi.^3 + 8.*R.*S.^3.*tanFi - S.^4.*tanFi.^4 - 2.*S.^4.*tanFi.^2 - S.^4).^(1/2) - 2.*C.*S + 4.*C.*R.*tanFi - 2.*C.*S.*tanFi.^2)./(C.^2.*tanFi.^2 + C.^2 + 2.*C.*P.*tanFi.^2 + 2.*C.*P - 2.*C.*R.*tanFi.^2 + 2.*C.*R - L.^2.*tanFi.^2 - L.^2 + P.^2.*tanFi.^2 + P.^2 - 2.*P.*R.*tanFi.^2 + 2.*P.*R + R.^2.*tanFi.^2 + R.^2 - 4.*R.*S.*tanFi + S.^2.*tanFi.^2 + S.^2)); %+ 2.*pi;
xupred = asin((S+C.*sin(cyklUpred)-ypaka)/L);


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);
tanFi=tan(1/2.*Fi*pi/180);
tanFi0=tan(1/2.*Fi0*pi/180);
ypaka0 = R*sinFi0;
ypaka = R*sinFi;
xpaka = R*cosFi;
S=Szad;
P=Pzad;
%Vypocet zadni cykliky
cyklUzad = 2.*atan(((- C.^4*tanFi.^4 - 2.*C.^4.*tanFi.^2 - C.^4 + 2.*C.^2.*L.^2.*tanFi.^4 + 4.*C.^2.*L.^2.*tanFi.^2 + 2.*C.^2.*L.^2 + 2.*C.^2.*P.^2.*tanFi.^4 + 4.*C.^2.*P.^2.*tanFi.^2 + 2.*C.^2.*P.^2 - 4.*C.^2.*P.*R.*tanFi.^4 + 4.*C.^2.*P.*R + 2.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)^2 + P^2 - 2*P*R*tan(1/2*fi)^2 + 2*P*R + R^2*tan(1/2*fi)^2 + R^2 - 4*R*S*tan(1/2*fi) + S^2*tan(1/2*fi)^2 + S^2)) + 2*pi*k
xuzad = asin((S+C.*sin(cyklUzad)-ypaka)/L);
%stranova kontrola
for k=1:length(Fi),
    cc=C*cos(cyklUzad(k));
    xx=P+xpaka(k)-L*cos(xuzad(k));
    if abs(cc-xx)>0.000001
        xuzad(k)=pi-xuzad(k);
    end
end
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);
tanFi=tan(1/2.*Fi*pi/180);
tanFi0=tan(1/2.*Fi0*pi/180);
ypaka0 = R*sinFi0;
ypaka = R*sinFi;
xpaka = R*cosFi;
% vypocet idealni cykliky
cyklU=sin((ypaka-ypaka0)./C);
%smernice
alfazad = ((2.*(tanFi0.^2 + 1).*(- C.^4.*tanFi0.^3 - C.^4.*tanFi0 + 2.*C.^2.*L.^2.*tanFi0.^3 + 2.*C.^2.*L.^2.*tanFi0 + 2.*C.^2.*P.^2.*tanFi0.^3 + 2.*C.^2.*P.^2.*tanFi0 - 4.*C.^2.*P.*R.*tanFi0.^3 + 2.*C.^2.*R.^2.*tanFi0.^3 + 2.*C.^2.*R.^2.*tanFi0 - 6.*C.^2.*R.*S.*tanFi0.^2 - 2.*C.^2.*R.*S + 2.*C.^2.*S.^2.*tanFi0.^3 + 2.*C.^2.*S.^2.*tanFi0 - L.^4.*tanFi0.^3 - L.^4.*tanFi0 + 2.*L.^2.*P.^2.*tanFi0.^3 + 2.*L.^2.*P.^2.*tanFi0 - 4.*L.^2.*P.*R.*tanFi0.^3 + 2.*L.^2.*R.^2.*tanFi0.^3 + 2.*L.^2.*R.^2.*tanFi0 - 6.*L.^2.*R.*S.*tanFi0.^2 - 2.*L.^2.*R.*S + 2.*L.^2.*S.^2.*tanFi0.^3 + 2.*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.^2.*S.^2.*tanFi0.^2 - 2.*R.^2.*S.^2 + 8.*R.*S.^3.*tanFi0.^3 + 8.*R.*S.^3.*tanFi0 - S.^4.*tanFi0.^4 - 2.*S.^4.*tanFi0.^2 - S.^4).^(1./2) - 2.*C.*S + 4.*C.*R.*tanFi0 - 2.*C.*S.*tanFi0.^2).^2./(C.^2.*tanFi0.^2 + C.^2 + 2.*C.*P.*tanFi0.^2 + 2.*C.*P - 2.*C.*R.*tanFi0.^2 + 2.*C.*R - L.^2.*tanFi0.^2 - L.^2 + P.^2.*tanFi0.^2 + P.^2 - 2.*P.*R.*tanFi0.^2 + 2.*P.*R + R.^2.*tanFi0.^2 + R.^2 - 4.*R.*S.*tanFi0 + S.^2.*tanFi0.^2 + S.^2).^2 + 1);
alfa=(R*cos((ypaka0)/C)*cosFi0)/C;
alfa0=R/C;
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)] )
%linerizovana cyklika
cyklUidealpred=alfapred.*(Fi-Fi0);
cyklUidealzad=alfazad.*(Fi-Fi0);
cyklUideal=alfa.*(Fi-Fi0);
cyklUideal0=alfa0.*(Fi-Fi0);
%statisticke odchylky
DU0=cyklU*180/pi-cyklUideal0;
xY0=sum(DU0)/size;
odY0 = (sum((DU0-xY0).^2)/size).^0.5;
disp(['U-Uideal0 chyba ' num2str(xY0) '  ' num2str(odY0) ' min' num2str(min(DU0)) ' max' num2str(max(DU0)) ' dmax' num2str(max(DU0)-min(DU0))])
DUzad0=cyklUzad*180/pi-cyklUideal0;
xYzad0=sum(DUzad0)/size;
odYzad0 = (sum((DUzad0-xYzad0).^2)/size).^0.5;
disp(['Uzad-Uideal0 chyba ' num2str(xYzad0) '  ' num2str(odYzad0) ' min' num2str(min(DUzad0)) ' max' num2str(max(DUzad0)) ' dmax' num2str(max(DUzad0)-min(DUzad0))])
DUpred0=cyklUpred*180/pi-cyklUideal0;
xYpred0=sum(DUpred0)/size;
odYpred0 = (sum((DUpred0-xYpred0).^2)/size).^0.5;
disp(['Upred-Uideal0 chyba ' num2str(xYpred0) '  ' num2str(odYpred0) ' min' num2str(min(DUpred0)) ' max' num2str(max(DUpred0)) ' dmax' num2str(max(DUpred0)-min(DUpred0))])
DU=cyklU*180/pi-cyklUideal;
xY=sum(DU)/size;
odY = (sum((DU-xY).^2)/size).^0.5;
disp(['U-Uideal chyba ' num2str(xY) '  ' num2str(odY) ' min' num2str(min(DU)) ' max' num2str(max(DU)) ' dmax' num2str(max(DU)-min(DU))])
DUzad=cyklUzad*180/pi-cyklUideal;
xYzad=sum(DUzad)/size;
odYzad = (sum((DUzad-xYzad).^2)/size).^0.5;
disp(['Uzad-Uideal chyba ' num2str(xYzad) '  ' num2str(odYzad) ' min' num2str(min(DUzad)) ' max' num2str(max(DUzad)) ' dmax' num2str(max(DUzad)-min(DUzad))])
DUpred=cyklUpred*180/pi-cyklUideal;
xYpred=sum(DUpred)/size;
odYpred = (sum((DUpred-xYpred).^2)/size).^0.5;
disp(['Upred-Uideal chyba ' num2str(xYpred) '  ' num2str(odYpred) ' min' num2str(min(DUpred)) ' max' num2str(max(DUpred)) ' dmax' num2str(max(DUpred)-min(DUpred))])
DUmezi=(cyklUpred-cyklUzad)*180/pi;
xYmezi=sum(DUmezi)/size;
odYmezi = (sum((DUmezi-xYmezi).^2)/size).^0.5;
disp(['Upred-Uzad chyba ' num2str(xYmezi) '  ' num2str(odYmezi) ' min' num2str(min(DUmezi)) ' max' num2str(max(DUmezi)) ' dmax' num2str(max(DUmezi)-min(DUmezi))])
%grafy
figure()
plot(Fi,cyklU*180/pi,Fi,cyklUzad*180/pi,Fi,cyklUpred*180/pi,Fi,cyklUideal,Fi,cyklUidealzad,Fi,cyklUidealpred,Fi,cyklUideal0)
title('Vychylky cykliky');
xlabel('Fi');
ylabel('Vychylka C');
legend('Zjednodusena cyklika','Zadni','Predni','Linearizovane zjednoduseny','Linearizovane zadni','Linerazivone predni','Linerizovany pri Fi=0');
plot(Fi,DU,Fi,DUzad,Fi,DUpred)
title('Odchylky cykliky(zaklad linearizovana zjednodusena cykl.)');
xlabel('Fi');
ylabel('chyba C');
legend('Zjednodusena cyklika','Zadni','Predni');
figure()
plot(Fi,cyklU*180/pi-cyklUidealpred,Fi,cyklUzad*180/pi-cyklUidealpred,Fi,cyklUpred*180/pi-cyklUidealpred)
title('Odchylky cykliky(zaklad linearizovana predni)');
xlabel('Fi');
ylabel('chyba C');
legend('Zjednoduseny cyklika','Zadni','Predni');
figure()
plot(Fi,DUmezi,Fi,(cyklU-cyklUzad)*180/pi,Fi,(cyklU-cyklUpred)*180/pi)
title('Odchylky cykliky mezisebou');
xlabel('Fi');
ylabel('chyba C');
legend('Chyba mezi zadni a predni','Zadni ku linearizovane cykl.','Predni ku linearizovane cykl.');
figure()
plot(Fi,xupred*180/pi,Fi,xuzad*180/pi)
title('Mezi uhly');
xlabel('Fi');
ylabel('Uhel');
legend('Predni','Zadni');
%animace

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);
tanFi=tan(1/2.*Fi*pi/180);
tanFi0=tan(1/2.*Fi0*pi/180);
ypaka0 = R*sinFi0;
ypaka = R*sinFi;
xpaka = R*cosFi;
figure()
axis([-20 25 -10 35])
hold on
for k=1:5:length(Fi),
    sinXu = sin(xupred(k));
    cosXu = cos(xupred(k));
    sinC = sin(cyklUpred(k));
    cosC = cos(cyklUpred(k));
    plot([C*cosC-Ppred -Ppred -Ppred 0 xpaka(k) xpaka(k)-L*cosXu],[Spred+C*sinC Spred 0 0 ypaka(k) ypaka(k)+L*sinXu])
end
title('Animace predni');

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);
tanFi=tan(1/2.*Fi*pi/180);
tanFi0=tan(1/2.*Fi0*pi/180);
ypaka0 = R*sinFi0;
ypaka = R*sinFi;
xpaka = R*cosFi;
figure()
axis([-20 25 -10 35])
S=Szad;
P=Pzad;
hold on
for k=1:5:length(Fi),
    sinXu = sin(xuzad(k));
    cosXu = cos(xuzad(k));
    sinC = sin(cyklUzad(k));
    cosC = cos(cyklUzad(k));
    plot([C*cosC-Pzad -Pzad -Pzad 0 xpaka(k) xpaka(k)-L*cosXu],[Szad+C*sinC Szad 0 0 ypaka(k) ypaka(k)+L*sinXu])
end
title('Animace zadni');
