in reply to How to graph trendline
I'm not sure exactly what your calculation does, but Statistics::Descriptive can calculate a least squares fit, if that's any use to you?