Routine to calculate residual from coupling parameters