function [xdot,Qdot,Rdot] = deriv (x,Q,R,ffun,param);

    [xdot,AQ] = ffun(x,Q,param);
    
    QTAQ = Q'*AQ;
    S = tril(QTAQ,-1);
    S = S - S';
    
    B = QTAQ - S;
    Rdot = B*R;
    
    Qdot = AQ - Q*B;
    