function [pos,vel,ace] = PolyTrajGenerator(t,tf,pos,vel,ace)

 % 3rd order polynomial: ZERO initial and final velocities ----------------
  if isempty(ace),
    
     Pi = pos(1); 
     Pf = pos(2);
    dPi = vel(1);
    dPf = vel(2);
     
    a0 = Pi;
    a1 = dPi;
    a2 = 3/tf^2*(Pf-Pi)-1/tf*(dPf+2*dPi);
    a3 = -2/tf^3*(Pf-Pi)+(dPf+dPi)/tf^2;
    
    pos = a3*t.^3+a2*t.^2+a1*t+a0;
    vel = 3*a3*t.^2+2*a2*t+a1;
    ace = 6*a3*t+2*a2;
   
  else 
 % 5th order polynomial: ZERO initial and final velocities&accelerations --
 DIM = length(t);
 tp = [0 tf]';
 %tp = [t(1) tf]';

   p = pos';
  dp = vel';
 ddp = ace';

 [COEFp, COEFv, COEFa] = polyfit2(tp, p, dp, ddp);
 
% Polinomials evaluation along time ---------------------------------------
 pos = polyval(COEFp, t);
 vel = polyval(COEFv, t);
 ace = polyval(COEFa, t);
 end
%-----------------------------------------------------------------------end
end