Function to compute the error for given data