function to calculate sum of all squared errors