function [COEF,dCOEF,ddCOEF] = polyfit2( x,p,v,a )

 X_dim = length(x);
 ORDER = length(p)+length(v)+length(a);
 for k=1:ORDER,
     if k<=X_dim,
        for l=1:ORDER,
           MATRIZ_x(k,l) = x(k)^(ORDER-l);
        end
     end
     if k>X_dim & k<=2*X_dim,
        for l=1:ORDER,
           if ORDER-l-1 >= 0,
              MATRIZ_x(k,l) = (ORDER-l)*x(k-X_dim)^(ORDER-l-1);
           else
              MATRIZ_x(k,l) = 0.0;
           end
        end
     end
     if k>2*X_dim,
        for l=1:ORDER,
           if ORDER-l-2 >= 0,
              MATRIZ_x(k,l) = (ORDER-l)*(ORDER-l-1)*x(k-2*X_dim)^(ORDER-l-2);
           else
              MATRIZ_x(k,l) = 0.0;
           end

       end
    end
 end
   COEF = inv(MATRIZ_x)*[p; v; a];
  array = ORDER-1:-1:1;
  dCOEF = array'.*COEF(1:ORDER-1);
  array = ORDER-2:-1:1;
  if ( isempty(array') | isempty(dCOEF(1:ORDER-2)) ),
      ddCOEF = [];
  else
      ddCOEF = array'.*dCOEF(1:ORDER-2);
  end
