You can derive the relevant expression from the code posted. Look at the last two loops of the Trend function.