Basically it was reusing the code for HandC that was done without the df_ext as argument. I think a way to shrink it would be to call [H,C] = HandC(q,qd,f_ext) and then compute the dH and dC inside that new function.