function for calculating the fields